Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Docker commands Guide – docker wait with examples

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

CommandDescription
docker wait my_containerWait for a single container to stop
docker wait container1 container2Wait for multiple containers to stop
docker wait a1b2c3d4e5f6Wait 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 tempRun 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:

  1. Use in automation scripts to manage container completion and handle exit statuses.
  2. Combine with docker logs to inspect why a container stopped.
  3. Monitor exit codes and trigger appropriate actions for success or failure.
  4. Avoid blocking unnecessarily—use it only when you need to wait for container completion.
  5. Combine with docker run --rm for temporary containers that clean up automatically after completion.

Common Errors and Solutions

  1. “No such container”
    → Ensure the container exists and has not been removed. Check with docker ps -a.
  2. “Container is still running” (No immediate output)
    → This is expected behavior; docker wait will block until the container stops.
  3. 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"

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x