Here’s a complete tutorial on docker wait
, covering what it does, examples, and use cases.
What is docker wait
?
docker wait
is a Docker command that blocks until a container stops and then returns the container’s exit code. It’s useful for synchronizing tasks, monitoring container completion, and handling exit status in scripts or automation processes.
Key Features:
- Waits for one or more containers to stop.
- Returns the exit status code of the container’s main process.
- Ideal for scripting and automation, especially when you need to act based on the container’s success or failure.
Basic Syntax
docker wait CONTAINER [CONTAINER...]
Arguments:
CONTAINER
: The name or ID of the container. You can specify multiple containers.
Examples of docker wait
1. Wait for a Single Container to Stop
docker wait my_container
This blocks until my_container
stops and then returns its exit code.
2. Wait for a Container by ID
docker wait a1b2c3d4e5f6
This waits for the container with ID a1b2c3d4e5f6
to stop.
3. Wait for Multiple Containers
docker wait container1 container2 container3
This waits for all specified containers to stop and returns their exit codes in the order they are specified.
Example Output:
0
1
137
4. Wait for a Container in a Script
#!/bin/bash
docker run --name my_temp_container ubuntu sleep 10 &
docker wait my_temp_container
echo "my_temp_container has stopped."
This script waits for my_temp_container
to stop and then prints a message.
5. Check the Exit Code of a Container
exit_code=$(docker wait my_container)
echo "Container exited with code: $exit_code"
This captures the container’s exit code in a variable for further use.
6. Wait for the Last Created Container
docker wait $(docker ps -lq)
This waits for the last created container to stop.
7. Use in a CI/CD Pipeline
docker run --name test_container my_app:latest
docker wait test_container
if [ $? -eq 0 ]; then
echo "Container completed successfully."
else
echo "Container failed."
fi
In this example, docker wait
checks the exit code and performs an action based on success or failure.
8. Handle Parallel Containers
docker run --name job1 ubuntu sleep 5 &
docker run --name job2 ubuntu sleep 10 &
docker wait job1 job2
echo "Both containers have stopped."
This waits for both containers (job1
and job2
) to stop.
Use Cases for docker wait
1. Synchronization in Automation Scripts
- Wait for containers to finish before moving to the next task.
- Example: Run tests in a container and only proceed if they pass.
2. Monitoring Container Completion
- Monitor batch jobs or background tasks and act based on their exit status.
- Example: Process large data files in a container and only continue if the job completes successfully.
3. CI/CD Pipelines
- Use
docker wait
to ensure that tests or builds inside containers have finished before deploying. - Example: In a CI pipeline, wait for a containerized test runner to finish and act on its result.
4. Error Handling and Recovery
- Check the exit status code of a container to detect failures and trigger recovery actions.
- Example: Restart a service if a container exits with a non-zero code.
5. Running Multiple Dependent Containers
- Wait for a dependent service to stop before starting the next one.
- Example: Wait for a database migration container to finish before starting the main app.
List of Common docker wait
Commands
Command | Description |
---|---|
docker wait my_container | Wait for a single container to stop |
docker wait container1 container2 | Wait for multiple containers to stop |
docker wait a1b2c3d4e5f6 | Wait for a container by its ID |
docker wait $(docker ps -lq) | Wait for the last created container |
docker run --name temp ubuntu sleep 5 & docker wait temp | Run a container and wait for it to finish |
exit_code=$(docker wait my_container) | Capture the container’s exit code |
Best Practices for Using docker wait
:
- Use in automation scripts to manage container completion and handle exit statuses.
- Combine with
docker logs
to inspect why a container stopped. - Monitor exit codes and trigger appropriate actions for success or failure.
- Avoid blocking unnecessarily—use it only when you need to wait for container completion.
- Combine with
docker run --rm
for temporary containers that clean up automatically after completion.
Common Errors and Solutions
- “No such container”
→ Ensure the container exists and has not been removed. Check withdocker ps -a
. - “Container is still running” (No immediate output)
→ This is expected behavior;docker wait
will block until the container stops. - Exit Code is Non-Zero
→ Check the container logs (docker logs my_container
) to identify the cause of failure.
Combining docker wait
with Other Commands
Run, Wait, and Remove a Container
docker run --rm --name temp_container ubuntu sleep 10 &
docker wait temp_container
Log the Output and Check Exit Code
docker run --name my_app my_image
docker logs my_app
exit_code=$(docker wait my_app)
echo "Exit code: $exit_code"
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND