Docker Network Bridge: What It Is and How It Works
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
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
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.