Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

โ€œInvest in yourself โ€” your confidence is always worth it.โ€

Explore Cosmetic Hospitals

Start your journey today โ€” compare options in one place.

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...]
Code language: CSS (css)

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."
Code language: PHP (php)

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"
Code language: JavaScript (javascript)

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)
Code language: JavaScript (javascript)

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
Code language: PHP (php)

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."
Code language: PHP (php)

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"
Code language: JavaScript (javascript)

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
Iโ€™m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at <a href="https://www.cotocus.com/">Cotocus</a>. I share tech blog at <a href="https://www.devopsschool.com/">DevOps School</a>, travel stories at <a href="https://www.holidaylandmark.com/">Holiday Landmark</a>, stock market tips at <a href="https://www.stocksmantra.in/">Stocks Mantra</a>, health and fitness guidance at <a href="https://www.mymedicplus.com/">My Medic Plus</a>, product reviews at <a href="https://www.truereviewnow.com/">TrueReviewNow</a> , and SEO strategies at <a href="https://www.wizbrand.com/">Wizbrand.</a> Do you want to learn <a href="https://www.quantumuting.com/">Quantum Computing</a>? <strong>Please find my social handles as below;</strong> <a href="https://www.rajeshkumar.xyz/">Rajesh Kumar Personal Website</a> <a href="https://www.youtube.com/TheDevOpsSchool">Rajesh Kumar at YOUTUBE</a> <a href="https://www.instagram.com/rajeshkumarin">Rajesh Kumar at INSTAGRAM</a> <a href="https://x.com/RajeshKumarIn">Rajesh Kumar at X</a> <a href="https://www.facebook.com/RajeshKumarLog">Rajesh Kumar at FACEBOOK</a> <a href="https://www.linkedin.com/in/rajeshkumarin/">Rajesh Kumar at LINKEDIN</a> <a href="https://www.wizbrand.com/rajeshkumar">Rajesh Kumar at WIZBRAND</a> <a href="https://www.rajeshkumar.xyz/dailylogs">Rajesh Kumar DailyLogs</a>

Related Posts

Docker Tutorials: Docker Image – Understanding Dockerfiles instructions & options

Hereโ€™s a step-by-step tutorial for Dockerfile, including explanations and examples for each major command. Dockerfile Tutorial A Dockerfile is a text file containing instructions to build a…

Read More

Docker Tutorials: Docker Image – Example and Sample Programs of Dockerfile

Reference Rajesh Kumar Iโ€™m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at Cotocus. I share tech blog at DevOps School, travel stories…

Read More

Docker Tutorials: Installation and Configurations

Docker Installation in Centos/RHEL Method -1: How to install Docker Community Edition via YUM? Step 1 – Install required packages. yum-utils provides the yum-config-manager utility, and device-mapper-persistent-data…

Read More

Docker Tutorials: How to Install Docker in Ubuntu?

Install Docker Engine in Ubuntu NOTE – All commands you must run as root user or add a current user into a linux group name called “docker”…

Read More

Docker Lab, Excercise & Assignment – 7 – Docker Volume

Below is a very detailed tutorial and lab manual for learning Docker Volumes, using the Ubuntu image for practical, hands-on labs. This covers all major types of…

Read More

Docker Lab, Excercise & Assignment – 4 – Docker Networking

Hereโ€™s an in-depth, step-by-step tutorial and lab manual for Docker Networkingโ€”starting from basics, covering all core concepts, and providing a hands-on guide to every feature and command….

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x