π Docker Networking: A Complete Guide¶
1οΈβ£ Introduction to Docker Networks¶
Docker provides different types of networks to enable communication between containers, host systems, and external networks.
Containers can connect to each other using IP addresses or container names (DNS resolution depends on the network type).
2οΈβ£ Types of Docker Networks¶
| Network Type | Scope | Default? | Container-to-Container Communication | External Connectivity | Use Case |
|---|---|---|---|---|---|
| bridge | Local | β Yes | β Via container name | β
(via -p flag) | Isolated container networking |
| host | Local | β No | β Uses hostβs network | β Uses hostβs IP | Performance optimization |
| none | Local | β No | β No networking | β No networking | Security, isolated workloads |
| overlay | Multi-Host | β No | β Across hosts | β | Swarm services |
| macvlan | Local | β No | β Uses unique MAC addresses | β Acts as a physical device | Direct access to LAN |
3οΈβ£ Detailed Explanation of Each Network Type¶
π 1. Bridge Network (Default)¶
- What Happens?
- A virtual bridge (
docker0) is created. - Containers get an IP from a private subnet (e.g.,
172.17.0.0/16). -
Containers can communicate only within the same bridge network.
-
How Containers Connect?
β By IP Address: Example βping 172.17.0.2
β By Name: DNS resolves names βping container2 -
Use Case:
- Running standalone containers that need isolated networking.
-
Example: A database container communicating with an app container.
-
Commands:
docker network create my_bridge docker run --network=my_bridge --name=app nginx docker run --network=my_bridge --name=db mysql
π 2. Host Network¶
- What Happens?
- The container shares the hostβs network stack.
-
No separate IP address for the container.
-
How Containers Connect?
-
Directly using the hostβs IP.
-
Use Case:
- Low-latency applications that need better network performance (e.g., gaming servers).
-
Avoiding port conflicts in monitoring tools.
-
Commands:
docker run --network=host -d nginx -
Difference from Bridge:
- No isolation β Containers share the same network as the host.
π 3. None Network¶
- What Happens?
- No network is assigned.
-
No external or internal connectivity.
-
Use Case:
- Security-sensitive applications (e.g., forensic analysis).
-
Data-processing containers that donβt need networking.
-
Commands:
docker run --network=none busybox ifconfig
π 4. Overlay Network (Multi-Host Networking)¶
- What Happens?
- Connects containers across multiple Docker hosts.
-
Uses an internal VXLAN for encrypted communication.
-
Use Case:
- Docker Swarm deployments (multi-container, multi-host networking).
-
Microservices that need cross-host communication.
-
Commands:
docker network create -d overlay my_overlay docker service create --network=my_overlay nginx -
Example:
- Containers running on host-1 and host-2 can communicate.
π 5. Macvlan Network¶
- What Happens?
- Assigns real MAC addresses to containers.
-
Containers appear as physical devices in the network.
-
Use Case:
- Running containers as network devices (e.g., a firewall, router).
-
Bypassing the host's IP stack.
-
Commands:
docker network create -d macvlan --subnet=192.168.1.0/24 --gateway=192.168.1.1 -o parent=eth0 my_macvlan docker run --network=my_macvlan --ip=192.168.1.100 nginx -
Example:
- The container is now directly accessible from the local network.
4οΈβ£ When to Use Which Network?¶
| Scenario | Best Network Choice |
|---|---|
| Default single-host networking | Bridge |
| High-performance applications | Host |
| Isolated, security-critical workloads | None |
| Multi-host microservices | Overlay |
| Direct LAN access (e.g., DHCP, firewalls) | Macvlan |
5οΈβ£ How Containers Connect?¶
| Network Type | Connection by IP | Connection by Name |
|---|---|---|
| Bridge | β Yes | β Yes |
| Host | β Yes (Host IP) | β No (No DNS resolution) |
| None | β No | β No |
| Overlay | β Yes | β Yes (Multi-host DNS resolution) |
| Macvlan | β Yes (LAN IP) | β No |
6οΈβ£ Real-World Use Cases¶
πΉ Use Case 1: Web App + Database in a Bridge Network¶
Scenario: Running a Python Flask app with a MySQL database.
Steps:
docker network create my_bridge
docker run -d --network=my_bridge --name=db mysql
docker run -d --network=my_bridge --name=app my-flask-app
Communication:
mysql -h db -uroot -p
πΉ Use Case 2: Nginx Reverse Proxy on Host Network¶
Scenario: Running Nginx as a reverse proxy with low latency.
Command:
docker run --network=host -d nginx
πΉ Use Case 3: Multi-Host Swarm with Overlay Network¶
Scenario: Running a multi-container app across multiple Docker hosts.
Commands:
docker network create -d overlay my_overlay
docker service create --network=my_overlay my-service
πΉ Use Case 4: Assigning Static IP with Macvlan¶
Scenario: Running a container that acts like a physical device.
Commands:
docker network create -d macvlan --subnet=192.168.1.0/24 -o parent=eth0 my_macvlan
docker run --network=my_macvlan --ip=192.168.1.100 nginx
Container now accessible at: 192.168.1.100
7οΈβ£ Summary of Key Differences¶
| Feature | Bridge | Host | None | Overlay | Macvlan |
|---|---|---|---|---|---|
| Scope | Single Host | Single Host | Single Host | Multi-Host | Single Host |
| Container-to-Container | β Yes | β No | β No | β Yes (Multi-Host) | β Yes |
| External Access | β Yes (-p) | β Yes (Host IP) | β No | β Yes | β Yes |
| Performance | πΉ Normal | π₯ High | πΉ Secure | πΉ Distributed | πΉ Direct Access |
| Use Case | Multi-container apps | Low-latency apps | Secure workloads | Swarm | LAN Access |