Docker networking confuses people because it hides a real Linux network behind friendly defaults. A container that "just works" on your laptop fails on a server because the default bridge, the port mapping, or the DNS behaved differently than you assumed.

I have debugged the same three failures for years: a container that cannot reach another container, a service that is reachable from the host but not from the internet, and a Swarm service that resolves to a dead IP. Every one of them traces back to which network driver was chosen and how it was configured.

This is the mental model I use in production, and the one I would defend in a DevOps interview.

How a Container Gets a Network

Every container gets a virtual Ethernet interface (veth) that connects it to a bridge on the host. The container has its own IP address, its own network namespace, and its own routing table. From the container's point of view it is a small machine on a private network. From the host's point of view it is a process with an interface plugged into a bridge.

docker network ls docker network inspect bridge ip addr show docker0 

The default bridge network is fine for a single container you are testing. It is the wrong default for production, because containers on the default bridge can only reach each other by IP address — there is no automatic DNS name resolution between them. That one fact causes half of all "container A cannot reach container B" tickets.

Bridge: The Default, Done Right

Create a user-defined bridge network and the behaviour changes completely:

docker network create app-net docker run -d --name web --network app-net nginx docker run -d --name api --network app-net my-api:latest 

On a user-defined bridge, containers resolve each other by name. web can reach api at http://api:8080 with no IP addresses hardcoded and no links. That is the single most important Docker networking habit: always use a named network, never the default bridge, for anything with more than one container.

A bridge network also isolates: containers on different bridge networks cannot talk to each other unless you connect them explicitly. That is a feature — use it to separate your frontend, backend, and database networks.

Port mapping, and the mistake everyone makes

docker run -d -p 8080:80 --network app-net nginx 

-p 8080:80 publishes the container's port 80 on the host's port 8080. Two common mistakes: publishing a database port to the world (-p 5432:5432 with no firewall in front of it), and assuming -p is needed for container-to-container traffic. It is not — containers on the same network talk directly on their internal ports. Publish only what the outside world needs.

Host: Fast, and Dangerous

docker run -d --network host nginx 

With --network host, the container shares the host's network namespace. No NAT, no port mapping, full speed — and no isolation. The container can bind any host port, see host interfaces, and conflict with host services.

Use host networking only where the performance or the protocol genuinely needs it — a high-throughput packet capture tool, a service that must bind a low port with specific behaviour. Never use it as a shortcut because bridge networking "did not work". That shortcut becomes a security and debugging problem in production.

Overlay: Networking Across Hosts

A single host is easy. A Swarm cluster or a multi-host setup needs containers on different machines to talk as if they were on one network. That is what overlay does:

docker network create -d overlay --attachable app-overlay docker service create --name api --network app-overlay my-api:latest 

Overlay uses VXLAN to tunnel container traffic between hosts. It works, and it adds real complexity: encrypted overlay traffic, MTU overhead (VXLAN adds about 50 bytes, so lower your container MTU or your packets fragment), and DNS that resolves a service name to a virtual IP with built-in load balancing.

In practice, most teams I work with do not run overlay by hand any more — Kubernetes CNI plugins (Calico, Cilium, Flannel) solve the same problem with better observability. But understanding overlay is how you understand what a CNI is doing under the hood, and it still comes up in interviews and in legacy Swarm setups.

Macvlan and None: The Edge Cases

  • Macvlan gives a container a MAC address on your physical network, so it looks like a real machine to the rest of the LAN. Useful for legacy apps that need a real IP on the corporate network, or for DHCP. The cost: the container is outside Docker's DNS and isolation, and the host cannot talk to its own macvlan containers without a second interface.
  • None gives a container no network at all. Use it for batch jobs that should never talk to anything, or for a container you will configure by hand.

Know they exist. Do not reach for them first.

DNS and Service Discovery: How Containers Find Each Other

Docker runs an embedded DNS server at 127.0.0.11 inside every container on a user-defined network. When web asks for api, that DNS server answers with the container's current IP. That is why renaming or recreating a container does not break name resolution — the name stays stable while the IP changes.

docker exec web getent hosts api docker exec web nslookup api 

In Compose, this is automatic: services in the same Compose file share a network and resolve each other by service name. In Kubernetes, the same idea scales up to Services and CoreDNS — the pattern is identical, only the implementation changes.

Two rules: never hardcode container IPs in config, and never assume a container's IP survives a restart. Names are stable. IPs are not.

Troubleshooting: The Commands That Actually Help

When networking breaks, work from the container outward:

# 1. Is the container on the right network? docker inspect web --format '{{json .NetworkSettings.Networks}}' # 2. Can it resolve the name? docker exec web getent hosts api # 3. Can it reach the port? docker exec web sh -c 'cat < /dev/null > /dev/tcp/api/8080 && echo OK' # 4. Is anything listening inside the target? docker exec api ss -tlnp # 5. What does the host see? iptables -t nat -L DOCKER -n 

Most failures land in one of four buckets: wrong network, DNS name wrong, the app is listening on 127.0.0.1 inside the container instead of 0.0.0.0, or a firewall rule on the host is dropping the published port. Check them in that order and you will find it in minutes, not hours.

Production Pitfalls Checklist

The failures I actually see, in order of frequency:

  1. Default bridge in production. No DNS between containers, so everything is hardcoded by IP and breaks on restart.
  2. App listening on 127.0.0.1. Works from inside the container, unreachable from every other container. Bind 0.0.0.0.
  3. Database port published to the world. -p 5432:5432 with no firewall — a breach waiting to happen.
  4. No named network in Compose. Services in different Compose files cannot resolve each other unless you put them on a shared external network.
  5. MTU ignored on overlay. VXLAN overhead fragments packets and some services fail in strange, intermittent ways.
  6. Hardcoded container IPs. They change on every recreate. Use names.
  7. No healthcheck on the dependency. The app starts before the database is ready and crashes in a restart loop — a networking symptom with a startup-order cause.
  8. Host firewall vs Docker iptables. Docker writes its own iptables rules. Your ufw deny may not do what you think on a published port — verify from outside.

What Hiring Managers Actually Ask

If you are targeting remote DevOps or sysadmin roles, this topic comes up because it reveals how you think about systems:

  • "Two containers on the same host cannot talk — walk me through it." (Answer: check the network, check DNS by name, check the listening address, check the port, then check the host firewall.)
  • "How do services find each other across hosts?" (Answer: overlay / CNI, service discovery by name, and why IPs are never hardcoded.)
  • "How do you expose a service securely?" (Answer: publish only the ports the outside needs, keep the database on an internal network, put a reverse proxy in front, and verify the firewall from outside.)

The answer they want is never "I run docker run -p". It is the model underneath: namespaces, bridges, DNS, and knowing which layer is broken.

The Monthly Networking Routine

My monthly routine takes fifteen minutes:

  1. docker network ls — any networks nobody owns? Remove them.
  2. Check published ports from outside — anything exposed that should not be?
  3. docker exec into one service and resolve its dependencies by name — does discovery still work?
  4. Review Compose files for hardcoded IPs or links: — replace with named networks.
  5. Check the firewall against the published ports — do they match?

Do that, and Docker networking stops being a mystery and becomes a system you can reason about. Networking is infrastructure: boring, named, monitored, and never a 3 AM surprise.

Your turn: What is the first thing you check when two containers cannot talk — the network, DNS, or the listening address? What bit you the first time? Tell me in the comments.