Difficulty: Intermediate
What does docker wait do, and how would you use it in a script or CI pipeline?
docker wait <container> blocks until the container stops and then prints its exit code. If you pass several containers, it waits for each one and prints one exit code per line. That makes it a simple way for a script to start a container in the background, carry on with other work, and later find out whether the container's job succeeded.
A foreground docker run already waits and returns the container's exit code. docker wait is useful when the container was started detached (-d), started by someone else, or when you want to start several containers and then collect their results. Typical patterns:
-d, stream its logs with docker logs -f, then call docker wait to get the result.| Code | Meaning |
|---|---|
0 | Process exited successfully |
1 (or other small numbers) | Application-level error, defined by your program |
125 | docker run itself failed, such as a bad flag |
126 | The command could not be executed (permission problem) |
127 | Command not found inside the image |
137 | 128 + 9: killed with SIGKILL, often the OOM killer or a docker stop timeout |
143 | 128 + 15: stopped by SIGTERM, a normal graceful shutdown |
A 137 is the one interviewers love. Check docker inspect -f '{{.State.OOMKilled}}' <id>: true means the memory limit was hit, false means something sent SIGKILL, often docker stop after its 10 second grace period.
timeout 600 docker wait job in CI.--restart always is restarted after it exits, so wait returns the exit code of that run, and the container then comes back. Use one-shot containers without a restart policy for jobs.--rm race. With docker run -d --rm, the container may be removed as soon as it exits. In older Docker versions this could make a later docker wait fail with "No such container", so avoid combining --rm with a separate wait.docker compose up --exit-code-from tests runs a stack and returns the test service's exit code, and docker compose run returns the one-off container's code directly.POST /containers/{id}/wait, which also supports a condition parameter (not-running, next-exit, removed) for tools built on the API.#!/usr/bin/env bash
set -euo pipefail
docker run -d --name migrate --network app_net \
-e DATABASE_URL="$DATABASE_URL" myorg/api:2.3 npm run migrate
# Stream logs in the background so they appear in the CI output
docker logs -f migrate &
# Block (max 10 minutes) until it exits, capture the exit code
code=$(timeout 600 docker wait migrate)
docker rm migrate >/dev/null
if [ "$code" -ne 0 ]; then
echo "Migration failed with exit code $code"
exit "$code"
fi
echo "Migration finished, deploying app"
docker wait turns the container's exit status into something the shell can test. The timeout guards against a job that hangs forever.
docker run -d --name unit myorg/api:2.3 npm test
docker run -d --name lint myorg/api:2.3 npm run lint
docker wait unit lint # one exit code per line, in the order given
Here the unit tests passed and lint failed, so the pipeline should fail.
docker wait, Exit codes, Container lifecycle, CI scripting, Exit code 137