7 Docker Networking
A focused guide to Docker Networking, connecting core concepts with practical Docker and container operations.
Docker networking is the set of features and mechanisms that allow containers to communicate with each other, with the host, and with the broader external network, encompassing different network drivers, port publishing, and name-based service discovery among containers.
The Default Bridge Network
Without any explicit network configuration, containers are attached to Docker's default bridge network, providing basic connectivity but without convenient name-based discovery between containers.
docker run -d --name container-a alpine sleep 1000
docker run -d --name container-b alpine sleep 1000
docker exec container-b ping container-a
This typically fails on the default bridge network, since it doesn't provide automatic DNS-based name resolution between containers attached to it.
User-Defined Bridge Networks
Creating a dedicated, user-defined network provides automatic name resolution between containers attached to it, making multi-container communication significantly more convenient.
docker network create mynet
docker run -d --name container-a --network mynet alpine sleep 1000
docker run -d --name container-b --network mynet alpine sleep 1000
docker exec container-b ping container-a
This succeeds, since containers on a user-defined network can resolve each other by name automatically.
Connecting to the External Network
Containers can reach the external internet by default (assuming appropriate host networking configuration), and can be made reachable from outside through port publishing.
docker run -d -p 8080:80 nginx:alpine
Inspecting Network Configuration
A container's actual network configuration — its assigned IP address, connected networks — can be inspected directly.
docker inspect myapp --format '{{json .NetworkSettings.Networks}}'
Why Understanding Docker Networking Matters
A solid grasp of Docker's networking model — particularly the distinction between the default bridge and user-defined networks — is essential for correctly designing any multi-container application that needs its components to communicate with each other reliably.