0
0
DockerConceptBeginner · 3 min read

Docker Network Bridge: What It Is and How It Works

A docker network bridge is a default virtual network that Docker creates to allow containers on the same host to communicate with each other. It acts like a virtual switch connecting containers, enabling them to share data securely and isolate network traffic from the outside world.
⚙️

How It Works

Think of the Docker network bridge as a virtual switch inside your computer. Just like a physical switch connects different devices in a local network, the bridge connects Docker containers so they can talk to each other easily. When you start a container, Docker attaches it to this bridge network by default, giving it an IP address and allowing it to send and receive data with other containers on the same bridge.

This bridge isolates container traffic from your main computer network, so containers communicate privately unless you explicitly expose ports. It uses Linux networking features to create this isolated environment, making sure containers can work together without interfering with your host system or other networks.

💻

Example

This example shows how to create two containers connected by the default bridge network and ping one from the other.
bash
docker run -dit --name container1 alpine sh

docker run -dit --name container2 alpine sh

# Get container1 IP address
container1_ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container1)

# Ping container1 from container2
docker exec container2 ping -c 3 $container1_ip
Output
PING 172.17.0.2 (172.17.0.2): 56 data bytes 64 bytes from 172.17.0.2: seq=0 ttl=64 time=0.123 ms 64 bytes from 172.17.0.2: seq=1 ttl=64 time=0.110 ms 64 bytes from 172.17.0.2: seq=2 ttl=64 time=0.105 ms --- 172.17.0.2 ping statistics --- 3 packets transmitted, 3 packets received, 0% packet loss round-trip min/avg/max = 0.105/0.112/0.123 ms
🎯

When to Use

Use the Docker network bridge when you want containers on the same host to communicate privately and securely. It is perfect for simple setups where containers need to share data or services without exposing them to the outside world.

For example, if you have a web server container and a database container on the same machine, connecting them via the bridge network lets the web server talk to the database safely. It also helps keep your host network clean and prevents unwanted access from outside containers or networks.

Key Points

  • The bridge network is Docker's default network for containers on the same host.
  • It acts like a virtual switch connecting containers with private IP addresses.
  • Containers on the bridge can communicate with each other and the host network by default, but are isolated from external networks unless ports are exposed.
  • You can create custom bridge networks for more control and better container communication.

Key Takeaways

Docker network bridge connects containers on the same host for private communication.
It isolates container traffic from the host and external networks by default.
Containers get private IPs and can communicate securely over the bridge.
Use bridge networks for simple multi-container setups on one machine.
Custom bridge networks offer more control over container communication.