Sub topic 13
A troubleshooting workflow with logs and exec
This chapter assumes you can run containers with docker run, list them with docker ps, and use a Unix shell with pipes and redirection. It also assumes you know the basics of docker logs and docker exec from earlier chapters. Here you combine them into a repeatable routine for a container that misbehaves.
The workflow
Work from the cheapest, least invasive check to the most invasive one. Logs cost nothing and change nothing. An interactive shell lets you alter a running container, so use it after the logs have told you where to look.
Step 1: Check the state
docker ps -a --filter name=webThe STATUS column tells you whether the container is Up, or Exited (code). An exited container cannot run docker exec, but docker logs still works on it, because the daemon keeps the log until you remove the container.
Step 2: Read the logs
Start with the end of the log, with timestamps, so you can line events up with what you did.
docker logs --tail 50 --timestamps webIf the output is noisy, narrow it by time and search both streams.
docker logs --since 10m web 2>&1 | grep -i errorRemember that docker logs sends the container's stderr to your stderr. Without 2>&1, grep sees only stdout and can miss the error you are looking for.
Many problems end here. A message such as bind() to 0.0.0.0:80 failed or permission denied often names the cause directly.
Step 3: Run one-off checks with exec
If the logs are not enough, test the container from the inside with single commands. These are easy to repeat and easy to script.
docker exec web nginx -t
docker exec web ls -l /var/log/nginx
docker exec -u root -w /etc/nginx -e DEBUG=1 web envThe first command validates the nginx configuration. The second shows whether the log files are links to stdout and stderr, which explains why docker logs has output for nginx. The third runs as root in a chosen directory, which helps when a permission problem depends on the user.
Step 4: Use exit codes
docker exec returns the exit code of the command it ran. That lets you turn a check into a pass or fail result.
docker exec web nginx -t
echo $?An exit code of 0 means success. Any other value means failure. A script can branch on it:
if docker exec web nginx -t; then
echo "config ok"
else
echo "config invalid, check docker logs web"
fiStep 5: Open a shell as a last resort
When you need to explore, open a shell.
docker exec -it web shUse sh when the image has no bash. Minimal images may have neither. In that case, run single commands as in step 3, or debug with a separate container.
Common errors
| Symptom | Likely cause | Action |
|---|---|---|
| `docker logs` prints nothing | The app writes to a file, not stdout or stderr, or the logging driver does not support reading | Check the driver with `docker inspect --format '{{.HostConfig.LogConfig.Type}}' web`, then look for log files with exec |
| `docker exec` says the container is not running | The container exited | Use `docker logs` and the exit code from `docker ps -a` |
| `executable file not found` | The tool or shell is missing from the image | Try `sh`, or run a different command |
| Changes made in a shell disappear | Edits live in the container's writable layer | Put the fix in the Dockerfile or configuration, then recreate the container |
The same workflow in Compose
Compose addresses a service by name instead of a container name.
docker compose logs --follow --tail 100 web
docker compose exec web shThe steps are the same. Read the logs first, then use exec for targeted checks.
Exercise
A container named api keeps showing "Exited (1)" shortly after it starts. Which commands do you run first, and why can't you use docker exec?
Show answer
Run docker ps -a --filter name=api to confirm the state, then docker logs --tail 50 --timestamps api 2>&1 to read the last output, including stderr. docker exec only works on a running container, and this one has exited. The logs are still available, and they usually name the cause of the failure.
Exercise
Write a shell line that prints "ok" only when the nginx configuration in the container web is valid.
Show answer
docker exec web nginx -t && echo ok. Because docker exec returns the exit code of nginx -t, the echo runs only when that code is 0.