Docker › Network

Docker bridge networks and embedded DNS service discovery

How user-defined Docker bridge networks, veth pairs, and the 127.0.0.11 embedded DNS server resolve container names without hardcoded IPs.

Docker bridge networks and embedded DNS service discovery

Containers connected to Docker’s default docker0 bridge can’t resolve each other by name, which triggers UnknownHostException on startup. This post walks through Linux network namespaces, veth pairs, user-defined bridges, and Docker’s 127.0.0.11 embedded DNS server with a Spring Boot and PostgreSQL example.


1. Why container-to-container DNS fails out of the box

When you start a database container and a Spring Boot container manually with docker run, you often hit this exception right during startup:

1
2
3
4
Caused by: java.net.UnknownHostException: postgres-db
    at java.base/sun.nio.ch.NioSocketImpl.connect(UnknownSocketImpl.java:567)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:318)
    ... 18 common frames omitted

A common workaround is running docker inspect postgres-db, grabbing its IP (like 172.17.0.2), and hardcoding jdbc:postgresql://172.17.0.2:5432/mydb into your app config.

Don’t do that. Container IPs change whenever containers restart in a different order or the host reboots. Hardcoding container IPs breaks on the very next restart.

So why can’t app-server resolve postgres-db even though you passed --name postgres-db?

Docker’s default docker0 bridge exists for legacy compatibility and does not enable automatic container-name DNS resolution. To get built-in service discovery, you need a user-defined bridge network.


2. Linux network namespaces and veth pairs under the hood

Every Docker container runs inside its own Linux network namespace (netns). Inside that namespace, the container gets a private loopback interface (lo), its own routing table, its own iptables chains, and its own eth0 interface—completely isolated from the host.

Packets cross that namespace boundary through a veth pair (Virtual Ethernet Pair).

flowchart TD
    subgraph Host ["Host OS (Root Network Namespace)"]
        subgraph Bridge ["Docker Bridge (L2 Virtual Switch)"]
            BR["my-app-net (br-xxxxxx)"]
        end

        VETH1["veth-app (Host Interface)"] <--> BR
        VETH2["veth-db (Host Interface)"] <--> BR
    end
    subgraph NetNS1 ["Container: app-server (netns)"]
        ETH1["eth0 (172.20.0.3)"]
    end
    subgraph NetNS2 ["Container: postgres-db (netns)"]
        ETH2["eth0 (172.20.0.2)"]
    end
    ETH1 ==>|"veth pair"| VETH1
    ETH2 ==>|"veth pair"| VETH2

How a veth pair moves packets

  • Virtual cable in pairs: Linux always creates veth interfaces in pairs. Anything pushed into one end comes out the other end immediately.
  • Bridging the namespace boundary: Docker moves one end (eth0) into the container’s network namespace and leaves the other end (vethXXXXXX) in the host’s root namespace.
  • L2 switching on the bridge: Docker attaches each host-side vethXXXXXX to a software L2 bridge (br-xxxxxx).

When app-server (172.20.0.3) opens a TCP connection to postgres-db (172.20.0.2), the packet follows five steps:

  1. app-server sends a TCP SYN packet to 172.20.0.2.
  2. The container’s routing table sends the packet out through eth0.
  3. The veth pair delivers it directly to veth-app in the host namespace.
  4. The bridge (br-xxxxxx) looks up the destination MAC address in its forwarding database (FDB) and switches the frame to veth-db.
  5. The second veth pair delivers the packet to postgres-db’s eth0.

3. Default bridge (docker0) vs. user-defined bridge

If you omit --network in docker run, Docker attaches the container to the default docker0 bridge. User-defined bridges created with docker network create behave very differently:

FeatureDefault bridge (docker0)User-defined bridge (docker network create)
Automatic DNS resolutionNot supported (IP only)Supported for container names and network aliases
Network isolationAll default containers share one flat networkIsolated per network at L2/L3
Dynamic attach/detachMust stop and recreate containersLive docker network connect / disconnect
Subnet and MTU controlGlobal /etc/docker/daemon.json onlyConfigurable per network
Legacy flagsSupports deprecated --linkUses embedded DNS instead of --link

Why docker0 still lacks DNS

Before Docker 1.9, containers discovered each other via --link, which statically wrote IP-to-name entries into /etc/hosts. Docker kept docker0 unchanged for backward compatibility and enabled the embedded DNS server only on user-defined bridges.

Never attach application stacks to the default docker0 bridge—locally or in production. Create a dedicated user-defined bridge per application stack so services get automatic DNS and clean isolation.


4. How Docker’s embedded DNS (127.0.0.11) resolves names

Inspect /etc/resolv.conf inside any container attached to a user-defined bridge:

1
2
3
$ docker exec -it app-server cat /etc/resolv.conf
nameserver 127.0.0.11
options ndots:0

Every DNS query goes to 127.0.0.11, Docker’s embedded DNS resolver inside the container’s network namespace.

sequenceDiagram
    autonumber
    participant App as Spring Boot Container
    participant DNS as Embedded DNS (127.0.0.11)
    participant Daemon as Docker Daemon (libnetwork)
    participant ExtDNS as Upstream Host DNS
    App->>DNS: Query A record for "postgres-db" (UDP:53)
    DNS->>Daemon: Look up container name/alias in network table
    alt Service exists on same user-defined network
        Daemon-->>DNS: Return container IP (172.20.0.2)
        DNS-->>App: Response: postgres-db = 172.20.0.2
    else External domain (e.g., api.github.com)
        Daemon-->>DNS: Not found internally
        DNS->>ExtDNS: Forward recursive query
        ExtDNS-->>DNS: Return public IP
        DNS-->>App: Forward DNS response
    end

How a loopback address talks to the Docker daemon

Docker installs kernel-level iptables (or nftables) NAT rules inside the container’s network namespace. When a process sends a DNS packet to 127.0.0.11:53, a PREROUTING / OUTPUT rule redirects it to the libnetwork DNS listener socket owned by the Docker daemon.

Network aliases and DNS round-robin

Docker’s embedded DNS also supports --network-alias, returning multiple A records in rotating order (DNS round-robin) when several containers share an alias:

1
2
docker run -d --network backend-net --network-alias api api-service:v1
docker run -d --network backend-net --network-alias api api-service:v2

When another container queries api, 127.0.0.11 shuffles the IP list on each response, giving you lightweight client-side L4 load balancing without an extra proxy.


5. Connecting Spring Boot and PostgreSQL without hardcoded IPs

Option 1: Docker CLI with a user-defined bridge

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 1. Create an isolated user-defined bridge
docker network create --driver bridge backend-net

# 2. Start PostgreSQL on backend-net
docker run -d \
  --name postgres-db \
  --network backend-net \
  -e POSTGRES_DB=appdb \
  -e POSTGRES_USER=appuser \
  -e POSTGRES_PASSWORD=secret \
  postgres:17-alpine

# 3. Verify embedded DNS resolution with a throwaway Alpine container
docker run --rm --network backend-net alpine nslookup postgres-db

You should see 127.0.0.11 answer with the container’s private IP:

1
2
3
4
5
Server:		127.0.0.11
Address:	127.0.0.11:53

Name:	postgres-db
Address: 172.18.0.2

Now launch Spring Boot on the same network using postgres-db directly in the JDBC URL:

1
2
3
4
5
6
7
8
9
# 4. Start Spring Boot referencing postgres-db by name
docker run -d \
  --name app-server \
  --network backend-net \
  -p 8080:8080 \
  -e SPRING_DATASOURCE_URL=jdbc:postgresql://postgres-db:5432/appdb \
  -e SPRING_DATASOURCE_USERNAME=appuser \
  -e SPRING_DATASOURCE_PASSWORD=secret \
  my-spring-app:latest

Option 2: Declarative setup with Docker Compose

In day-to-day work, compose.yaml handles this automatically. Docker Compose creates a project-scoped user-defined bridge by default, so every service name works as a DNS hostname out of the box:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
services:
  postgres-db:
    image: postgres:17-alpine
    container_name: postgres-db
    environment:
      POSTGRES_DB: appdb
      POSTGRES_USER: appuser
      POSTGRES_PASSWORD: secret
    volumes:
      - pgdata:/var/lib/postgresql/data
    networks:
      - backend-net
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U appuser -d appdb"]
      interval: 5s
      timeout: 5s
      retries: 5

  app-server:
    image: my-spring-app:latest
    container_name: app-server
    depends_on:
      postgres-db:
        condition: service_healthy
    environment:
      SPRING_DATASOURCE_URL: jdbc:postgresql://postgres-db:5432/appdb
      SPRING_DATASOURCE_USERNAME: appuser
      SPRING_DATASOURCE_PASSWORD: secret
    ports:
      - "8080:8080"
    networks:
      - backend-net

networks:
  backend-net:
    driver: bridge
    ipam:
      config:
        - subnet: 172.28.0.0/16

volumes:
  pgdata:

6. Production pitfalls and debugging commands

1) JVM DNS caching TTL

Even when Docker’s embedded DNS updates immediately after a container restarts with a new IP, the JVM can hold onto stale addresses. Historically, with a Security Manager enabled, the JVM cached DNS lookups forever (networkaddress.cache.ttl = -1), and even without one, the default 30-second cache can cause brief Connection refused bursts during rolling restarts.

Pass -Dnetworkaddress.cache.ttl=5 as a JVM flag (or set networkaddress.cache.ttl=10 in $JAVA_HOME/conf/security/java.security) so containerized Java apps pick up updated container IPs quickly.

2) When not to use bridge networks (network_mode: host trade-offs)

If you switch a container to network_mode: host to remove veth and bridge overhead, the container shares the host’s network namespace directly. You get bare-metal network throughput, but Docker’s embedded DNS (127.0.0.11) stops working completely and you lose port isolation. I covered the full trade-offs in Docker port mapping, EXPOSE, and host networking explained.

3) Quick debugging commands

When name resolution or routing looks broken, these three commands cover most checks (and for deeper packet captures, see debugging Docker networks with netshoot):

1
2
3
4
5
6
7
8
# 1. Inspect the subnet and every connected container's IP/MAC
docker network inspect backend-net

# 2. Attach a running container to another bridge without restarting it
docker network connect frontend-net app-server

# 3. Verify DNS lookup from inside the container
docker exec -it app-server getent hosts postgres-db

7. Quick checklist before deploying

  1. Never use docker0 for multi-container apps: Always define a user-defined bridge in compose.yaml or via docker network create.
  2. Never hardcode container IPs: Point JDBC, Redis, and HTTP clients at Docker service names or network aliases.
  3. Cap JVM DNS TTL: Set -Dnetworkaddress.cache.ttl=5 so restarted dependencies don’t leave stale IPs cached in your app.
  4. Verify 127.0.0.11 inside the container: If /etc/resolv.conf doesn’t list 127.0.0.11, the container is either on docker0 or running in host network mode.
This post is licensed under CC BY 4.0 by the author.