07. Docker Networking
7.1 Networking Fundamentals
Section titled “7.1 Networking Fundamentals”Before looking at Docker-specific features, you must understand the basic terms used to route traffic.
- Container IP: Every container is assigned its own unique, private IP address (usually starting with
172.x.x.x) by Docker’s internal DHCP server. - Host IP: The IP address of the actual physical (or virtual) machine running Docker (e.g., your laptop, or an AWS EC2 instance).
- Internal Ports: The port that the application inside the container is actively listening on (e.g., an Nginx container listens on internal port
80). - Published Ports: The port on the Host machine that is exposed to the outside world, acting as a gateway to the container’s internal port.
- Port Mapping: The act of securely tying a Published Port to an Internal Port so external traffic can reach the container.
7.2 Docker Network Types
Section titled “7.2 Docker Network Types”Docker includes several default network drivers that dictate how containers are isolated or connected.
Bridge
Section titled “Bridge”The default network driver. If you don’t specify a network, Docker places the container on the default bridge network. It creates a private internal network on the host, allowing containers on the same bridge to communicate, while providing NAT (Network Address Translation) for outside access.
Removes network isolation between the container and the Docker host. The container shares the host’s exact networking stack. If the container binds to port 80, it will take up port 80 directly on the host machine. (Useful for high-performance networking, but eliminates security isolation).
Completely disables all networking for the container. The container has no network interfaces (other than a local loopback). Used for highly secure, isolated processing tasks that do not need internet access.
Overlay (Conceptual)
Section titled “Overlay (Conceptual)”Used strictly for multi-host networking in Docker Swarm or Kubernetes. It creates a distributed network that spans across multiple physical machines, allowing containers on Server A to talk securely to containers on Server B as if they were on the same local switch.
7.3 Bridge Networks
Section titled “7.3 Bridge Networks”Default Bridge
Section titled “Default Bridge”When you install Docker, it creates a default bridge named bridge. All new containers are attached here unless specified otherwise.
Warning: The default bridge is considered a legacy feature. Containers on the default bridge can only communicate by their raw IP addresses, which change every time a container restarts!
Custom Bridge (User-Defined Bridge)
Section titled “Custom Bridge (User-Defined Bridge)”Always create your own custom bridge networks for applications. Custom bridges provide superior isolation and, most importantly, Automatic DNS resolution.
Container-to-Container Communication & Automatic DNS
Section titled “Container-to-Container Communication & Automatic DNS”On a custom bridge network, Docker runs a built-in DNS server. This allows containers to ping and communicate with each other using their container names rather than fragile IP addresses.
# Create a custom networkdocker network create my-app-net
# Run Database on the networkdocker run --name db --network my-app-net postgres
# Run Web app on the network# The web app can connect to the database using the hostname "db" instead of an IP!docker run --name web --network my-app-net my-web-image7.4 Docker DNS
Section titled “7.4 Docker DNS”Docker’s internal DNS is the magic that makes microservices possible without complex service discovery configurations.
- Container Names: The name you pass via
--namebecomes the DNS hostname for that container. - Service Names: In Docker Compose, the name of the service block automatically becomes the DNS hostname.
- DNS Resolution: When Container A asks to connect to “db”, the Docker DNS server intercepts the request and instantly translates “db” into the internal
172.x.x.xIP address of the database container. - Network Aliases: You can assign multiple DNS names to a single container using
--network-alias, allowing different services to refer to it by different names.
7.5 Port Publishing
Section titled “7.5 Port Publishing”To allow the outside world to reach your container, you must publish its internal port to the host.
The Syntax
Section titled “The Syntax”-p HOST_PORT : CONTAINER_PORTExamples & Understanding
Section titled “Examples & Understanding”docker run -p 8080:80 nginx- Traffic hits the Host machine on
8080. - Docker transparently routes that traffic to the Nginx container on port
80.
IP Binding:
By default, -p 8080:80 binds to 0.0.0.0 (all IPv4 interfaces on the host), meaning anyone on the internet who can reach your host can reach your container.
For security, you can bind it strictly to localhost so only traffic originating from the host machine itself can reach the container:
docker run -p 127.0.0.1:8080:80 nginx7.6 Network Commands
Section titled “7.6 Network Commands”docker network ls
Section titled “docker network ls”Lists all networks currently managed by Docker on the host.
docker network create
Section titled “docker network create”Creates a new custom bridge network.
docker network create my-networkdocker network inspect
Section titled “docker network inspect”Provides detailed JSON output about a network, including the subnet, gateway, and a list of all containers currently connected to it (along with their specific IP addresses).
docker network inspect my-networkdocker network connect
Section titled “docker network connect”Connects an already running container to an existing network dynamically, without needing to restart it.
docker network connect my-network my-running-containerdocker network disconnect
Section titled “docker network disconnect”Disconnects a running container from a network.
docker network disconnect my-network my-running-containerdocker network rm
Section titled “docker network rm”Deletes a network. (You cannot delete a network if containers are still attached to it).
docker network rm my-network7.7 Network Troubleshooting
Section titled “7.7 Network Troubleshooting”When things break, it is almost always a networking issue. Here is a checklist for the most common errors:
- DNS Problems (“Host not found”): Are the containers on the exact same custom bridge network? (Remember, the default bridge does not support DNS resolution by container name).
- Connection Refused:
Is the application inside the container actually listening on
0.0.0.0? If a web framework (like React or Django) is configured to listen only on127.0.0.1internally, Docker cannot route external traffic to it. It must bind to0.0.0.0inside the container. - Timeout:
The container might be overwhelmed, or the internal routing is failing. Check
docker network inspectto verify IP assignments. - Wrong Port:
Double-check your
-pmapping. Ensure you didn’t accidentally swap the HOST and CONTAINER ports (e.g.,-p 80:8080instead of-p 8080:80). - Wrong Network:
Containers on
custom-net-Acannot talk to containers oncustom-net-B. Usedocker network connectto bridge them if necessary. - Firewall Issues:
Docker automatically manipulates Linux
iptablesto route traffic. If you use UFW or strictly managed firewall rules on your host server, they may violently conflict with Docker, blocking all incoming container traffic.