Follow
Oct 21, 2019 · 4 min read
Docker container exit code — how to use them for troubleshooting?
It’s one of the most common question that I come across: “Why is my container not running?”
Can docker container exit codes help troubleshoot this issue?
The first step in answering this question is to identify the exit code for the docker container. The exit code may give a hint as to what happened to stop the container running. This article lists the most common exit codes when working with docker containers and aims to answer two important questions:
This will ultimately help answer the original question: “Why is my container not running?”
docker ps --filter "status=exited"
docker ps -a grepExample: docker ps -a | grep hello-world
docker inspect--format='{{.State.ExitCode}}'Example: docker inspect ca6cbb290468 --format='{{.State.ExitCode}}'
Common exit codes associated with docker containers are:
Here is an example using the public docker container — “hello-world”. If you have docker installed on your system or VM instance, run this:
docker run hello-world
You will get a message, “Hello from docker!” but try to find the container using this code:
docker ps -a | grep hello-world
You’ll notice that the container exited and the exit code is 0. This is because the container does not have any foreground process attached, such as a Java process or a shell process that runs until a SIGTERM event occurs
Exit code 0
sample.ja
instead of sample.jar
)Exit Code 1
docker kill
docker kill
can be initiated manually by the user or by the host machine. If initiated by host machine, then it is generally due to being out of memory. To confirm if the container exited due to being out of memory, verify docker inspect
against the container id for the section below and check if OOMKilled
is true (which would indicate it is out of memory):"State": { "Status": "exited", "Running": false, "Paused": false, "Restarting": false, "OOMKilled": true, "Dead": false, "Pid": 0, "ExitCode": 137, "Error": "", "StartedAt": "2019-10-21T01:13:51.7340288Z", "FinishedAt": "2019-10-21T01:13:51.7961614Z" }
docker stop
or docker-compose stop
. In this case there was a manual termination that forced the container to exit:docker stopOR docker-compose down
docker stop
can also result in exit code 137. This typically happens if the application tied to the container doesn’t handle SIGTERM — the docker daemon waits ten seconds then issues SIGKILL