Bridge Networks
Imagine a small neighborhood where houses (containers) are connected by private roads (bridge network). The residents can visit each other easily, but outsiders need a special gate (port mapping) to enter. This is how Docker’s bridge network works - it connects containers internally while controlling access from the outside world.
Bridge Netwrork Foundations
1. What is a Bridge Network?
- A bridge network is Docker’s default networking mode.
- It creates a private internal network on the host machine.
- Containers attached to the bridge network can communicate with each other using IP addresses or container names.
- External access requires explicit port mapping.
2. Characteristics of Bridge Networks
- Isolation: Containers in the bridge network are isolated from the host and other networks.
- Name Resolution: Containers can resolve each other by name using Docker’s built‑in DNS.
- Port Mapping: Required to expose container services externally.
- Flexibility: Multiple containers can join the same bridge network and communicate seamlessly.
3. Default vs Custom Bridge Networks
- Default Bridge Network:
- Automatically created by Docker (
bridge). - Containers can connect but must use IP addresses.
- Automatically created by Docker (
- Custom Bridge Network:
- Created by users for better control.
- Provides automatic name resolution (containers can use names instead of IPs).
4. Bridge Network Workflow
- User runs a container.
- Docker attaches it to the default bridge network.
- Containers can talk internally but need port mapping for external communication.
- Custom bridge networks allow easier service discovery.
Things to Remember
- Bridge is the default network mode for containers.
- Port mapping is essential for external access.
- Custom bridge networks improve usability with name‑based communication.
Hands‑On Lab
Step 1: Run a Container in the Default Bridge Network
docker run -d --name web1 nginx
Step 2: Inspect the Network
docker network inspect bridge
- Shows connected containers and IP addresses.
Step 3: Create a Custom Bridge Network
docker network create mybridge
Step 4: Run Containers in the Custom Network
docker run -d --network=mybridge --name web nginx
docker run -d --network=mybridge --name db redis
Step 5: Test Communication
docker exec -it web ping db
webcan resolvedbby name.
Step 6: Expose Ports for External Access
docker run -d --network=mybridge -p 8080:80 --name webapp nginx
- Access Nginx at
http://localhost:8080.
Practice Exercise
- Create a custom bridge network called
appnet. - Run three containers (
frontend,backend,database) inappnet. - Test communication between them using container names.
- Expose the
frontendcontainer on port 8081 and access it in your browser.
Visual Learning Model
Host Machine
↓
Bridge Network
├── Container A (frontend) → Port 8081
├── Container B (backend) → Internal only
└── Container C (database) → Internal only
The Hackers Notebook
Bridge networks are Docker’s default networking mode, enabling containers to communicate internally while requiring port mapping for external access. Custom bridge networks enhance usability by allowing name‑based communication, making them ideal for multi‑container applications.
