Debugging containers
Find out why a container exits, restarts or behaves differently from your laptop.
Check the same few things in order and most problems show up.
docker ps -a # is it running? exited? restarting?
docker logs --tail 100 -f myapp # what did it print?
docker inspect myapp # config, env, mounts, exit code, health
docker exec -it myapp sh # a shell inside the running container
docker stats # live CPU, memory, networkReading the exit code
| Code | Usually means |
|---|---|
| 0 | The process finished normally. A container ends when its main process ends |
| 1 | The application raised an error. Read the logs |
| 137 | Killed with SIGKILL, often out of memory (check docker inspect for OOMKilled) |
| 139 | Segmentation fault |
| 143 | Stopped with SIGTERM (a normal docker stop) |
The container exits immediately
- Run it with a shell instead of the normal command to look around:
docker run -it --entrypoint sh myapp. - A process that runs in the background (daemonises) makes the container exit. The main process must stay in the foreground.
- Check the working directory, file permissions and the user the container runs as.
Logs in production
Write logs to stdout and stderr, not to files inside the container. The default json-file driver grows without limit, so set a cap.
docker run -d --log-opt max-size=10m --log-opt max-file=3 myappNoteOn images without a shell (distroless), attach a temporary debug container that shares the target's namespaces, for example with docker debug or, in Kubernetes, kubectl debug.
Test yourself
Answer all the questions, then check them. Finish with every answer right to mark the lesson as done.