Networks
Docker networking is simple right up until it isn’t, and then it’s iptables all the way down.
Network drivers you’ll meet
Section titled “Network drivers you’ll meet”| Driver | What it is | When |
|---|---|---|
bridge |
Private virtual network on the host. Containers get internal IPs. | Default. 95% of the time. |
host |
No isolation. Container uses the host’s network stack directly. | Performance-sensitive stuff, or tools that need to see host interfaces. |
none |
No network at all. | Batch jobs that should never talk to anything. |
macvlan / ipvlan |
Container gets its own IP on your physical LAN. | Home labs, legacy apps that insist on a “real” IP. Debugging it is its own hobby. |
overlay |
Spans multiple hosts. | Swarm. If you’re here, you have other problems. |
The default bridge is a trap
Section titled “The default bridge is a trap”Containers started with plain docker run (no --network) land on the default bridge network. On that network, containers can’t resolve each other by name. Only by IP, which changes every time they restart.
User-defined networks have built-in DNS:
docker network create mynetdocker run -d --name db --network mynet postgres:17docker run -d --name app --network mynet myapp # can reach "db" by nameCompose creates a user-defined network for every project automatically (<project>_default), which is one more reason to just use Compose.
Inside a container on a user-defined network, the DNS server is 127.0.0.11. That’s Docker’s embedded resolver. Keep it in mind for nginx in a container.
Ports: publish vs expose
Section titled “Ports: publish vs expose”EXPOSEin a Dockerfile is documentation. It does nothing on its own. Really. Nothing.ports:/-pactually publishes a container port on the host.
ports: - "8080:80" # host 0.0.0.0:8080 -> container 80. World-reachable. - "127.0.0.1:8080:80" # localhost only. What you usually want behind a proxy.Containers on the same network don’t need published ports to talk to each other. They use the container port directly: http://app:8000, not http://app:8080. Most “connection refused between containers” tickets come down to this.
Docker bypasses your firewall
Section titled “Docker bypasses your firewall”What actually works:
- Bind to localhost:
127.0.0.1:5432:5432. Simplest, and correct. - Don’t publish at all. Put the services that need access on the same Docker network.
- Filter in the
DOCKER-USERchain, which Docker leaves alone for exactly this reason. Works, but now you’re maintaining iptables rules by hand. Godspeed. - Cloud firewall / security groups in front of the host. Belt and braces.
Check what’s really exposed from outside the box: nmap -Pn your.server.ip. What you think you published and what you actually published are often different lists.
Segmenting a stack
Section titled “Segmenting a stack”services: proxy: image: nginx:1.27 ports: ["80:80", "443:443"] networks: [frontend]
app: image: myapp networks: [frontend, backend]
db: image: postgres:17 networks: [backend]
networks: frontend: backend: internal: true # no outbound internetproxy can reach app but not db. db can’t reach the internet at all. If the proxy gets popped, the attacker has to work for the database. Make them work.
Sharing a network between stacks
Section titled “Sharing a network between stacks”A common setup: one reverse-proxy stack in front of many app stacks. Create the network once, outside any stack:
docker network create proxyThen in every stack that needs it:
services: app: networks: [proxy, default]
networks: proxy: external: trueexternal: true means “this already exists, don’t create it, and don’t delete it on down.” If it doesn’t exist, up fails loudly. That’s good. Loud failures are the nice kind.
Debugging
Section titled “Debugging”docker network lsdocker network inspect mynet # which containers are attached, with IPsdocker exec -it app getent hosts db # does the name resolve?docker run --rm -it --network mynet nicolaka/netshoot # a container full of network toolsMost app images ship without curl, ping, or dig, because minimal images are good, right up until you need to debug one. netshoot on the same network is how you get those tools back without installing junk into production containers.
Checklist for “container A can’t reach container B”:
- Are they on the same network? (
docker network inspect) - Are you using the service/container name, not
localhost? Inside a container,localhostis the container itself. - Are you using the container port, not the published host port?
- Is B listening on
0.0.0.0, not127.0.0.1? - Is B actually up?
docker compose ps. Healthy, or restarting in a loop?