User-defined networks and container DNS

Intermediate · 1 min

Create your own bridge networks so containers find each other by name through Docker's DNS at 127.0.0.11, stay isolated from other stacks, and rewire live.

Before thisThe default bridge network

NetworkChuck · 5 min · from 10:44

User-defined networks and container DNS

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by NetworkChuckReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • docker network create makes a new bridge with its own subnet. Containers on it reach each other by container name, network alias or Compose service name.
  • On user-defined networks, containers use Docker's embedded DNS server at 127.0.0.11, which answers for container names and forwards everything else to the host's DNS servers.
  • Containers on the same user-defined network can reach each other on every port. Containers on different networks cannot, except through published ports.
  • A container can join several networks, and docker network connect and disconnect change that while it runs. That is how a proxy sits between a public and a private network.
  • --internal creates a network with no route out, for back-end services that should never reach the internet.
  • Docker's default address pools give 31 networks before they run out. Narrow the pool size in daemon.json if you need more.

Names that keep working

Your API needs to reach its database. On the default bridge you would have to look up the database container’s IP and pass it in, and the IP changes every time the container is recreated. What you want is to write postgres://db:5432 and have db always mean the database container. User-defined networks give you that, and isolation between stacks as a bonus. The bridge driver page puts it as “user-defined bridge networks are superior to the default bridge network.”

Creating and using one

docker network create notes-net
docker run -d --name db  --network notes-net -e POSTGRES_PASSWORD=dev postgres:18
docker run -d --name api --network notes-net -e DATABASE_URL=postgres://postgres:dev@db:5432/postgres notes:1.4.2

api connects to db by name. Nothing is published, and nothing needs to be: containers on the same user-defined network can reach each other on every port. Only traffic from outside the network needs a published port.

Docker Compose does this automatically. Each project gets a network named <project>_default, and every service joins it under its service name. That is why db works in a Compose file without any network configuration.

How names resolve

Containers on a user-defined network get nameserver 127.0.0.11 in their /etc/resolv.conf. That address is Docker’s embedded DNS server, reachable from inside every container on such a network. From the networking overview:

  • It answers for the names of containers on the networks the asking container is attached to: the container name, any --network-alias, and in Compose the service name.
  • It forwards every other lookup to the DNS servers configured on the host, so example.com still works.
  • It queries those upstream servers in order and stops at the first successful or NXDOMAIN answer.

If an application insists on a DNS server address, the docs say to use 127.0.0.11. There is no IPv6 equivalent, and the IPv4 address works even in IPv6-only containers.

Aliases give a container extra names on one network. --network-alias search on two containers gives both the name search, and the embedded DNS returns both addresses. The name resolution lesson covers how the resolver inside the container uses what it gets back.

Isolation between networks

Each user-defined network is a separate Linux bridge with its own subnet, and Docker installs firewall rules so that, in the bridge driver’s words, “containers connected to different bridge networks can only communicate with each other using published ports.” Two Compose projects on one host cannot see each other’s databases. That is a real improvement over the default bridge, where every container sees every other.

Containers can belong to more than one network, and the attachment can change while they run:

docker network create public
docker network create --internal private
docker run -d --name app   --network private notes:1.4.2
docker run -d --name proxy --network public -p 443:443 nginx:1.29
docker network connect private proxy

proxy is on both networks and reaches app by name. app is only on private, which is --internal: per the network create reference, it has no default route and its traffic to other networks is dropped, though it can still talk to the gateway address on the host. The app cannot call out to the internet even if compromised, and only the proxy’s port is published. When a container is on several networks, the gw-priority option on --network chooses which one provides its default route.

docker network disconnect removes a running container from a network just as easily, which is useful for cutting a misbehaving service off without stopping it.

Subnets and running out

Without --subnet, Docker picks one from its default address pools. The pools in the networking overview are three /16s, three /14s split into /16s, and 192.168.0.0/16 split into /20s. I counted what that yields with Python’s ipaddress module: 31 networks in total. A CI host that creates a network per job and never prunes them hits that limit and fails with an error about the pools being fully subnetted.

Two fixes. Run docker network prune regularly. Or hand out smaller subnets:

{
  "default-address-pools": [{ "base": "10.200.0.0/16", "size": 24 }]
}

That gives 256 networks of 254 addresses each, plenty for most stacks. Choosing a base that does not clash with your office or cloud networks avoids the classic “VPN breaks when Docker starts” problem. For an explicit range, docker network create --subnet 10.200.5.0/24 --gateway 10.200.5.1 notes-net.

About the video

The embed is a 5-minute chapter, “The second network: The User-defined Bridge” (10:44 to 15:38), of NetworkChuck’s “Docker networking is CRAZY!! (you NEED to learn it)”, August 2022. In it he creates a network, runs containers on it, pings them by name and shows they are isolated from the default bridge. It does not cover the embedded DNS server’s details, --internal or address pools, which the lesson adds. YouTube reports an English original audio track. Christian Lempa’s chapters in deeper cover the same ground with a focus on isolation.

What was run: the 31-network figure was computed with Python’s ipaddress module from the default pools in the networking overview. No network was created. The DNS behaviour, isolation rules and options are from the networking overview, the bridge driver page and the docker network create and docker network connect references, checked 2026-11-28.

Where this fits

Containers now find each other. Next, how traffic from outside gets in, and why it can slip past your firewall: publishing ports and the host firewall.

Check yourself

  1. 01A web container on network app resolves db, but a container on network other cannot resolve or reach db. Is that a bug?

    No. Names resolve only within the networks a container shares, and Docker's rules block traffic between different bridge networks. Connect the second container to app if it really needs db.

  2. 02What is the nameserver line in /etc/resolv.conf of a container on a user-defined network, and what happens to a lookup for example.com?

    nameserver 127.0.0.11, Docker's embedded DNS. It answers container names itself and forwards other names, such as example.com, to the DNS servers configured on the host.

  3. 03How do you put an nginx proxy in front of an app that must not be reachable from other stacks?

    Put the app only on a private network, put nginx on both that network and a public-facing one, and publish only nginx's port. nginx reaches the app by name, and nothing else can.

  4. 04docker network create fails with 'all predefined address pools have been fully subnetted' on a CI host. Why, and what can you do?

    The built-in pools hand out 31 networks, and old networks from previous jobs used them up. Prune unused networks, or configure default-address-pools with smaller subnets, such as size 24.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark