Docker Networking Basics: Bridge, Host, and Overlay Networks

Networking in Docker can be confusing at first, but it’s critical for connecting containers to each other and the outside world. In this guide, you’ll learn the basics of bridge, host, and overlay networks—with simple, Python-based examples.

What Is Docker Networking?

Docker provides built-in networking drivers to allow containers to communicate:

  • With each other
  • With the host
  • Or across multiple Docker daemons (in swarm mode)

Docker networking abstracts low-level configuration into a set of well-defined network drivers.

1. Bridge Network (Default)

Best for: Containers on the same host communicating in user-defined networks.

When you run a container without specifying a network, it’s attached to the bridge network (unless overridden).

Create a custom bridge network:

docker network create my_bridge_net

Python Example – Two containers talking

DockerNetworkExample/
├── server.py
├── client.py
├── Dockerfile

server.py

from http.server import BaseHTTPRequestHandler, HTTPServer

class SimpleHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.end_headers()
        self.wfile.write(b"Hello from server")

HTTPServer(('', 8000), SimpleHandler).serve_forever()

client.py

import requests
res = requests.get("http://server-container:8000")
print("Response:", res.text)

Dockerfile

FROM python:3.11-slim
WORKDIR /app
COPY . .
RUN pip install requests
CMD ["python", "client.py"]

Run:

# Build image
docker build -t python-network-demo .
# Run server
docker run -d --name server-container --network my_bridge_net python-network-demo python server.py

# Run client
docker run --rm --network my_bridge_net python-network-demo python client.py

The client can resolve server-container by name because they’re in the same bridge network.

2. Host Network

Best for: High-performance or port-sensitive applications on Linux hosts only.

Here, the container shares the host’s network namespace. No port mapping is needed.

docker run --rm --network host python-network-demo python server.py

Access the server directly at localhost:8000 from the host.

⚠️ On Windows/macOS, this mode behaves differently due to Docker Desktop’s VM layer.

3. Overlay Network (Docker Swarm)

Best for: Multi-host networking in a Docker Swarm cluster.

Overlay networks let containers on different hosts communicate securely.

docker swarm init

docker network create --driver overlay my_overlay_net

Then deploy services that use this network. Example omitted here, as it requires swarm setup, but the concept is key for scaling microservices across machines.

Summary Table

Network TypeScopeHost AccessInter-Container DNSUse Case
Bridge
Single-host
Via port✅ Yes
Default, dev/testing
HostSingle-hostNative❌ NoHigh-perf, same-port apps (Linux)
OverlayMulti-host
Yes
✅ Yes
Docker Swarm, multi-node clusters

Clean Up

docker network rm my_bridge_net
docker rm -f server-container
docker network rm my_overlay_net  # if swarm used

Final Thoughts

Understanding Docker’s networking options is vital to building scalable, secure, and performant applications. Whether you’re running a few containers on a laptop or orchestrating dozens across servers, choosing the right network driver makes a big difference.

This is the end of the Docker Networking Basics: Bridge, Host, and Overlay Networks.

You can read more useful articles like Understanding Docker Volumes and Bind Mounts.

Follow us for the more helpful posts!

We hope this is a useful post for you.

 

5 1 vote
Article Rating

Aaron LX

Aaron is a passionate writer, crazy about shopping, eCommerce and trends. Besides his outstanding research skills and a positive mind, Aaron eagerly shares his experience with the readers.

Leave a Reply or put your Question here

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x