Sub topic 10
Debugging with logs, ps, exec, and config
This chapter assumes you can write a compose.yaml, start a stack with docker compose up -d, and read basic container output. It uses the shop stack from earlier chapters, with a web service and a db service.
A debugging order that works
When a stack misbehaves, check these in order:
psshows whether each container is running, restarting, or unhealthy.logsshows what the process inside printed.configshows what Compose actually resolved from your files.execlets you look inside a running container.
Check state with ps
docker compose psThe output lists each service, its state, and its published ports. Look at the status column. Up (healthy) means the health check passes. Up (health: starting) means checks have not passed yet. Restarting means the main process keeps exiting. Add -a to include stopped containers, which matters when a service exited right after start.
Read logs
docker compose logs web
docker compose logs -f --tail 50 web db
docker compose logs -t web- With no service name,
logsprints every service, each line prefixed by its service name. -ffollows new output. Press Ctrl+C to stop following. The containers keep running.--tail 50limits output to the last 50 lines per container.-tadds timestamps, which helps when you compare two services.
Logs only contain what the process writes to standard output and standard error. If an application writes only to a file inside the container, logs shows nothing. Configure the application to log to the console, or read the file with exec.
Inspect with exec
exec runs a command in a container that is already running.
docker compose exec db psql -U app -d app
docker compose exec web sh
docker compose exec web envThe last command prints the environment the process sees. This quickly shows a missing or wrong DATABASE_URL. From a shell in web, you can also test networking, for example getent hosts db to confirm the name db resolves.
If a container crashes on start, there is nothing to exec into. Use docker compose run --rm web sh to start a fresh container from the same service definition with a shell instead of the normal command. Use logs first, since it still works on exited containers.
Verify the result with config
docker compose configThis prints the merged file after variable interpolation, override files, and profile selection. Use it to catch these common problems:
- A variable such as
${DB_PASSWORD}is empty because.envis missing or misnamed. Compose also prints a warning that the variable is not set. - An override file changed a value you did not expect.
- Indentation puts a key under the wrong parent.
configfails with a validation error that names the problem.
docker compose config --services lists only the service names. docker compose config -q prints nothing and returns a non-zero exit code on error, which is useful in scripts.
Common failure patterns
| Symptom in `ps` | Likely cause | Where to look |
|---|---|---|
| `Restarting` | Process exits at startup | `logs` for the error |
| `Up (unhealthy)` | Health check command fails | `exec` and run the check by hand |
| `Exited (1)` right away | Bad command or missing config | `logs`, then `config` |
| Port not listed | Missing `ports` entry | `config` |
Exercise
The web container shows Restarting and docker compose logs web prints "connection refused" for host localhost:5432. Which command would you run next to see the value the service received, and what is the likely fix?
Show answer
Run docker compose config to see the resolved DATABASE_URL, or run docker compose run --rm web env. The URL probably uses localhost, which inside the web container refers to web itself. Change the host to the service name db.
Exercise
You run docker compose logs web and see nothing, although the application is running. Name one possible reason and one way to confirm it.
Show answer
The application may write logs only to a file inside the container instead of stdout and stderr. Confirm it with docker compose exec web sh and look for log files in the application's directory, then configure the application to log to the console.