Sub topic 15
Debugging with Docker Compose
This chapter assumes you know basic Docker commands, can read a compose.yaml file, and have read the earlier chapters on docker ps, docker inspect, docker logs, and exit codes. Compose runs several containers as one project. The debugging steps are the same as for a single container, but you work per service and have to watch how the services depend on each other.
Find the failing service
Start with the state of every service in the project. The -a flag includes services that have already exited.
docker compose ps -aThe output shows each service's state, exit code, and health. Look for the first service that failed, not the loudest one. A web service that crashes with "connection refused" often points to a db service that never became ready.
Read logs per service
Pass a service name to limit the output. Without a name, Compose merges the logs of all services and prefixes each line with the service name.
docker compose logs --tail 50 webAdd --follow to stream new lines, and --timestamps to compare the order of events across services. Logs from a container that has already exited are still available until the container is removed.
Check the final configuration
Compose builds its configuration from the file, .env files, shell variables, and override files. A value that looks right in compose.yaml may be replaced by one of the others. Print the merged result with variables already substituted:
docker compose configIf a variable is not set, Compose substitutes an empty string and prints a warning. This is a common cause of a container that starts with an empty password or the wrong image tag.
Understand startup order
depends_on controls the order in which containers start. By default it does not wait for the dependency to be ready. A database container can be running while its server is still initializing. To wait for readiness, define a health check on the dependency and use a condition:
services:
db:
image: postgres:16
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 3s
retries: 10
web:
image: myapp:latest
depends_on:
db:
condition: service_healthyIf docker compose ps shows db as unhealthy, web will not start. Inspect the health probe results as described in the health check chapter.
Debug a single service
Use the same tools as before, with Compose naming. Compose names containers <project>-<service>-<number>, so you can use the plain docker commands too. Compose also has service-aware equivalents:
docker compose exec web sh
docker compose run --rm --entrypoint sh webexec opens a shell in a running container. run starts a new container from the service definition, with its networks, volumes, and environment, and replaces the entrypoint. This is the Compose version of reproducing a failed start.
Services reach each other by service name on the project's network. From inside web, test the name directly, for example getent hosts db. If the name does not resolve, check that both services share a network in the configuration.
Common errors
| Symptom | Likely cause | Check |
|---|---|---|
| Service exits right after `up` | Dependency not ready | `depends_on` with `service_healthy` |
| Empty or wrong setting | Unset variable or override file | `docker compose config` |
| Name does not resolve | Services on different networks | Network settings in the file |
| Old behavior after a code change | Image not rebuilt | `docker compose up --build` |
Exercise
A web service exits with a database connection error, and docker compose ps -a shows db as running. Which two things do you check next, and which Compose setting makes web wait for the database?
Show answer
Check the db logs to see whether the server finished starting, and check whether db has a health check. Add a health check to db and set depends_on with condition: service_healthy on web, because plain depends_on only waits for the container to start.