Knowledge

User-defined bridge networks in Docker: containers calling each other by name through built-in DNS

Updated 2026-11-21 · Present

  • On a user-defined bridge network (docker network create), containers can resolve each other by name or alias; on the default network they cannot.
  • A user-defined network isolates better: only containers on the same network see each other, and you can attach or detach containers while they run.
  • Applications should call http://web instead of an IP address, because a container's IP can change each time it is recreated.

Goal

You will create a dedicated bridge network, run two containers in it and let them call each other by name. You will also see clearly why you should use a user-defined network rather than the default one for applications made of several containers.

Preparation

Why the default network is not enough

According to Docker's documentation, a user-defined bridge network has these advantages over the default one:

The first point is the one you use every day: a container's IP address is assigned by Docker and can change when the container is recreated, whereas a name is chosen by you and stays stable.

Steps

Steps

1. Create the network

docker network create app-net
docker network ls

If you do not specify a driver, Docker uses the bridge driver. To choose the address range yourself, add --subnet 192.0.2.0/24 (an example range is used here; on a real machine pick a private range that does not clash with your network).

2. Run a container in that network

docker run -d --name web --network app-net nginx

The container name web is its DNS name in the app-net network. Note, as the documentation points out: the automatic resolution only recognizes the names you set (and aliases), not the random names Docker generates.

3. Call it by name from another container

Run a temporary container on the same network to call web. The --rm flag removes the temporary container when it ends:

docker run --rm --network app-net busybox wget -qO- http://web

You will get back the nginx welcome page. You do not need to know the IP of web, and you do not need to publish a port: traffic between containers on the same network goes straight over the virtual bridge.

4. Network aliases

A container can get an extra name in a network with --network-alias:

docker run -d --name web2 --network app-net --network-alias frontend nginx
docker run --rm --network app-net busybox wget -qO- http://frontend

Aliases help when an application is preconfigured with a name such as db or cache while the real container name is different.

5. Attach another network while running

Suppose a container must talk to two different applications. Create a second network and attach it:

docker network create other-net
docker network connect other-net web
docker network inspect other-net

Now web is on both networks, and containers on other-net can call web by name. To detach: docker network disconnect other-net web.

Verification

Clean up (these commands delete containers and networks; data inside the containers will be lost):

docker rm -f web web2
docker network rm app-net other-net

docker network rm only works when no container is using that network anymore.

Common errors

Summary

Treat a user-defined network as the good default for every multi-container application: one network per application, containers calling each other by name, and publish only the ports of the service that must serve users. When should you drop this virtual network layer altogether? The next article covers the host network.

References