Skip to content

Networks

Docker networking is simple right up until it isn’t, and then it’s iptables all the way down.

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.

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:

Terminal window
docker network create mynet
docker run -d --name db --network mynet postgres:17
docker run -d --name app --network mynet myapp # can reach "db" by name

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

  • EXPOSE in a Dockerfile is documentation. It does nothing on its own. Really. Nothing.
  • ports: / -p actually 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.

What actually works:

  1. Bind to localhost: 127.0.0.1:5432:5432. Simplest, and correct.
  2. Don’t publish at all. Put the services that need access on the same Docker network.
  3. Filter in the DOCKER-USER chain, which Docker leaves alone for exactly this reason. Works, but now you’re maintaining iptables rules by hand. Godspeed.
  4. 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.

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 internet

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

A common setup: one reverse-proxy stack in front of many app stacks. Create the network once, outside any stack:

Terminal window
docker network create proxy

Then in every stack that needs it:

services:
app:
networks: [proxy, default]
networks:
proxy:
external: true

external: 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.

Terminal window
docker network ls
docker network inspect mynet # which containers are attached, with IPs
docker exec -it app getent hosts db # does the name resolve?
docker run --rm -it --network mynet nicolaka/netshoot # a container full of network tools

Most 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”:

  1. Are they on the same network? (docker network inspect)
  2. Are you using the service/container name, not localhost? Inside a container, localhost is the container itself.
  3. Are you using the container port, not the published host port?
  4. Is B listening on 0.0.0.0, not 127.0.0.1?
  5. Is B actually up? docker compose ps. Healthy, or restarting in a loop?