How do you find out why a Docker container exited immediately?
Run docker ps -a to see its status and exit code, then docker logs for the output. A container lives only as long as its main process, so a quick exit means the process finished or crashed.
Explain it simply
A container is not a small computer that stays on. It is a process wearing a costume. When the process ends, the container ends, like an actor leaving the stage when his scene is over.
docker ps shows only running containers, so a crashed one seems to vanish. docker ps -a lists stopped ones too, with an exit code in the STATUS column: Exited (0) means the process finished normally, Exited (1) is a generic error, Exited (137) means it was killed by SIGKILL (often out of memory), and Exited (127) means command not found.
Then run docker logs <name> for the error message, and docker inspect <name> for details such as State.OOMKilled. A very common cause: someone runs docker run ubuntu, which has no long-running process, so it exits straight away.
Example
docker run --name t1 ubuntu
docker ps -a # t1 Exited (0) a few seconds ago
docker logs t1
docker inspect -f '{{.State.ExitCode}} {{.State.OOMKilled}}' t1
# 0 falseWhat to say in the interview
"I run docker ps -a to see the exit code, docker logs for the error output, and docker inspect to check OOMKilled. Exit 0 means the main process finished, 137 usually means killed or out of memory, and 127 means the command was not found."
Common mistakes
- Expecting a bare ubuntu or alpine container to keep running without a foreground process.