Docker Compose: one file, many containers

  1. Quick summary
  2. Step 1 — Confirm you are using Compose v2
  3. Step 2 — Create the project directory and the web content
  4. Step 3 — Write compose.yaml for one service
  5. Step 4 — Validate the configuration before creating containers
  6. Step 5 — Start, check, and access from another machine
  7. Step 6 — Scale to three services and split one service onto a separate network
  8. Step 7 — Measure network isolation with ping
  9. Step 8 — The project lifecycle and logs at two levels
  10. Step 9 — Trace a container back to its compose file and clean up the lab
  11. Common misconceptions
  12. Hands-on 1 — One web service with Compose
  13. Hands-on 2 — Three services and a network isolation test
  14. Hands-on 3 — Lifecycle, two-level logs and tracing the compose file
  15. Review questions
  16. Flashcards

Estimated time: 50 minutes.

Quick summary

Step 1 — Confirm you are using Compose v2

docker compose version shows the plugin version you have. v2 commands are written without a hyphen; docker-compose in old documentation is v1, which is no longer supported (Docker: migrate to Compose v2).

When it breaks: Typing commands from old documentation on a machine that only has v2 gives a command that does not exist; conversely, if a leftover v1 remains on the machine, its behavior differs from the current documentation.

docker compose version
docker version --format "{{.Server.Version}}"
docker ps

Observe with:

docker compose version ; docker version

Note: Before the hands-on, clean up or stop any containers occupying ports 8080-8082 to avoid conflicts between the old exercise and the new one.

Step 2 — Create the project directory and the web content

Each Compose project should have its own directory: the configuration file and every relative path inside it are resolved relative to the file's location (docker compose CLI).

The html directory holds the page source and will be mounted into the container in a later step.

When it breaks: If you scatter the configuration file among other directories, relative paths point to the wrong place and the bind mount attaches an empty directory.

mkdir -p ~/docker-lab/compose-lab/html
cd ~/docker-lab/compose-lab

cat > html/index.html <<'EOF'
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Docker Compose Lab</title>
</head>
<body>
  <h1>An application managed with Docker Compose</h1>
</body>
</html>
EOF

ls -la html

Observe with:

pwd ; ls -la ; cat html/index.html

Note: The heredoc is written as <<'EOF' with single quotes, so the content is written verbatim, with no variable expansion by the shell.

Step 3 — Write compose.yaml for one service

A minimal web service consists of an image, a container name, a restart policy, a published port, a read-only bind mount of the web content, and its own network (Compose file reference).

Reading this file tells you the whole deployment intent.

When it breaks: Leave out the networks key and the service goes into the project's default network — it still runs, but you lose control over who sees whom in the later steps.

cat > compose.yaml <<'EOF'
services:
  web:
    image: nginx:alpine
    container_name: compose-web
    restart: unless-stopped
    ports:
      - "0.0.0.0:8080:80"
    volumes:
      - ./html:/usr/share/nginx/html:ro
    networks:
      - app-network

networks:
  app-network:
    driver: bridge
EOF

cat compose.yaml

Observe with:

cat compose.yaml

Note: The left side of ports is 0.0.0.0 so other machines can reach it.

With 127.0.0.1 only the host itself can access it; use a specific IP when the machine has several network interfaces and you only want to expose on one network.

Step 4 — Validate the configuration before creating containers

docker compose config merges the configuration and prints the final version; a syntax error is reported right at the offending line (docker compose config).

YAML is indented with spaces, not tabs.

When it breaks: An indentation error found in the middle of up leaves some services created and some not — cleaning up a half-finished state costs more time than checking beforehand.

docker compose config
ss -lntp | grep -E ":8080 " || echo "port 8080 is free"

Observe with:

docker compose config ; ss -lntp | grep 8080

Symptoms when it breaks:

How to fix:

  1. Replace every tab in the file with spaces and validate again

sed -i "s/\t/  /g" compose.yaml && docker compose config
  1. Find and clean up the process or container holding the port

ss -lntp | grep :8080 ; docker ps --format "table {{.Names}}\t{{.Ports}}"

Step 5 — Start, check, and access from another machine

up -d creates the network, creates the containers and runs them in the background. compose ps shows the status from the project's point of view;

docker ps shows it from the whole host's point of view (docker compose up).

When it breaks: Running curl against localhost on the server itself will not reveal the case where the service is not exposed on the external network interface.

docker compose up -d
docker compose ps
docker ps
curl -s http://127.0.0.1:8080/
ip -4 addr show scope global | grep inet

Observe with:

docker compose ps ; docker ps ; curl from another machine using the host's IP

Note: Take the host's IP from the last line and open it in a browser on another machine in the same network — this is the real test of the port publishing.

Step 6 — Scale to three services and split one service onto a separate network

The real-world problem is running many applications at once:

each service gets its own host port because a port can be held by only one process. web03 is deliberately left outside app-network so the next step can measure the isolation (Compose:networking).

When it breaks: If two services use the same host port, the second service cannot come up, and the error message is mixed into the output of up.

docker compose down

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

docker compose config
docker compose up -d
docker compose ps

Observe with:

docker compose config ; docker compose ps ; curl 8080/8081/8082 in turn

Note: web02 and web03 do not need a bind mount because this exercise only measures the network. web03 does not declare networks, so it goes into the project's default network, which differs from app-network.

Step 7 — Measure network isolation with ping

Read the real IP of each container with docker inspect, then ping from inside one container to the other two.

On the same network it works; on a different network it does not (Docker: bridge network).

When it breaks: Reasoning "same file, so they probably see each other" without measuring leads to a wrong design: either services cannot connect, or every service sees every other one unintentionally.

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 exec compose-web01 ping -c 3 compose-web02
docker exec compose-web01 ping -c 3 <WEB03_IP>

docker network inspect compose-lab_app-network --format "{{range .Containers}}{{.Name}} {{end}}"

Observe with:

docker network ls ; docker network inspect ; docker inspect reading NetworkSettings

Symptoms when it breaks:

Note: Inside app-network, pinging by service NAME also works because Compose provides internal DNS. Towards web03, neither the name nor the IP gets through — that is the proof of isolation.

Step 8 — The project lifecycle and logs at two levels

stop keeps the containers in the stopped state, start brings back the very same containers, and down removes the project's containers and networks (docker compose down).

Logs come at two levels: merged for the whole project, or targeting one container.

When it breaks: Edit the compose file and then run start, and the change does not take effect, because the new configuration is only re-read on up.

docker compose logs --tail 20
docker compose logs --tail 20 web01
docker container logs --tail 20 compose-web02

docker compose stop
docker compose ps --all
docker compose start
docker compose ps

curl -s http://127.0.0.1:8080/does-not-exist
docker compose logs --tail 5 web01

Observe with:

docker compose ps --all after each command ; docker compose logs vs docker container logs

Note: Requesting a path that does not exist and then looking at the log shows a 404 line — this is a pattern to compare against when you move on to the centralized log-collection lab.

Step 9 — Trace a container back to its compose file and clean up the lab

Compose keeps state nowhere other than the com.docker.compose.* labels attached to the containers.

The config_files label records the path of the file that produced that container (docker inspect).

When it breaks: On a host with many projects, without a lookup path it is easy to run a cleanup command against another project — down removes containers without asking again.

docker inspect compose-web01 --format '{{json .Config.Labels}}'
docker inspect compose-web01 --format '{{index .Config.Labels "com.docker.compose.project.config_files"}}'

cd ..
docker compose ps
docker compose -f ~/docker-lab/compose-lab/compose.yaml ps

cd ~/docker-lab/compose-lab
docker compose down
docker ps -a
ls -l html

Observe with:

docker inspect reading .Config.Labels ; try running a compose command from a directory outside the project

Symptoms when it breaks:

How to fix:

  1. cd into the directory containing compose.yaml, or point to it with the -f flag

docker compose -f /path/to/compose.yaml ps

Script it: Run it when taking over an unfamiliar server to rebuild the project map before touching anything.

#!/bin/bash
# List every container on the host together with the compose file that produced it
for c in $(docker ps -aq); do
  name=$(docker inspect "$c" --format '{{.Name}}' | tr -d /)
  file=$(docker inspect "$c" --format '{{index .Config.Labels "com.docker.compose.project.config_files"}}')
  printf "%-28s %s\n" "$name" "${file:-<not compose>}"
done

Note: After down, the project's containers and networks are removed but the html directory on the host is intact — bind mount data is not part of the container lifecycle.

Common misconceptions

Right: Two tools with two different goals: a Dockerfile produces an image; a compose file describes how to run containers from existing images. With no image, there is nothing to run.

A Dockerfile is a build recipe: each instruction produces a layer, and the end result is an image (Docker: Dockerfile reference).

A compose file is a description of the desired state of a group of containers on one host: which image, which port, what mounts, which network (Compose file reference).

The source of confusion is that Compose CAN build: declare the build key in a service and Compose calls the engine to build the image first and then run it.

But that build still needs a Dockerfile — Compose only orchestrates, it does not replace the build recipe.

The consequence of telling them apart correctly: an image is built once and reused in every environment, while the compose file changes per environment (ports, mount paths, variables).

Mixing the two roles leads to rebuilding an image just because a port changed.

Right: Only services on the same network can see each other. A service not declared into a shared network is isolated, even though it is in the same file.

Compose assigns a service to a network according to that service's own networks key; a service that declares nothing goes into the project's default network (Compose: networking).

When the file defines a dedicated network and only two services are attached to it, the third service sits on a different network — two separate address ranges with no route between them.

The typical test: web01 and web02 are attached to app-network, web03 is not. Read the IPs with docker inspect and then ping from inside web01: to web02 it works, to web03 it does not.

The consequence for real design: network isolation is a security tool, not a side effect.

An externally exposed web service and the database should meet only through exactly one network; cramming every service into one shared network opens up lateral movement when one service is compromised.

Right: docker-compose (with a hyphen) is v1, which is no longer supported; docker compose is the v2 plugin of the Docker CLI. Only v2 still accepts the current syntax and features.

Compose v1 was a separate program that is no longer developed or supported; v2 was rewritten as a plugin of the Docker CLI and is therefore invoked as a subcommand:

docker compose (Docker:migrate to Compose v2).

This is a frequent source of errors when following old documentation: the copied command does not work, or it runs an old leftover version on the machine with different behavior.

The file name changed too: the currently recommended name is compose.yaml, while docker-compose.yml is still accepted for compatibility.

Consequence: check the version with docker compose version before you begin, and when reading old documentation, translate the command to v2 instead of typing it verbatim.

Right: down REMOVES the project's containers and networks. The command that stops while keeping the containers is stop.

down tears down what up created:

the project's containers and networks are removed (docker compose down). stop only moves the containers to the stopped state,

and start brings the very same containers back (docker compose CLI).

An important point for both commands: data on the host's bind mounts is NOT deleted.

The web-content directory or data directory lives outside the container, so it is still there after cleanup — which is why down is used freely in the lab.

Consequence: use stop when you need to free a port temporarily and want to come back to exactly the same containers; use down when you want to release the ports, container names and network completely.

After editing the compose file you must run up -d again, because start does not re-read the configuration.

Right: Compose looks for the configuration file in the current directory. Standing in the wrong place makes every project command fail, unless you point to the file with the -f flag.

Compose identifies the project from the configuration file in the working directory,

and every relative path in the file (for example ./html) is resolved relative to the location of that file (docker compose CLI).

Leave the project directory and run docker compose down, and it reports that no configuration was found. The immediate fix is the -f flag pointing at the file path.

The way to look it up once you have lost track is docker inspect reading the label com.docker.compose.project.config_files —

this label records exactly the file that produced the container (docker inspect).

The consequence in operations: a host can contain dozens of Compose projects; the convention of keeping each project in its own directory and always cd-ing into it before acting is the cheapest way to avoid running a command against another project.

Hands-on 1 — One web service with Compose

20 minutes

Build exactly the standard flow: write the file → validate → up → check from another machine.

The validate step is where you save the most time (docker compose config).

docker compose up -d && docker compose ps

Observe: docker compose config ; docker compose ps ; curl using the host's IP from another machine

1. Create ~/docker-lab/compose-lab/html/index.html with a single heading line, then cd into ~/docker-lab/compose-lab.

The project directory has an html directory containing index.html.

2. Write a one-service compose.yaml for web: nginx:alpine, container_name: compose-web, restart: unless-stopped, ports "0.0.0.0:8080:80", bind mount ./html:/usr/share/nginx/html:ro, network app-network.

The file exists and reads back exactly what you wrote.

3. Run docker compose config.

It prints the merged configuration without errors. If there is an indentation error, replace tabs with spaces and redo it.

4. Run docker compose up -d and then docker compose ps and docker ps.

The web service is Up, with port 8080 mapped to 80.

5. Get the host's IP with ip -4 addr show scope global and then open http://<IP>:8080 from another machine.

The page shows exactly the content of the index.html file on the host.

6. Edit html/index.html on the host and reload the page.

The new content appears immediately, with no need to restart the container — the bind mount reads the host directory directly.

Analysis

Observation: One file fully describes a web service, and the web content can be edited without touching the container.

Mechanism: Compose reads the file, creates the network and then creates the container with exactly the flag that corresponds to each key; the bind mount maps the host directory into the container.

Takeaway: The main value is that the configuration lives in a file you can put in Git, not the number of commands you save typing.

Troubleshooting:

Hands-on 2 — Three services and a network isolation test

25 minutes

Prove with measurements that being in the same compose file does not mean being on the same network — the foundation for designing network segmentation for a real system later (Compose: networking).

docker exec compose-web01 ping -c 3 compose-web02

Observe: docker network ls ; docker inspect reading IPAddress ; docker network inspect listing the containers in the network

1. docker compose down and then rewrite compose.yaml with three services web01/web02/web03, publishing 8080/8081/8082. Only web01 and web02 declare networks: [app-network].

config is clean; up -d brings up three containers in the Up state.

2. Read each container's IP: docker inspect compose-web01 --format "{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}", and do the same for web02 and web03.

web01 and web02 share one IP range; web03 is in a different range.

3. From inside web01, ping web02 by name: docker exec compose-web01 ping -c 3 compose-web02.

It works — 3 packets received. Name resolution works thanks to the network's internal DNS.

4. From inside web01, ping web03's IP: docker exec compose-web01 ping -c 3 <WEB03_IP>.

It does not work — 100% packet loss. This is the correct result of the exercise, not an incident.

5. List the containers in the network: docker network inspect compose-lab_app-network --format "{{range .Containers}}{{.Name}} {{end}}".

Only web01 and web02 appear.

6. Curl ports 8080, 8081 and 8082 in turn from the host.

All three web services respond from the host — the isolation is in the network between containers, not in the entry path from the host.

Analysis

Observation: Three containers in the same file and on the same host, but only two of them can reach each other.

Mechanism: Compose attaches a service to a network according to its networks key; a service that declares nothing goes into the project's default network — two separate address ranges.

Takeaway: Network isolation is a design tool: split networks by the pairs that genuinely need to talk to each other, and do not cram every service into one network.

Troubleshooting:

Hands-on 3 — Lifecycle, two-level logs and tracing the compose file

20 minutes

Tell stop from down, pick the right log level when hunting for errors, and know how to look things up when you lose track of a project —

three tasks that take up most of the time spent operating Compose (docker compose CLI).

docker inspect compose-web01 --format '{{index .Config.Labels "com.docker.compose.project.config_files"}}'

Observe: docker compose ps --all after each command ; compare the output of compose logs with container logs

1. docker compose logs --tail 20, then docker compose logs --tail 20 web01, then docker container logs --tail 20 compose-web02.

The first command merges the logs of three services; the next two show the log of exactly one service or one container.

2. Request a path that does not exist: curl -s http://127.0.0.1:8080/does-not-exist and then look at the web01 log.

The log records the request with a 404 status — a pattern to compare against when you move on to the centralized log-collection lab.

3. docker compose stop and then docker compose ps --all; after that docker compose start and then docker compose ps.

After stop the containers are in the Exited state; after start the very same containers run again.

4. Read all of the Compose labels: docker inspect compose-web01 --format '{{json .Config.Labels}}'.

You see the com.docker.compose.* group of labels, including project, service, working_dir and config_files.

5. cd .. and then run docker compose ps; after that run it again with the -f flag pointing at the file.

The command without -f fails with no configuration file provided; the command with -f works.

6. Back in the project directory, run docker compose down and then docker ps -a and ls -l html.

The project's containers and networks have been removed; the html directory on the host is intact.

Analysis

Observation: stop and down give different results; Compose commands depend on the directory you are standing in; the labels on a container record its origin.

Mechanism: Compose keeps no state of its own — it recognizes its project again through the com.docker.compose.* labels and finds the configuration in the working directory.

Takeaway: The convention of one directory per project, and always cd-ing into it, is the cheapest way to avoid acting on the wrong project on the same host.

Troubleshooting:

Review questions

Question 1. What is the difference in purpose between a compose file and a Dockerfile?

  1. A compose file is the new version of the Dockerfile, a complete replacement

  2. A Dockerfile produces an image; a compose file describes how to run containers from an image

  3. A Dockerfile is for one container, a compose file is for one image

  1. Both produce images, only the syntax differs

Answer: A Dockerfile produces an image; a compose file describes how to run containers from an image.

A Dockerfile is the recipe for building an image (Dockerfile reference); a compose file describes the desired state of a group of containers:

which image, which port, what mounts, which network (Compose file reference).

Compose can invoke a build, but that build still needs a Dockerfile.

Question 2. compose.yaml declares three services web01, web02, web03; only web01 and web02 have a networks key pointing at app-network. What is the result of pinging from inside web01 to web03?

  1. It works, because the three services are in the same compose file

  2. It works, because Docker always lets containers on the same host talk to each other

  3. It does not work, because web03 is on a different network

  1. It does not work, because Docker blocks ping by default

Answer: It does not work, because web03 is on a different network. web03 does not declare networks, so it goes into the project's default network, which differs from app-network —

two address ranges with no route between them (Compose:networking).

This is exactly the test in the accompanying exercise: web01 pings web02 successfully and cannot ping web03.

Question 3. ports declares "127.0.0.1:8080:80". What happens when another machine on the same LAN accesses http://IP-host:8080?

  1. It is reachable, because port 8080 has been published

  2. It is not reachable, because the host only listens on the loopback address

  3. It is reachable if the firewall opens port 8080

  1. It is not reachable, because you also need to declare the expose key

Answer: It is not reachable, because the host only listens on the loopback address. The left side of ports decides which address the host listens on.

127.0.0.1 is the loopback address — only processes on the machine itself can reach it (Docker: published ports).

To let other machines in, use 0.0.0.0 or fill in the IP of the corresponding network interface.

Question 4. After editing compose.yaml, you run docker compose start. Does the change take effect?

  1. Yes, start always re-reads the configuration file

  2. No, start only brings back existing containers — you must run up -d so Compose reads the new configuration

  3. Yes, but only for port changes

  1. No, you have to delete each container by hand and recreate it with docker run

Answer: No, start only brings back existing containers —

you must run up -d so Compose reads the new configuration. start brings back the exact containers that already exist and does not recreate them from the new configuration. up -d compares the

configuration against the existing containers and recreates the services that changed (dockercomposeup).

Question 5. How do docker compose stop and docker compose down differ with respect to the bind-mount directory on the host?

  1. stop keeps the data, down deletes the data on the host

  2. Both delete the data on the host

  3. Both leave the data on the host intact; the difference is in the containers and the network

  1. stop removes the network, down keeps the network

Answer: Both leave the data on the host intact; the difference is in the containers and the network. stop stops the containers and keeps them; down removes the containers along with the project's network.

Neither command touches the host's bind-mount directory, because that directory belongs to the host and not to the container lifecycle (docker compose down).

Question 6. A host has dozens of containers, and you need to know which compose file a container was created from. What is the right way to look it up?

  1. Read /var/lib/docker/containers/<id>/config.v2.json by hand

  2. docker inspect reading the label com.docker.compose.project.config_files

  3. Run docker compose ls in the root directory of the system

  1. Grep the entire disk for a compose.yaml file containing the container name

Answer: docker inspect reading the label com.docker.compose.project.config_files.

Compose attaches the com.docker.compose.* group of labels to the container, and config_files records the absolute path of the file that produced it (docker inspect).

This is a fast and precise way to look it up.

Question 7. What does depends_on guarantee?

  1. The dependent service starts only after the other service

  2. The dependent service starts only after the other service is ready to accept connections

  3. The two services are always on the same network

  1. The other service will be restarted if the dependent service fails

Answer: The dependent service starts only after the other service. depends_on only decides the start ORDER.

To wait until the other service is truly ready, you must also declare a healthcheck and the service_healthy condition (Compose: depends_on).

Question 8. What happens when you run a docker compose command from a directory that does not contain compose.yaml?

  1. It runs normally because Compose searches upward through the parent directories

  2. It fails with a configuration-not-found message — unless you point to the file with the -f flag

  3. It runs but applies to every project on the host

  1. It runs but skips the volumes section

Answer: It fails with a configuration-not-found message — unless you point to the file with the -f flag.

Compose looks for the configuration file in the working directory; if there is none, it reports no configuration file provided.

The -f flag lets you point at a specific path (docker compose CLI).

When working by hand, the recommendation is still to cd into the project directory.

Flashcards

A Dockerfile produces an image; a compose file describes how to run multiple containers from images on one host.

*Tell them apart correctly and an image is built once and used in many environments, while the compose file is the part that changes per environment. *

docker compose (no hyphen) is the current v2; docker-compose is v1, which is no longer supported. Check with docker compose version.

The address and port on the host: 0.0.0.0 = every network interface, 127.0.0.1 = only the machine itself, or the IP of the specific interface you want to expose on.

docker compose config — merges and prints the final configuration, reporting syntax errors before any container is created. YAML is indented with spaces, not tabs.

In the project's default network — different from a dedicated network declared in the file, so it cannot see the services on that dedicated network.

By service NAME — Compose provides internal DNS. You do not need to know the IP, and the IP can change after every recreation.

stop stops but keeps the containers; down removes the project's containers and networks. Neither deletes bind-mount data on the host.

docker compose up -d — start does not re-read the new configuration.

docker compose logs -f for the whole project; add a service name to filter; docker container logs -f targets one specific container by name or ID.

docker inspect reading the label com.docker.compose.project.config_files — it records the absolute path of the file that produced the container.

No, unless you use the -f flag pointing at the file. Compose looks for the configuration in the current directory and resolves relative paths against the file's location.

Only the start order; it does not guarantee that the other service is ready to accept connections — for that you need to add a healthcheck.

A host port can be held by only one process; on a clash the second service does not come up, with the error address already in use.

Docker recreates the container every time it exits, unless the operator stopped it themselves. If the Status shows Restarting, read the log right away.

Docker Compose: one file, many containers