Docker Networking & Embedded DNS
Compare bridge, host, and none network drivers, user-defined bridge DNS resolution, container-to-container traffic, and safe port publishing.
TL;DR
Put containers that talk to each other on a user-defined bridge network so they resolve names via Docker's embedded DNS (127.0.0.11) instead of fragile IP addresses. Use none for offline jobs, host only when NAT overhead matters, and publish internal ports to 127.0.0.1 so they don't leak to the public internet.
Intent
Understand how Docker isolates container network namespaces, resolves container names on user-defined bridges, and publishes ports safely to the host.
Problem
Beginners often hardcode container IPs (which change on every restart), publish database ports with -p 5432:5432 (accidentally exposing Postgres to 0.0.0.0), or attach everything to the default bridge network where container-name DNS doesn't work.
Solution
1. Core network drivers
| Driver | Isolation | Name DNS | Typical use |
|---|---|---|---|
User-defined bridge | Isolated L2 bridge + NAT | Yes (127.0.0.11) | Default choice for multi-container apps and Docker Compose |
Default bridge | Shared legacy bridge | No (IP only) | Avoid for new workloads |
host | Shares host network namespace | Host DNS | Ultra-high-throughput proxies (Linux); no -p mapping needed |
none | Loopback (lo) only | None | Air-gapped batch jobs, build sandboxes |
2. User-defined bridge DNS and container-to-container traffic
When you create a user-defined bridge (docker network create app-net), Docker injects /etc/resolv.conf pointing to 127.0.0.11 inside each attached container. Container A can dial http://api:3000 or postgres://db:5432 without publishing any ports to the host at all.
3. Port publishing (-p)
- Only publish what external callers need: Containers on the same bridge talk directly over their container ports.
- Bind loopback for local proxies: Use
-p 127.0.0.1:3000:3000when Caddy/Nginx on the host terminates TLS and proxies to the container. Using-p 3000:3000binds0.0.0.0and insertsDOCKERNAT rules that can bypass default-deny UFW policies.
Real-world analogy
▶ Flow: watch it work
Container-to-Container Traffic via Embedded DNS
Press Start or tap any step dot to trace the path.
▸All steps (4)
Code
Creating an isolated user-defined bridge and publishing only the app port on loopback:
# Create a user-defined bridge network (not run)
docker network create app-net
# Run Postgres with zero published host ports (only reachable on app-net) (not run)
docker run -d --name db --network app-net -e POSTGRES_PASSWORD=dev postgres:19
# Run API on the same bridge, binding host port 3000 to loopback only (not run)
docker run -d --name api --network app-net -p 127.0.0.1:3000:3000 my-api:1.0When you DON'T need this
- Single standalone container: A lone CLI tool or static file server doesn't need custom bridge networks.
- Multi-node orchestration: Once containers span multiple hosts, a single-host
bridgecannot route across machines—you need an overlay network or Kubernetes CNI. - Cost / complexity trade-off: Creating separate bridges (
frontend-net,backend-net) adds a few lines of Compose config in exchange for strong east-west isolation (preventing a compromised proxy from reaching internal admin ports).
When to use / when to avoid
- Use user-defined bridge networks (or Docker Compose default project networks) for all multi-container apps.
- Use 127.0.0.1:<host>:<container> when publishing ports behind a host reverse proxy.
- Never attach containers to the default bridge network when they need name-based DNS.
- Never publish database ports with -p 5432:5432 on a public VPS.
How to implement
Define dedicated bridge networks
Group services into minimal networks (e.g., edge for proxy + app, internal with --internal for app + DB).
Reference services by name
Use postgres://db:5432/app in environment variables instead of localhost or container IPs.
Audit published ports
Run docker ps and verify PORTS shows 127.0.0.1:... or only your intended public proxy port.
Pros / Cons
- Built-in service discovery via 127.0.0.11 without external DNS servers.
- Internal services stay unreachable from the host network when ports are not published.
- Single-host scope—bridge networks do not span multiple VMs without an overlay driver.
- Publishing ports without a bind IP bypasses UFW inbound rules via Docker NAT.
Relations
Check yourself
Check Yourself
1. Container 'api' wants to connect to container 'db' on port 5432. Both are on a user-defined bridge 'app-net', and 'db' has no -p flag. Will 'postgres://db:5432' work?
2. Why is '-p 5432:5432' dangerous on a public VPS running UFW?
Sources
Related Notes
Docker Compose
Run multi-container stacks locally or on a single host with automatic service DNS, health-gated depends_on, isolated networks, env files, secrets, and profiles.
Volumes vs Bind Mounts vs tmpfs
Choose the right Docker storage mount—named volumes, bind mounts, or tmpfs—avoid UID permission mismatches, and back up persistent volumes safely.