0
0
DockerConceptBeginner · 3 min read

What is Bridge Network in Docker: Explanation and Example

A bridge network in Docker is the default network type that connects containers on the same host, allowing them to communicate privately. It acts like a virtual switch that links containers so they can share data securely without exposing ports externally unless specified.
⚙️

How It Works

Think of a bridge network in Docker as a private neighborhood inside your computer where containers live. Each container gets its own house (IP address), and the bridge network acts like the roads connecting these houses so they can visit each other easily.

This network is created automatically by Docker on your machine and uses a virtual Ethernet bridge to connect containers. When containers are attached to this bridge, they can talk to each other using their IP addresses or container names, but they are isolated from the outside world unless you open doors (publish ports).

This setup helps keep container communication secure and organized, just like neighbors chatting inside a gated community without strangers walking in.

💻

Example

This example shows how to create two containers on the default bridge network and ping one from the other to test connectivity.

bash
docker run -dit --name container1 alpine sh

docker run -dit --name container2 alpine sh

docker exec container1 ping -c 3 container2
Output
PING container2 (172.17.0.3): 56 data bytes 64 bytes from 172.17.0.3: seq=0 ttl=64 time=0.123 ms 64 bytes from 172.17.0.3: seq=1 ttl=64 time=0.110 ms 64 bytes from 172.17.0.3: seq=2 ttl=64 time=0.105 ms --- container2 ping statistics --- 3 packets transmitted, 3 packets received, 0% packet loss
🎯

When to Use

Use the bridge network when you want containers on the same host to communicate privately without exposing their ports to the outside world. It is ideal for simple setups where containers need to work together, like a web server container talking to a database container.

For example, if you run a small app with a frontend and backend container on your laptop, the bridge network lets them share data securely. However, if you need containers to communicate across multiple hosts or want advanced networking features, other network types like overlay might be better.

Key Points

  • The bridge network is Docker's default network for containers on the same host.
  • It creates a private virtual network allowing containers to communicate securely.
  • Containers can reach each other by IP or container name on this network.
  • Ports are not exposed outside unless explicitly published.
  • Best for simple, single-host container communication.

Key Takeaways

The bridge network connects containers privately on the same Docker host.
Containers on a bridge network can communicate using IP addresses or names.
Ports are isolated from outside unless you publish them explicitly.
Use bridge networks for simple, local container communication setups.
Docker creates a default bridge network automatically on installation.