Estimated time: 50 minutes.
One file replaces a chain of docker run commands
Why: compose.yaml describes many containers in a single file: image, name, ports, mounts, network, restart policy (Compose file reference).
The main value is not typing speed but that the file is readable, comparable and can be put in Git.
When it breaks: If the configuration lives in your head or in shell history, no one else can rebuild the system, and even the author forgets after a few weeks.
In real deployments: One directory per project, with the compose file in Git; configuration changes go through commits rather than being edited directly on the server.
The basic keys of a service
Why: image · container_name · restart · ports · volumes · networks —
each key corresponds to exactly one docker run flag (Compose file reference,
Understanding this one-to-one relationship lets you read the compose file of any project without learning it again.
When it breaks: Setting a fixed container_name on every service means that service cannot be scaled to multiple replicas, and two projects with the same name will collide on the host.
In real deployments: Pin a specific image tag instead of latest; set container_name only when you really need a known name for looking up logs.
The left side of ports decides where it listens
Why: The full form is "address:host-port:container-port".
0.0.0.0 means listen on every network interface of the host; 127.0.0.1 means only the machine itself can reach it; with a specific IP, only that interface exposes the service (Docker: published ports).
When it breaks: Leaving 127.0.0.1 and then hunting for the fault in the firewall is wasted time — the service never listened on the interface that other machines come in through.
In real deployments: On a server with several network interfaces: bind internal services to the internal-network IP, and do not expose them on every interface by default.
config validates before up
Why: docker compose config merges and prints the final configuration, reporting syntax errors before any container is created (docker compose config).
YAML indents with spaces, not tabs — this is the most common mistake when writing the file by hand.
When it breaks: If you skip this step, an indentation error shows up in the middle of up, when some services are already running and some are not — cleaning up a half-finished state takes longer.
In real deployments: Put docker compose config into the CI check step: an invalid configuration never reaches the server.
The network Compose creates and isolation
Why: Services on the same network resolve each other by service NAME and share one IP range;
a service on a different network has no route to them (Compose:networking,
The project network is named in the form directory-name_network-name.
When it breaks: Cramming every service into one shared network opens up lateral movement: one compromised service can reach even the database that did not need to be exposed.
In real deployments: Split networks by the pairs that need to talk to each other; a service exposed to the outside does not sit in the same network as the data tier.
Lifecycle: up · stop · start · down
Why: up -d creates and runs in the background; stop stops but keeps the containers; start brings the old containers back;
down removes the project's containers and networks (docker compose up,
Neither stop nor down deletes data on the host's bind mounts.
When it breaks: Using start after editing the compose file means the change does not take effect — the new configuration is only re-read on up.
In real deployments: Update workflow: edit the file in Git → pull it onto the server → docker compose up -d so Compose recreates exactly the services that changed.
Logs of the whole project vs logs of one container
Why: docker compose logs -f merges the logs of EVERY service in the project; add a service name at the end to filter;
docker container logs -f targets exactly one container by name or ID (docker compose CLI).
When it breaks: Watching the merged log of a seven-service stack while hunting for one service's error buries the line you want in noise.
In real deployments: Use the merged log while starting the stack to see the order and early errors; after that, switch to per-service logs once you have narrowed down the area.
Tracing a container back to its compose file
Why: Compose attaches the labels com.docker.compose.project, .service,
.working_dir and .config_files to the container. docker inspect reads these labels to tell which file the container came from (docker inspect).
When it breaks: Without this lookup path, on a host with many projects all you can do is dig through directories one by one — and it is easy to run a cleanup command against someone else's project.
In real deployments: When taking over an unfamiliar server: list the containers and read the config_files label to rebuild the project map before touching anything.
depends_on: start order, not readiness
Why: depends_on guarantees that the dependent service is started later, for example a log-collection agent starting after the log receiver (Compose: depends_on).
It does not wait until the other service is ready to accept connections — for that you must also declare a healthcheck.
When it breaks: Believing depends_on means "wait until ready" leads to services that start in the right order but still hit connection errors in the first few seconds.
In real deployments: Combine depends_on with a healthcheck for a truly hard dependency; beyond that, having the application retry its connection on its own is the more durable approach.
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.
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.
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.
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:
config reports an error at the very first line of the service: yaml: line N: found character that cannot start any token — a tab mixed into the indentation
up fails because the port is already taken: Error response from daemon: ... bind: address already in use
How to fix:
Replace every tab in the file with spaces and validate again
sed -i "s/\t/ /g" compose.yaml && docker compose config
Find and clean up the process or container holding the port
ss -lntp | grep :8080 ; docker ps --format "table {{.Names}}\t{{.Ports}}"
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.
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.
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:
ping to web03 gets no response at all: 3 packets transmitted, 0 received, 100% packet loss — exactly what the exercise expects, not an incident
ping by the name compose-web03 reports it cannot be resolved: ping: bad address — the internal DNS only resolves within the same network
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.
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.
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:
A compose command run outside the project directory fails: no configuration file provided: not found
How to fix:
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.
Wrong: Docker Compose is an upgrade of the Dockerfile, so once you write Compose you no longer need a Dockerfile.
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.
Wrong: Services declared in the same compose file can talk to each other automatically.
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.
Wrong: docker-compose and docker compose are two ways of writing the same command.
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.
Wrong: docker compose down is the command to stop a project's containers.
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.
Wrong: The docker compose command works from any directory, as long as the project is running.
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.
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:
up reports address already in use: Port 8080 is taken. Check with ss -lntp | grep :8080 and then clean up the old container or change the host port.
The page returns 403 Forbidden: The mounted directory lacks index.html or has the wrong read permission — check ls -l html.
Other machines cannot reach it but curl on the host works: The left side of ports is 127.0.0.1. Change it to 0.0.0.0 or the interface's IP and run up -d again.
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:
ping reports bad address when called by name: The internal DNS only resolves within the same network. To reach a container on another network you must use the IP — and it will not get through either.
A service does not come up: Usually a host-port clash. Read the output of up or docker compose ps --all and then change the port.
The network name differs from the sample command: Compose names it in the form directory-name_network-name. Get the real name with docker network ls.
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:
You edited the compose file but the change did not take effect: You used start. Run docker compose up -d so Compose compares the configuration and recreates the changed services.
You need to clean up many stopped containers: docker rm $(docker ps -aq) cleans up quickly in the lab.
Do not use it on a production machine because it sweeps the whole host without distinguishing projects.
Question 1. What is the difference in purpose between a compose file and a Dockerfile?
A compose file is the new version of the Dockerfile, a complete replacement
A Dockerfile produces an image; a compose file describes how to run containers from an image
A Dockerfile is for one container, a compose file is for one image
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.
The two tools do not replace each other: with no image, the compose file has nothing to run. And a compose file orchestrates many containers, not one image.
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?
It works, because the three services are in the same compose file
It works, because Docker always lets containers on the same host talk to each other
It does not work, because web03 is on a different network
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.
Being in the same file does not mean being on the same network. Docker does not let containers on two different networks see each other, and it does not block ping within the same network.
Question 3. ports declares "127.0.0.1:8080:80". What happens when another machine on the same LAN accesses http://IP-host:8080?
It is reachable, because port 8080 has been published
It is not reachable, because the host only listens on the loopback address
It is reachable if the firewall opens port 8080
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.
Opening the firewall does not help when the service never listened on that interface. The expose key is documentation only and does not publish a port to the host.
Question 4. After editing compose.yaml, you run docker compose start. Does the change take effect?
Yes, start always re-reads the configuration file
No, start only brings back existing containers — you must run up -d so Compose reads the new configuration
Yes, but only for port changes
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).
start does not re-read the configuration for any kind of change. Nor do you need to delete by hand: up -d takes care of the recreation.
Question 5. How do docker compose stop and docker compose down differ with respect to the bind-mount directory on the host?
stop keeps the data, down deletes the data on the host
Both delete the data on the host
Both leave the data on the host intact; the difference is in the containers and the network
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).
down does not delete bind mounts — to delete them you have to run rm on the host yourself. And stop does not touch the network.
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?
Read /var/lib/docker/containers/<id>/config.v2.json by hand
docker inspect reading the label com.docker.compose.project.config_files
Run docker compose ls in the root directory of the system
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.
Reading the daemon's internal files is a roundabout and error-prone approach. docker compose ls lists projects but does not help when you need to start from one specific container; grepping the whole disk is both slow and inconclusive.
Question 7. What does depends_on guarantee?
The dependent service starts only after the other service
The dependent service starts only after the other service is ready to accept connections
The two services are always on the same network
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).
There is no guarantee about readiness, no relation to the network, and no reverse restart mechanism.
Question 8. What happens when you run a docker compose command from a directory that does not contain compose.yaml?
It runs normally because Compose searches upward through the parent directories
It fails with a configuration-not-found message — unless you point to the file with the -f flag
It runs but applies to every project on the host
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.
Compose does not search upward through parent directories on its own, does not apply to every project, and does not skip any part of the configuration.
What is a Dockerfile for, and what is a compose file for?
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 or docker-compose?
docker compose (no hyphen) is the current v2; docker-compose is v1, which is no longer supported. Check with docker compose version.
What does the left side of ports mean?
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.
Which command validates a compose file?
docker compose config — merges and prints the final configuration, reporting syntax errors before any container is created. YAML is indented with spaces, not tabs.
Where does a service that does not declare networks live?
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.
Within the same network, how do containers call each other?
By service NAME — Compose provides internal DNS. You do not need to know the IP, and the IP can change after every recreation.
How does stop differ from down?
stop stops but keeps the containers; down removes the project's containers and networks. Neither deletes bind-mount data on the host.
After editing the compose file, which command do you run?
docker compose up -d — start does not re-read the new configuration.
Viewing logs for the whole project vs one service?
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.
How do you trace a container to its compose file?
docker inspect reading the label com.docker.compose.project.config_files — it records the absolute path of the file that produced the container.
Can you run a compose command from another directory?
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.
What does depends_on guarantee?
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.
Why must three web services use three different host ports?
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.
What does restart: unless-stopped mean?
Docker recreates the container every time it exits, unless the operator stopped it themselves. If the Status shows Restarting, read the log right away.