DEV Community

丁久
丁久

Posted on • Originally published at dingjiu1989-hue.github.io

Docker Networking: Bridge, Overlay, Host, Macvlan, and Troubleshooting

This article was originally published on AI Study Room. For the full version with working code examples and related articles, visit the original post.

Docker Networking: Bridge, Overlay, Host, Macvlan, and Troubleshooting

Docker Networking: Bridge, Overlay, Host, Macvlan, and Troubleshooting

Docker Networking: Bridge, Overlay, Host, Macvlan, and Troubleshooting

Docker Networking: Bridge, Overlay, Host, Macvlan, and Troubleshooting

Docker Networking: Bridge, Overlay, Host, Macvlan, and Troubleshooting

Introduction

Docker networking is a critical component of containerized applications. Understanding the available network drivers and their behavior is essential for designing secure, performant multi-container deployments. Docker provides five built-in network drivers: bridge, host, overlay, macvlan, and none. Each serves different use cases with distinct trade-offs.

This article explores each driver in detail, along with network policies and troubleshooting techniques.

Bridge Networks

The bridge network driver creates an internal virtual network within the Docker host. Containers connected to the same bridge network can communicate using IP addresses or container names (when embedded DNS is enabled). The default bridge network (docker0) has limitations: containers cannot resolve each other by name unless linked — a deprecated feature.

User-defined bridge networks overcome these limitations with automatic DNS resolution and better isolation. They also support dynamic attachment and detachment, allowing containers to be moved between networks without restarting.

docker network create --driver bridge --subnet 172.20.0.0/16 my-network

docker run --network my-network --name web nginx

Port publishing maps container ports to host ports using the -p flag. Each published port consumes a host port, making bridge networks unsuitable for running multiple containers that all need port 80 without an external load balancer or reverse proxy.

Host Networks

The host network driver removes network isolation between container and host. The container shares the host's network stack directly, meaning ports are exposed without mapping. This provides the best network performance since there is no bridge or NAT layer.

Host networking is ideal for network-intensive applications where performance is critical, such as metrics collectors, network monitoring tools, or applications needing direct access to host network interfaces. The trade-off is reduced portability and the inability to run multiple containers on the same host port.

Overlay Networks

Overlay networks enable communication between containers across multiple Docker hosts. They are essential for Docker Swarm services and for multi-host container communication in general. The overlay network driver creates a distributed network using VXLAN encapsulation.

docker network create --driver overlay --attachable my-overlay

Traffic between containers on an overlay network is encrypted by default using IPSec. The control plane manages distributed network state, ensuring consistent connectivity as services scale up and down.

Overlay networking requires a key-value store (Docker Swarm's built-in raft consensus provides this). Latency is slightly higher than bridge networking due to VXLAN encapsulation overhead.

Macvlan Networks

The macvlan driver assigns a MAC address to each container, making it appear as a physical device on the network. Containers can be assigned IP addresses from the same subnet as the host, enabling direct communication with external systems without port mapping.

Macvlan is useful for legacy applications that expect direct network attachment, monitoring tools that need to inspect network traffic, and environments where IP address assignment must come from a specific pool. The main limitation is that many cloud providers (AWS, GCP, Azure) restrict MAC addresses on their virtual networks, making macvlan impractical in those environments.

Network Policies and Security

Docker's built-in security features include:

  • Network isolation between different bridge networks

  • User-defined networks for communication control

  • --internal flag to prevent external access

  • iptables rules managed by Docker for traffic filtering

For production deployments, combining Docker user-defined networks with external firewalls and service meshes provides defense in depth. Each container should be connected only to network


Read the full article on AI Study Room for complete code examples, comparison tables, and related resources.

Found this useful? Check out more developer guides and tool comparisons on AI Study Room.

Top comments (0)