appzasGamesToolsDevLearnCheat sheetsQuick calcsConvertersReferenceNetworkTimeCalculatorsCompareLLM prices

Debugging containers

Lesson 3 of 411 minAdvanced

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, network

Reading the exit code

CodeUsually means
0The process finished normally. A container ends when its main process ends
1The application raised an error. Read the logs
137Killed with SIGKILL, often out of memory (check docker inspect for OOMKilled)
139Segmentation fault
143Stopped 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 myapp

NoteOn 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.

1. A container exited with code 137. What is a likely cause?
2. Why does a container end right after start if its process daemonises?
Hintdocker logs with --tail and -f.

Key terms