Wait Command

Difficulty: Intermediate

Question

What does docker wait do, and how would you use it in a script or CI pipeline?

Answer

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.

Why it exists when docker run already blocks

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:

  • Start a database migration container with -d, stream its logs with docker logs -f, then call docker wait to get the result.
  • Run unit and integration test containers in parallel and wait for both.
  • In a deploy script, wait for a one-off seeding job to finish before starting the app.

Reading the exit code

CodeMeaning
0Process exited successfully
1 (or other small numbers)Application-level error, defined by your program
125docker run itself failed, such as a bad flag
126The command could not be executed (permission problem)
127Command not found inside the image
137128 + 9: killed with SIGKILL, often the OOM killer or a docker stop timeout
143128 + 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.

Gotchas

  • It waits forever. If the container never stops, neither does your script. Wrap it with timeout 600 docker wait job in CI.
  • Restart policies. A container with --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.
  • Compose alternatives. 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.
  • Under the hood the CLI calls the Engine API POST /containers/{id}/wait, which also supports a condition parameter (not-running, next-exit, removed) for tools built on the API.
Using docker wait in a CI job. The script starts a job detached, follows its logs, then uses the exit code to pass or fail.
  1. CI script → Docker Engine: docker run -d migrate
  2. Docker Engine → Job container: Start migration process
  3. CI script → Docker Engine: docker wait migrate (blocks)
  4. Job container → Docker Engine: Process exits with code 0
  5. Docker Engine → CI script: Prints exit code 0
  6. CI script → Docker Engine: docker rm migrate, then deploy or fail

Code examples

Run a migration job and fail the pipeline on error

#!/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.

Wait for several containers

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.

Key points

  • docker wait blocks until containers stop and prints one exit code per container
  • Useful for detached jobs, parallel test containers and deploy scripts
  • 137 means SIGKILL (check State.OOMKilled); 143 means SIGTERM
  • Guard it with timeout in CI, and avoid restart policies on one-shot jobs
  • docker compose up --exit-code-from is a Compose-level alternative

Common follow-up questions

  • How do you tell whether exit code 137 was an OOM kill or a manual kill?
  • How does docker stop shut down a container, and what is the grace period?
  • How would you run integration tests that need a database in CI?

Interview tips

  • Explain when docker wait is useful compared to a plain foreground docker run
  • Know the common exit codes, especially 137 and 143; it shows production debugging experience

Concepts covered

docker wait, Exit codes, Container lifecycle, CI scripting, Exit code 137

More Docker & Containers questions