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?
Use
docker inspect
with templates:The exit status of
docker run
is the exit status of the contained command, except if there is a problem with Docker itself (in which case the status is 125) or the contained command could not be invoked (in which case the status is 126) or could not be invoked (in which case the status is 127).Therefore when you execute
docker run
as a child process, you can retrieve the exit status as you would for any other child process:waitpid()
.bash
), as the content of the special$?
variable immediately after completion of adocker run
.Process.exitValue()
for theProcess
you used to dodocker run
.If you are using a Docker API, it should provide access to the exit status.
ContainerExit.statusCode()
of theContainerExit
object returned bydocker.waitContainer()
.You can use
echo
: