Sub topic 15
Debugging with Docker Compose
This chapter covers how to debug a multi-container application started with Docker Compose. It assumes you can already use docker ps, docker inspect, docker logs, and docker exec on single containers, and that you have read a basic compose.yaml. Compose adds a project layer on top of those commands: services, generated container names, a default network, and startup ordering. Most Compose problems come from that layer, not from the container itself.
Services, containers, and the project
A Compose file defines services. Compose turns each service into one or more containers and groups them into a project. The project name defaults to the directory name, and container names look like myapp-web-1.
Two consequences matter when debugging:
- Compose commands take a service name (
web). Plaindockercommands take a container name or ID (myapp-web-1). - Compose commands only see the current project.
docker psshows containers from every project on the host.
If a Compose command reports "no such service", check that you are in the right directory or pass -p <project> or -f <file> explicitly.
Start with the state of every service
docker compose ps -aThe -a flag includes stopped containers. Without it, a service that crashed is simply missing from the list, which is easy to misread as "never started". Read the STATUS column the way you read it for single containers:
Up 3 minutes (healthy)means running and passing its health check.Up 10 seconds (health: starting)means the health check has not passed yet.Restarting (1) 5 seconds agomeans a crash loop. The number is the last exit code.Exited (137) 2 minutes agomeans the container stopped. Chapter material on exit codes applies unchanged: 137 is SIGKILL, often an out-of-memory kill.Createdmeans the container was made but never started. This often means a dependency was not ready or a port could not be bound.
Once you know which service is wrong, you can drop down to the plain Docker tools using its container name.
Read the final configuration
Compose does substitution and merging before it creates anything. Values from .env, shell variables, environment: blocks, and multiple -f files are combined. The result may not match what you think you wrote.
docker compose configThis prints the final configuration after variable substitution and file merging. It also validates syntax, so a typo in the YAML or an unset required variable shows up here with a line reference.
Check these items first:
- Variables that resolved to an empty string. Compose warns about unset variables, and the warning is easy to miss in a long output.
- Port mappings, volume paths, and
commandorentrypointvalues after merging an override file. - Which image or build context each service uses.
docker compose config --services
docker compose config webThe first lists service names. The second prints the resolved configuration for one service in current Compose versions. If your version rejects it, use the full output.
Read logs per service and across services
docker compose logs --tail 50 web
docker compose logs --follow --timestamps web db
docker compose logs --no-log-prefix webPassing no service name merges the logs of all services, each line prefixed with the service name. With --timestamps, you can line up events across services. A common pattern is that web fails at 10:00:03 with "connection refused", and the log for db shows it only finished starting at 10:00:07.
Logs from a container that was recreated are gone. If docker compose up replaced the failing container, you are looking at the new one. To keep the failing run's output, avoid up --force-recreate and down until you have captured the logs, for example with docker compose logs web > web.log.
Startup order is not readiness
The most common Compose bug is a service that starts before the thing it depends on can accept connections. The depends_on key in its short form only controls the order in which containers are started. It does not wait for the dependency to be ready, because "started" means the process was launched, not that a database is accepting connections.
The fix is to pair a health check with a condition:
services:
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: example
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 3s
retries: 5
start_period: 10s
web:
image: myimage:latest
depends_on:
db:
condition: service_healthy
ports:
- "8080:8080"With this file, Compose starts db, waits until its health check passes, then starts web. If db never becomes healthy, docker compose up stops with an error naming the dependency, and web stays in the Created state. Inspect the probe output with:
docker inspect --format '{{json .State.Health}}' myapp-db-1Other conditions are service_started (the default behavior) and service_completed_successfully, which suits one-shot jobs such as database migrations.
A service_healthy condition only helps if the health check tests the right thing. A check that passes as soon as the port opens can still let the application connect too early. Applications should also retry connections on their own, because a database can restart after the first start.
Networking between services
Compose creates a default network for the project. Each service is reachable by its service name through Docker's built-in DNS. A few rules explain most failures:
- Use the service name as the host, such as
db:5432.localhostinside thewebcontainer is thewebcontainer itself. - Use the container port, not the published port, for traffic between services. If
dbpublishes5433:5432, other services still connect todb:5432. - Published ports (
ports:) are for access from the host.expose:only documents a port and does not publish it. - Services on different networks cannot reach each other. Check any
networks:keys in the resolved config.
To test from inside the project network, run a command in a service that has the tools:
docker compose exec web getent hosts db
docker compose exec web curl -sv http://api:8000/healthIf the image has no shell or tools, attach a tooling container to the service's namespaces, as in the earlier chapter on minimal images:
docker run --rm -it --network=container:myapp-web-1 nicolaka/netshootName resolution works in that tooling container because it shares the network namespace of web, including its DNS settings.
Run commands with exec and run
Compose has two ways to get a shell, and they behave differently.
| Command | What it does | Use it when |
|---|---|---|
| `docker compose exec web sh` | Runs a command inside the existing running container | The container is up and you want to see its live state |
| `docker compose run --rm web sh` | Creates a new container from the service definition and runs the command in it | The service crashes at startup, or you do not want to touch the running one |
docker compose run applies the service's environment, volumes, and networks, so it reproduces the failing setup closely. It does not publish the service's ports by default, and it starts dependencies unless you pass --no-deps. To replace the normal start command, use --entrypoint:
docker compose run --rm --no-deps --entrypoint sh webThis is the Compose equivalent of the entrypoint override technique used for a container that exits at startup.
Rebuilding and recreating
A frequent source of confusion is that the container is running an older image than the code on disk. Compose does not rebuild an image when you change source files unless you ask:
docker compose up -d --build web
docker compose up -d --force-recreate web--build rebuilds the image. --force-recreate replaces the container even if its configuration did not change, which resets its writable layer. To check which image a container uses and when it was created, run docker inspect --format '{{.Image}} {{.Created}}' myapp-web-1 and compare against your build time.
Environment changes need a recreate too. Editing .env or the environment: block has no effect on a running container until docker compose up -d recreates it.
A Compose debugging sequence
- Run
docker compose ps -ato find which services are not in the expected state. - Run
docker compose configto confirm the file resolves as you intended. - Read
docker compose logs --timestampsfor the failing service and its dependencies. - Use
docker inspecton the container for exit code,OOMKilled, and health results. - Check dependency order and health conditions if a service fails only at startup.
- Use
docker compose execfor a running service ordocker compose run --no-deps --entrypoint shfor one that will not stay up.
Exercise
A web service connects to db:5432 and logs "connection refused" on startup, but if you run docker compose restart web a minute later it works. Your Compose file has depends_on: [db] with no health check. What is happening, and what two changes fix it?
Show answer
depends_on in its short form only starts db before web. It does not wait for PostgreSQL to accept connections, so web tries to connect while the database is still initializing. The restart works because the database is ready by then. Fix it by adding a healthcheck to db (for example pg_isready) and changing web to depends_on: db: condition: service_healthy. Also make the application retry its database connection, so it survives a later database restart.
Exercise
A service named worker exits immediately after docker compose up, and its logs are empty. Which Compose command lets you get a shell in a container with the same environment, volumes, and networks, without starting its dependencies?
Show answer
docker compose run --rm --no-deps --entrypoint sh worker. It creates a new container from the service definition, replaces the entrypoint with a shell, and skips dependency services because of --no-deps.