Docker manages networking - containers can be attached to Docker networks, and containers on the same network can reach each other by IP address.
Docker also runs a DNS server, so containers on the same network can find each other using the container name as the DNS host name.
How this lab works
Read this once - every lab in this track follows the same rules.
Where you type the commands. Open a terminal and go to the folder where you cloned this repository. On Windows use PowerShell (search for it in the Start menu), on macOS or Linux use Terminal. Every path in this lab is written from the repository root, so stay there unless a step says otherwise.
Lines starting with > are explanations, not commands. Don't paste them.
Only the text inside grey code boxes gets typed.
The 📋 sign means it's your turn. When you see it, try the step yourself first. Underneath it there is a collapsed "Not sure how?" arrow with the answer - open it after you have tried, not before. The labs are written this way on purpose: you remember what you worked out, not what you pasted.
If a command fails, that is normal. Some steps fail deliberately to show you what the error looks like. Read the sentence above the command before you assume something is broken.
When you get stuck, in this order:
- Re-read the paragraph above the command - the answer is usually there.
- Open the "Not sure how?" arrow.
- Open
hints.mdfor a nudge. - Open
solution.mdfor the full answer. Using it is fine; copying it without reading is not.
When you finish, run the Cleanup section at the bottom. Labs share ports and container names, so leftovers from one lab break the next one.
Before you start
Takes about 40 minutes.
Docker Desktop must be running. Check it:
docker version
A Server: section in the output means you are ready. If you get an error
instead, start Docker Desktop and wait until the whale icon stops animating.
Run every command from the root of this repository - the paths in this lab are written relative to it.
Reference
- Docker networking overview
- Container network options in Compose: DNS, hostname aliases, IP addresses
- Network configuration in Compose
CLI overview
Networks are separate objects, which you can manage from the Docker CLI:
docker network --help
docker network ls
Docker uses a plugin model for networking, so containers can be modelled to fit with different physical network architectures.
Container networking
Docker networks are first-class citizens. We've been using them already, and Docker Compose creates them when we deploy an app:
- compose.yml - defines a network with no special configuration
📋 Run the app, list all networks and print the details of the new application network.
Not sure how?
docker compose -f labs/networking/compose.yml up -d
# networks are a top-level object in the Docker CLI:
docker network ls
# compose adds the project name as a prefix to the network name:
docker network inspect networking_app-net
You'll see a lot of details about the network - including containers which are attached to it. The API and web containers are there, with IP addresses in the same network range so they should are able to connect.
The web container image has some networking tools installed, which we can use for troubleshooting:
nslookupto query the DNS server and get an IP addresspingto test the network connection to a remote machinewgetto make HTTP requests
Use them to check the network setup:
# run a DNS lookup for the API service name:
docker exec networking-rng-web-1 nslookup rng-api
# ping the IP address of the API container :
docker exec networking-rng-web-1 ping -c3 <rng-api-ip>
# try calling the API inside the web container:
docker exec networking-rng-web-1 wget -qO- rng-api/rng
The Docker DNS server returns the IP address for container names and network aliases. The web container can access the API container on the standard HTTP port 80.
Publishing ports
Containers connected to the same network can access each other by IP address or name. There's no restriction to the traffic - any ports can be used between containers, they don't need to be published.
Traffic from outside the container network is restricted, and the only way to send traffic into a container is by publishing ports. Docker listens on the published port and sends the traffic into the target container port.
Try running a simple web server, publishing to a specific port:
docker run -d -p 8080:80 ghcr.io/yasinoz/sixeyed/whoami:24.07
curl localhost:8080
The -p flag - lowercase p - publishes a port, so here Docker listens on port 8080 and sends traffic into the container on port 80.
Ports are single-use resources, only one process can listen on them. If you repeat the command you'll get a port is already allocated failure message.
📋 Run some more containers from the same image, using different ports (8081, 8082 and 8083 should be free).
Not sure how?
docker run -d -p 8081:80 ghcr.io/yasinoz/sixeyed/whoami:24.07
docker run -d -p 8082:80 ghcr.io/yasinoz/sixeyed/whoami:24.07
docker run -d -p 8083:80 ghcr.io/yasinoz/sixeyed/whoami:24.07
Port mapping lets you listen on any free port on your machine, even though the containers are all listening on port 80 internally. Check the running containers:
docker ps
They each publish to different ports, so you can see the response from each container:
curl localhost:8081
curl localhost:8082
curl localhost:8083
Customizing container networks
Docker sets the IP address, DNS server and other network options for a container - and you can configure those too.
There's a script in this folder which we can mount in a container to print the network settings - network-info.sh. Save the path to the script in a variable to make it easier to mount in a container:
# on macOS/Linux:
scriptsPath="${PWD}/labs/networking/scripts"
chmod +x "${PWD}/labs/networking/scripts/network-info.sh"
# OR with PowerShell:
$scriptsPath="${PWD}/labs/networking/scripts"
Now run a basic Alpine container to execute the script:
docker run -v ${scriptsPath}:/scripts alpine sh /scripts/network-info.sh
These network settings are all built by Docker
📋 Repeat the run command for a new container with extra settings to specify a DNS server of 1.1.1.1 and hostname of alpine1.
Not sure how?
docker run --dns 1.1.1.1 --hostname alpine1 -v ${scriptsPath}:/scripts alpine sh /scripts/network-info.sh
This is useful for apps which need a particular configuration.
You can also set an IP address - but first you need to create a network with a specific IP address range:
docker network create --subnet=10.10.0.0/16 courselabs
docker run --network courselabs --ip 10.10.0.100 -v ${scriptsPath}:/scripts alpine sh /scripts/network-info.sh
Unless you configure them, IP addresses are set at random by Docker - you'll need to use a custom subnet range if Docker's IP address collides with your network or VPN.
You can configure custom networking in Docker Compose too:
- compose-network.yml - the random number app using custom IP addresses with a named network
📋 Run the app with Compose and test it. The API container also mounts the network test script - run that to check the IP address.
Not sure how?
# update the app:
docker compose -f labs/networking/compose-network.yml up -d
# try it out at http://localhost:8090
# print the container's network details:
docker exec networking-rng-api-1 sh /scripts/network-info.sh
Check the logs of the web container and you'll see it's using the custom IP address for the API container:
docker logs networking-rng-web-1
You don't often need to customize networking like this - containers are supposed to run in a dynamic environment. But this is very useful for moving older apps to containers, if they have fixed expectations about the environment.
Lab
Docker's DNS server returns the IP address for a container, using the name as the domain name.
When you run apps with Compose it builds container names like rng_rng-api_1 - so how do containers discover each other using the Compose service name?
Scale up the random number API to run multiple containers and check that the website load-balances requests across them all; then look at the containers' network setup to see how it works.
If something goes wrong
Cannot connect to the Docker daemon - Docker Desktop is not running.
Start it and wait until the whale icon stops animating.
port is already allocated - a container from an earlier lab is still
holding that port. See what is running with docker ps, then remove the old
container with docker rm -f <name>.
The container name "/..." is already in use - same cause, same fix:
docker rm -f <name>, then run the command again.
unable to prepare context: path ... not found on a build - you are in the
wrong folder. Every command in this lab assumes you are at the root of this
repository.
A docker run that prints usage text instead of working - some commands in
this lab are meant to fail, to show you what a wrong argument looks like. Read
the text around the command before assuming it is broken.
Still stuck? The Cleanup section below resets everything, so you can start the lab again from a clean machine.
Cleanup
Clean up by removing this lab's containers:
docker compose -f labs/networking/compose.yml down
docker compose -f labs/networking/compose-network.yml down
This removes only the containers this lab started. If you want a clean slate,
docker pswill show anything still running.