Challenge
DifficultyEasy
Learner activity52 solved of 69 attempts

Connect Containers with a User-Defined Docker Bridge Network

Containers on the default bridge network can reach each other only by IP address. Learn how to create a user-defined bridge network, connect containers by name, and inspect the DNS magic that makes it work.
Important

This challenge focuses on Docker Engine running directly on a Linux server, which is the setup most production Docker installations use. Docker Desktop runs Docker Engine inside a separate VM, so the network interfaces Docker creates are not visible on your computer directly.

Docker creates the default bridge network on its own, and it also lets you create bridge networks of your own. Containers on such user-defined networks can reach each other by name, which makes these networks the recommended way to connect the containers of an application. In this challenge, you will create a user-defined bridge network, start containers on it, and compare it with the default bridge.

A user-defined bridge network next to the default bridge network.

The containers on the diagram may differ from the ones in your playground.

1. Create a network

Create a bridge network called app-net. Let Docker choose the subnet and gateway address for it.

Hint 1

The docker network create command takes the name of the new network:

docker network create NETWORK_NAME

Without extra options, it creates a bridge network and picks an available subnet. Use docker network ls to check that your new network appears in the list.

2. Find the bridge interface

Like the default bridge network, app-net has a virtual bridge interface on the Docker host. This interface holds the network's gateway address.

Find the network interface on the Docker host that belongs to app-net.

Hint 2

Start by inspecting the Docker network:

docker network inspect app-net

Find the Subnet and Gateway values in the IPAM section. Then list the host's network interfaces and their addresses:

ip -br addr

Which interface has the gateway address of app-net? The docker0 interface belongs to the default bridge network, so look for a different one.

3. Reach a container by name

Start an Nginx container named web-01 on app-net, then send an HTTP request to http://web-01 from a BusyBox container on the same network.

Leave the web server running when you're done. You don't need to publish its port: the client will connect to it from inside the same Docker network.

Hint 3

Use the --network flag of docker run to attach a container to app-net. The --name flag gives it a name that other containers on this network can use as a hostname:

docker run -d --name CONTAINER_NAME --network NETWORK_NAME IMAGE

Use nginx:alpine for the server image. The -d flag leaves the server running in the background.

Hint 4

BusyBox includes wget, which can send an HTTP request and print the response:

wget -qO- http://HOSTNAME

Run this command inside a BusyBox container attached to app-net:

docker run --rm --network app-net busybox COMMAND [ARGUMENTS...]

Use web-01 as the hostname. The --rm flag removes the client container after its command exits.

If the request succeeds, you'll see the Nginx welcome page in the terminal. You can also run docker logs web-01 to check that the server received it.

4. Find the DNS server

The client reached web-01 without knowing its IP address. Docker provides this name resolution through an embedded DNS server.

Find the IP address of the DNS server used by containers on app-net.

Hint 5

Each container has its own /etc/resolv.conf file. The nameserver lines tell you where it sends DNS queries. Read this file inside a container on app-net:

docker run --rm --network app-net busybox cat /etc/resolv.conf

Use the address from the nameserver line, rather than any addresses in the comments. Reading /etc/resolv.conf on the Docker host would show the host's DNS settings instead.

Hint 6

You can also ask BusyBox to resolve the name web-01 directly:

docker run --rm --network app-net busybox nslookup web-01

The output contains two kinds of addresses: the DNS server that answered the query and the container address it returned. Look for the Server line to answer this step.

5. Compare with the default bridge

Now try the same name lookup from a container on the default bridge network. This network doesn't provide automatic name resolution between containers.

Start a BusyBox container on the default bridge, try to resolve web-01, and find the IP address of the DNS server it queries. The lookup is expected to fail, but the output should still show which DNS server received the query.

Hint 7

Repeat the nslookup command from the previous step, but omit --network app-net. Without a --network flag, Docker attaches the new container to the default bridge.

Look at the Server line again. You can also check the container's configured DNS servers with:

docker run --rm busybox cat /etc/resolv.conf

Compare its nameserver lines with those of a container on app-net.

Problem
Solution
Start
Tasks