Skip to content

07. Docker Networking

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.

Docker includes several default network drivers that dictate how containers are isolated or connected.

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.

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.

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!

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.

Terminal window
# Create a custom network
docker network create my-app-net
# Run Database on the network
docker 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-image

Docker’s internal DNS is the magic that makes microservices possible without complex service discovery configurations.

  • Container Names: The name you pass via --name becomes 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.x IP 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.

To allow the outside world to reach your container, you must publish its internal port to the host.

-p HOST_PORT : CONTAINER_PORT
Terminal window
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:

Terminal window
docker run -p 127.0.0.1:8080:80 nginx

Lists all networks currently managed by Docker on the host.

Creates a new custom bridge network.

Terminal window
docker network create my-network

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).

Terminal window
docker network inspect my-network

Connects an already running container to an existing network dynamically, without needing to restart it.

Terminal window
docker network connect my-network my-running-container

Disconnects a running container from a network.

Terminal window
docker network disconnect my-network my-running-container

Deletes a network. (You cannot delete a network if containers are still attached to it).

Terminal window
docker network rm my-network

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 on 127.0.0.1 internally, Docker cannot route external traffic to it. It must bind to 0.0.0.0 inside the container.
  • Timeout: The container might be overwhelmed, or the internal routing is failing. Check docker network inspect to verify IP assignments.
  • Wrong Port: Double-check your -p mapping. Ensure you didn’t accidentally swap the HOST and CONTAINER ports (e.g., -p 80:8080 instead of -p 8080:80).
  • Wrong Network: Containers on custom-net-A cannot talk to containers on custom-net-B. Use docker network connect to bridge them if necessary.
  • Firewall Issues: Docker automatically manipulates Linux iptables to 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.