Knowledge
Exercise: multi-service Compose and network isolation
Estimated time: 45 minutes.
On a Linux host with Docker installed (for example Ubuntu 24.04), write a compose.yaml file that runs three web services at the same time, deliberately leaving one service outside the shared network, and then measure the isolation with ping.
Acceptance criteria: (1) docker compose config is clean and up -d brings up three containers in the Up state on ports 8080/8081/8082; (2) docker inspect shows web01 and web02 in the same IP range as app-network, while web03 is in a different range; (3) a ping from inside web01 to web02 succeeds and to web03 fails; (4) you can read the com.docker.compose.project.config_files label and it points to the exact file that created the container; (5) you can show that stop differs from down, and that neither deletes the bind mount directory on the host.
Step 1 — Confirm you are using Compose v2 and free up the ports
Check Compose with docker compose version — the v2 command is written without a hyphen. Before you start, make sure ports 8080, 8081 and 8082 are free so they do not conflict with an earlier exercise.
docker compose version
docker ps --format "table {{.Names}}\t{{.Ports}}"
ss -lntp | grep -E ":(8080|8081|8082) " || echo "all three ports are free"
Note: docker-compose (with a hyphen) is v1, which is no longer supported — copying old documentation verbatim means typing the wrong command (Docker: migrate to Compose v2).
Step 2 — Create the project directory and the web content
Create a dedicated directory for the project and the web page of each service — one directory per Compose project, because every relative path in the compose file is resolved against the location of that file itself.
mkdir -p ~/docker-lab/compose-multi/html
cd ~/docker-lab/compose-multi
cat > html/index.html <<'EOF'
<!doctype html>
<html lang="vi">
<head><meta charset="utf-8"><title>Compose Multi Web</title></head>
<body><h1>Web 01 - content mounted from the host</h1></body>
</html>
EOF
ls -la html
Note: The heredoc is written with single quotes, so the content is written verbatim and the shell does not expand variables in it.
Step 3 — Write compose.yaml with three services
Declare three web services in one file — each service gets its own host port because a port can be held by only one process. web01 and web02 declare networks: [app-network]; web03 does NOT — this is exactly the point we will measure in a later step.
cat > compose.yaml <<'EOF'
services:
web01:
image: nginx:alpine
container_name: compose-web01
restart: unless-stopped
ports:
- "0.0.0.0:8080:80"
volumes:
- ./html:/usr/share/nginx/html:ro
networks:
- app-network
web02:
image: nginx:alpine
container_name: compose-web02
restart: unless-stopped
ports:
- "0.0.0.0:8081:80"
networks:
- app-network
web03:
image: nginx:alpine
container_name: compose-web03
restart: unless-stopped
ports:
- "0.0.0.0:8082:80"
networks:
app-network:
driver: bridge
EOF
cat compose.yaml
Note: The left-hand side of ports is 0.0.0.0 so other machines on the network can connect. With 127.0.0.1, only the host itself can access it (Docker: published ports).
Step 4 — Validate before creating any container
Run docker compose config to check the file before creating containers. YAML is indented with spaces, never tabs. The config command prints the merged configuration and reports errors right at the offending line.
docker compose config
Note: If you skip this step, an indentation error shows up in the middle of up, when some services have been created and others have not (docker compose config).
Step 5 — Start up and check that all three web servers respond
Start all three services, then curl each port from the host. The isolation lives in the network between containers, not in the entry path from the host, so all three ports must respond to curl.
docker compose up -d
docker compose ps
docker ps
curl -s http://127.0.0.1:8080/ | head -3
curl -s -o /dev/null -w "8081 -> %{http_code}\n" http://127.0.0.1:8081/
curl -s -o /dev/null -w "8082 -> %{http_code}\n" http://127.0.0.1:8082/
Note: Only web01 has a bind mount, so it returns the content you wrote; web02 and web03 return Nginx's default page.
Step 6 — Read the real IP of each container
Read each container's IP with docker inspect — these are the figures you will compare against in the ping step. web01 and web02 must be in the same range; web03 is in a different range because it belongs to the project's default network.
docker network ls
docker inspect compose-web01 --format "{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}"
docker inspect compose-web02 --format "{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}"
docker inspect compose-web03 --format "{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}"
docker network inspect compose-multi_app-network --format "{{range .Containers}}{{.Name}} {{end}}"
Note: Compose names a network in the form directory-name_network-name. The last command lists the containers in that network — you should see only web01 and web02 (Compose: networking).
Step 7 — Measure the isolation with ping
Ping from inside web01 to web02, then to web03 — this is the most important acceptance step of the exercise. Within the same network, pinging by service NAME also works because Compose provides internal DNS. To web03, neither the name nor the IP is reachable.
docker exec compose-web01 ping -c 3 compose-web02
docker exec compose-web01 ping -c 3 <WEB03_IP>
docker exec compose-web01 ping -c 3 compose-web03
Note: The second and third commands must fail — 100% packet loss or a bad address. That is the correct result for this exercise, not an incident (Docker: bridge network).
Step 8 — Trace a container back to its compose file
Read the com.docker.compose.* labels to find out which file a container was created from. Compose keeps no state anywhere other than the labels attached to containers. This is the lookup path when a host runs dozens of projects.
docker inspect compose-web01 --format '{{json .Config.Labels}}'
docker inspect compose-web01 --format '{{index .Config.Labels "com.docker.compose.project.config_files"}}'
docker inspect compose-web01 --format '{{index .Config.Labels "com.docker.compose.project"}}'
Note: The config_files label records the absolute path of the file that created the container (docker inspect).
Step 9 — Show that you must be in the right directory
Try typing a Compose command from outside the project directory. Compose looks for the configuration file in the current directory. If you move away and type a project command, it fails unless you point to the file with the -f flag.
cd ..
docker compose ps
docker compose -f ~/docker-lab/compose-multi/compose.yaml ps
cd ~/docker-lab/compose-multi
Note: The second command reports no configuration file provided: not found; the third one works (docker compose CLI). When working by hand, the recommendation is still to cd into the project directory.
Step 10 — Tell stop apart from down, then clean up the lab
Compare the container state after stop and after down, then clean up the lab. stop keeps the containers in the stopped state; down deletes the project's containers and network. Neither touches the bind mount directory on the host.
docker compose stop
docker compose ps --all
docker compose start
docker compose ps
docker compose down
docker ps -a
docker network ls | grep compose-multi || echo "the project network has been removed"
ls -l html
Note: After down, the containers and the network are gone but the html directory is intact — bind mount data is owned by the host (docker compose down).
Illustration 1 — docker compose version
The output of docker compose version confirms you are using the v2 plugin.
docker compose version
Result:
Docker Compose version v2.39.1
Note: The v2 command is written without a hyphen. If the machine returns a 1.x version, you are running an unsupported release.
Illustration 2 — docker compose config
docker compose config prints the merged configuration before any container is created.
docker compose config
Result:
name: compose-multi
services:
web01:
container_name: compose-web01
image: nginx:alpine
networks:
app-network: null
ports:
- mode: ingress
host_ip: 0.0.0.0
target: 80
published: "8080"
protocol: tcp
restart: unless-stopped
...
Note: The line name: compose-multi is the project name, derived from the directory name — it is also the prefix of the network name.
Illustration 3 — docker compose ps
docker compose ps shows three services in the Up state on three different ports.
docker compose up -d
docker compose ps
Result:
NAME IMAGE SERVICE STATUS PORTS
compose-web01 nginx:alpine web01 Up 12 seconds 0.0.0.0:8080->80/tcp
compose-web02 nginx:alpine web02 Up 12 seconds 0.0.0.0:8081->80/tcp
compose-web03 nginx:alpine web03 Up 12 seconds 0.0.0.0:8082->80/tcp
Note: If a host port collides, the second service does not come up, with an address already in use error in the output of up.
Illustration 4 — The IPs of the three containers
The IPs of the three containers fall into two different ranges.
docker inspect compose-web01 --format "{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}"
docker inspect compose-web02 --format "{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}"
docker inspect compose-web03 --format "{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}"
Result:
172.21.0.2
172.21.0.3
172.22.0.2
Note: web01 and web02 are in 172.21.0.0/16 of app-network; web03 is in 172.22.0.0/16 — the project's default network. The exact ranges on your machine may differ; what you need to observe is that the two ranges are different.
Illustration 5 — Ping within the same network
A ping from web01 to web02 succeeds, and the service name resolves.
docker exec compose-web01 ping -c 3 compose-web02
Result:
PING compose-web02 (172.21.0.3): 56 data bytes
64 bytes from 172.21.0.3: seq=0 ttl=64 time=0.089 ms
64 bytes from 172.21.0.3: seq=1 ttl=64 time=0.104 ms
64 bytes from 172.21.0.3: seq=2 ttl=64 time=0.098 ms
--- compose-web02 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
Note: You do not need to know the IP: the network's internal DNS resolves container names and service names.
Illustration 6 — Ping to a container outside the network
A ping from web01 to web03 fails. This is the correct result for the exercise.
docker exec compose-web01 ping -c 3 172.22.0.2
docker exec compose-web01 ping -c 3 compose-web03
Result:
PING 172.22.0.2 (172.22.0.2): 56 data bytes
--- 172.22.0.2 ping statistics ---
3 packets transmitted, 0 packets received, 100% packet loss
ping: bad address 'compose-web03'
Note: By IP, every packet is lost; by name, it cannot be resolved, because the internal DNS only works within the same network.
Illustration 7 — The Compose labels attached to a container
The Compose labels on a container give you the path back to the compose file.
docker inspect compose-web01 --format '{{json .Config.Labels}}'
Result:
{
"com.docker.compose.project": "compose-multi",
"com.docker.compose.service": "web01",
"com.docker.compose.working_dir": "/root/docker-lab/compose-multi",
"com.docker.compose.project.config_files": "/root/docker-lab/compose-multi/compose.yaml",
"com.docker.compose.version": "2.39.1"
}
Note: A container without this group of labels was created by a standalone docker run command and does not belong to any Compose project.
Illustration 8 — stop differs from down
The container state after stop and after down is different.
docker compose stop
docker compose ps --all
docker compose down
docker ps -a
Result:
NAME IMAGE SERVICE STATUS PORTS
compose-web01 nginx:alpine web01 Exited (0) 3 seconds ago
compose-web02 nginx:alpine web02 Exited (0) 3 seconds ago
compose-web03 nginx:alpine web03 Exited (0) 2 seconds ago
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Note: After stop, the containers remain in the Exited state; after down, the list is completely empty and the project's network is deleted too.