9. Docker Networking Deep Dive
How Containers Communicate
Containers need to talk to each other and to the outside world. Docker provides several network drivers for different use cases. Understanding networking is essential for connecting your application to databases, caches, and other services.
The Default Bridge Network
When Docker is installed, it creates a default `bridge` network named `bridge`. Containers connected to this network can communicate with each other by IP address, but NOT by container name. The default bridge also does NOT provide automatic DNS resolution between containers. This makes it inconvenient for multi-container applications. Additionally, containers on the default bridge network are exposed to each other by default, which is not ideal for security. For these reasons, you should rarely use the default bridge in production. Instead, create user-defined bridge networks.
# Inspect the default bridge network docker network inspect bridge # Run two containers on default bridge docker run -d --name web nginx docker run -d --name db postgres # They can reach each other by IP (find IP with docker inspect) docker exec web ping $(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' db) # But NOT by name — this fails! docker exec web ping db # Error: unknown host
User-Defined Bridge Networks
User-defined bridge networks are superior to the default bridge. They provide automatic DNS resolution (containers can reach each other by name), better isolation, and the ability to connect/disconnect containers dynamically. When you create a user-defined network, Docker runs an embedded DNS server that resolves container names to IP addresses. This means container `web` can ping container `db` by name. User-defined networks also have better security — containers are only connected to networks you explicitly attach them to. For any multi-container application, create a dedicated network for that application's services.
# Create a user-defined bridge network docker network create myapp-network # Run containers on the network docker run -d --name web --network myapp-network nginx docker run -d --name db --network myapp-network postgres # Now they CAN reach each other by name! docker exec web ping db # Works! # Connect existing container to network docker network connect myapp-network existing-container # Disconnect container docker network disconnect myapp-network container-name # List networks docker network ls # Remove network (must have no connected containers) docker network rm myapp-network
Port Publishing: Exposing Containers to the Host
Containers have their own network namespace with private IP addresses. To access a container from outside the Docker host (your browser, another machine, a load balancer), you must publish ports. The `-p` flag maps a port on the host to a port in the container. The format is `-p host_port:container_port`. For example, `-p 8080:80` means "forward traffic from host port 8080 to container port 80". You can also publish ports randomly with `-P`, which maps a random high-numbered host port to the container's `EXPOSE`d ports. Use `docker port container_name` to see the port mappings.
# Map host port 8080 to container port 80 docker run -d -p 8080:80 --name web nginx # Now http://localhost:8080 reaches nginx inside the container # Map multiple ports docker run -d -p 8080:80 -p 8443:443 nginx # Map to specific host IP (for multi-network hosts) docker run -d -p 127.0.0.1:8080:80 nginx # Only accessible locally # Publish all exposed ports to random host ports docker run -d -P --name web nginx docker port web # Shows random port mappings # In Docker Compose services: web: ports: - "8080:80" - "127.0.0.1:8443:443"
Host Network Driver (Performance)
The `host` network driver removes network isolation between the container and the host. The container uses the host's network stack directly — no port mapping needed, and the container sees the host's network interfaces. This eliminates the performance overhead of NAT (Network Address Translation), making host networking faster. However, it also reduces security and breaks container port isolation (two containers cannot both bind to the same host port). Use host networking only when you need maximum throughput (like high-performance web servers) or when your application needs to discover host network interfaces. The host network driver is only available on Linux hosts.
# Run with host networking (Linux only) docker run -d --network host nginx # Now nginx is accessible on the host's IP address on port 80 # No -p needed! # Check if host network is available docker network ls | grep host # Use in Docker Compose (Linux only) services: app: network_mode: host
Overlay Networks (Multi-Host Communication)
Bridge networks work only on a single Docker host. For multi-host communication (Docker Swarm, Kubernetes), you need overlay networks. Overlay networks use VXLAN tunneling to connect containers across different physical machines. Docker Swarm creates an overlay network for you when you initialize the swarm. Overlay networks are transparent to your application — containers can reach each other by name even if they are on different servers. This is the foundation of container orchestration.
# Create overlay network in Swarm mode docker network create -d overlay my-overlay # Deploy service on the overlay network docker service create --network my-overlay --name web nginx # Containers on different nodes can still communicate docker service scale web=3 # Spreads across Swarm nodes
Knowledge Check
Ready to test your understanding of 9. Docker Networking Deep Dive?