Skip to content

Container DNS and User-Defined Networks

As you saw in the previous lesson, containers on the default bridge can reach each other by IP address but not by name. This is a significant limitation in practice — IP addresses are dynamic and change every time a container is recreated.

Hardcoding IP addresses into application configuration is fragile. You need a way to refer to services by a stable name. That is exactly what user-defined networks provide.

The solution: create a user-defined bridge

Section titled “The solution: create a user-defined bridge”

Use docker network create to create a named bridge network:

Terminal window
docker network create appnet

Docker creates a new bridge (separate from docker0) and starts an embedded DNS resolver scoped to that network. Containers attached to appnet can resolve each other’s names automatically.

Running containers on a user-defined network

Section titled “Running containers on a user-defined network”

Start an nginx container named web on appnet:

Terminal window
docker run -d --name web --network appnet nginx:alpine

Start an Alpine container named client on the same network:

Terminal window
docker run -it --name client --network appnet alpine sh

Inside the client shell, ping web by name:

Terminal window
ping -c 3 web

The ping succeeds. Docker’s embedded DNS resolver translates web to the container’s IP address automatically.

From the host, inspect appnet to see which containers are attached and what IPs they were assigned:

Terminal window
docker network inspect appnet

The output includes a Containers section listing each attached container with its name, IP, and MAC address.

Terminal window
docker stop web client
docker rm web client
docker network rm appnet
# Create a user-defined bridge network
docker network create appnet

# Start an nginx container on appnet
docker run -d --name web --network appnet nginx:alpine

# Start a client container on appnet and ping web by name
docker run --rm --name client --network appnet alpine ping -c 3 web

# Inspect the network to see attached containers
docker network inspect appnet

# Cleanup
docker stop web && docker rm web
docker network rm appnet
What feature do user-defined bridge networks provide that the default bridge does not?
Which command creates a user-defined bridge network named 'appnet'?
You run two containers on the same user-defined bridge network. Container A is named 'api' and container B is named 'db'. How can container A reach container B?
What happens to container name resolution if you move containers back to the default bridge after using a user-defined network?