Sub topic 11
Exit codes and using exec in scripts
This chapter assumes you can run docker run, docker ps, and docker exec. It also assumes you know basic shell pipes, redirection, and the $? variable. Here you will learn how docker exec reports success or failure, and how to build that into scripts.
What the exit code means
Every Unix process ends with an exit code, a number from 0 to 255. By convention, 0 means success and any other value means failure.
docker exec returns the exit code of the command it ran inside the container. It does not return a code of its own unless Docker itself fails. Your script can therefore treat docker exec like a local command.
docker run -d --name web -p 8080:80 nginx
docker exec web sh -c 'exit 3'
echo $?The echo line prints 3. The command inside the container chose that code, and Docker passed it through.
Docker's own exit codes
When docker exec cannot run your command, it returns a code that tells you why.
| Code | Meaning |
|---|---|
| 0 | The command succeeded. |
| 1 to 125 (except as below) | The command ran and returned this code. Some low codes can also come from Docker itself. |
| 125 | The Docker client or daemon reported an error, for example the container does not exist or is not running. |
| 126 | The command was found but could not be run, for example it is not executable. |
| 127 | The command was not found inside the container. |
Codes 126 and 127 follow the shell convention. They are the usual sign of a typo or a tool missing from a slim image.
Using the code in a script
Use the exit code in an if statement. Here the script checks that nginx has a valid configuration before it reloads.
#!/bin/sh
if docker exec web nginx -t; then
docker exec web nginx -s reload
echo "reloaded"
else
echo "nginx configuration is invalid, not reloading" >&2
exit 1
fiIf you need the exact value, save it right away. Any later command overwrites $?.
#!/bin/sh
docker exec web sh -c 'exit 3'
code=$?
case "$code" in
0) echo "ok" ;;
127) echo "command not found in container" ;;
*) echo "failed with code $code" ;;
esacFlags that matter in scripts
Do not use -t in scripts that run without a terminal, such as cron jobs or CI pipelines. Docker reports "the input device is not a TTY" when you ask for a terminal that does not exist. A terminal also merges stderr into stdout and can add carriage returns to the output. Use -i only when you pipe data into the command.
echo "select 1;" | docker exec -i db psql -U postgresCapturing output works the same way as for any command.
version=$(docker exec web nginx -v 2>&1)
echo "Running: $version"Here 2>&1 is needed because nginx -v writes to stderr.
Exercises
Exercise
A script runs docker exec web curl -s http://localhost/health and gets exit code 127. What is the most likely cause, and how can you confirm it?
Show answer
The curl command does not exist in the container image, which is common in slim images. Confirm it with docker exec web sh -c 'command -v curl'. If that prints nothing and returns a non-zero code, curl is not installed.
Exercise
Why is docker exec -it web nginx -t a poor choice in a CI job, and what should you use instead?
Show answer
CI jobs usually have no terminal, so -t makes Docker fail with "the input device is not a TTY". Use docker exec web nginx -t instead, with no -i or -t, because the command needs no input.