How to get the numeric exit status of an exited docker container?

Adam Matan picture Adam Matan · Sep 19, 2017 · Viewed 20.8k times · Source

When a container exits, docker ps -a shows its exit code (scroll

$ docker run ubuntu bash -c "exit 1"
CONTAINER ID   IMAGE    COMMAND              CREATED        STATUS                     PORTS                     NAMES
c2c769c4b9ef   ubuntu   "bash -c 'exit 1'"   6 seconds ago  Exited (1) 3 seconds ago                             happy_fermat

How do I get the numeric exit code programmatically, without error-prone grep-ing and cut-ing?

Answer

Adam Matan picture Adam Matan · Sep 19, 2017

Use docker inspect with templates:

$ docker inspect c2c769c4b9ef --format='{{.State.ExitCode}}'
1