Docker exit codes and docker wait
Quick answer
An exit code is a number reporting how a program finished. Zero commonly means success; a nonzero code commonly means some failure or special condition. Docker records the exit code of a container process, which can help diagnose a job that has ended.
Before you run commands
Use an approved disposable Linux-container Docker lab. Confirm the effective context and permissions; use a reviewed, locally preloaded busybox:1.37 image whose digest/platform the lab owner has recorded. This tag is mutable. Do not download an image automatically; --pull=never requires it locally. The new containers use --network none, with no host mounts or published ports; the CLI still connects to the selected engine. Verify the approved image tag still resolves to the recorded image before creating containers. Check docker ps -a for all names used below. If any is occupied, choose fresh names consistently. Run commands one at a time and stop on any unexpected failure; never act on resources you did not create. Expected results are predictions, not measured execution.
How it works
Here sh -c executes exit 7, deliberately ending with code 7. The launch is detached so a successful CLI launch is not evidence that the job succeeds. docker wait prints the job’s exit code as output; that printed value is distinct from the shell status of the wait command itself. Inspect the stored value with .State.ExitCode. In attached mode, a successfully launched docker run normally returns the program’s status; launch failures can instead have Docker-specific codes. Interpret the code with the application’s own documentation.
docker run -d --pull=never --network none --name bm02-exit busybox:1.37 sh -c 'exit 7'docker wait bm02-exitdocker inspect --format '{{.State.ExitCode}}' bm02-exitdocker rm bm02-exitBoth wait and inspect should print 7 if the job was launched correctly.
docker run -d --pull=never --network none --name bm02-ok busybox:1.37 sh -c 'exit 0'docker wait bm02-okdocker rm bm02-okExpect 0 from wait.
Common mistake
An exited container is not necessarily broken: batch jobs are supposed to finish. A nonzero result alone does not identify the underlying cause; combine it with logs, the command, and application behavior.
Try it yourself
Create another detached job with exit 3 and predict wait output. Explain why the fact that Docker printed an ID does not prove success. Use a fresh unused name and the same reviewed image and isolation flags; wait for completion and remove only the container you created. If running without the lab, write the predicted code instead.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Docker exit codes and docker wait — knowledge check
Apply the lifecycle concepts and interpret the lab behavior.
The shell runs exit 7 inside a container. What should docker wait print?
Does an exited state always indicate a problem?
What should you use to interpret a nonzero code?