Skip to Content
Mengty Learns
Containers

Docker Networking & Embedded DNS

Compare bridge, host, and none network drivers, user-defined bridge DNS resolution, container-to-container traffic, and safe port publishing.

explanationintermediateTested with docker 29.x (docs)Updated Nov 27, 2026

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

TL;DR

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

DriverIsolationName DNSTypical use
User-defined bridgeIsolated L2 bridge + NATYes (127.0.0.11)Default choice for multi-container apps and Docker Compose
Default bridgeShared legacy bridgeNo (IP only)Avoid for new workloads
hostShares host network namespaceHost DNSUltra-high-throughput proxies (Linux); no -p mapping needed
noneLoopback (lo) onlyNoneAir-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:3000 when Caddy/Nginx on the host terminates TLS and proxies to the container. Using -p 3000:3000 binds 0.0.0.0 and inserts DOCKER NAT rules that can bypass default-deny UFW policies.

Real-world analogy

▶ Flow: watch it work

Container-to-Container Traffic via Embedded DNS

Overview of Container-to-Container Traffic via Embedded DNS. 4 steps.
Overview4 steps in this flow

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

When 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 bridge cannot 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

When to use
  • 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.
When to avoid
  • 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

Pros
  • 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.
Cons
  • 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