Updated April 3, 2023
Introduction to Docker wait
The ‘docker wait’ is a command that is used to wait or block until one or more containers stop, and then it outputs their exit codes which means you cannot use your terminal if you are running the command on the terminal. In other words, it is used where we have to wait until containers running in detached mode finish their job. It is mostly used in the script to define dependencies; for example, if we have to execute any command after completion of a task running inside a container so we can use this command to wait until the container stops and then run further codes. Docker has built this command on top of the wait command already available in Unix.
Syntax
docker wait CONTAINER [CONTAINER...]
There is no option available in this command.
We can use the ‘–help’ to know more about the command as below: –
docker wait --help
How wait command works in docker?
When we run this command from the command line with multiple containers, it holds or blocks the terminal and continuously checks for the container status and outputs the status code once the container stops. However, if we have specified multiple containers, then it checks one by one, which means it will only check the status of the first container then only go to the next one, even if the second container stops before the first container.
Example of Docker wait
Let’s understand the working of the ‘docker wait’ command with the below examples: –
Scenario: – Simple test the command on the terminal.
1. Create a few containers as below:
docker run -d --name con2 nginx
docker run -d --name con3 redis
docker run -d --name con1 ubuntu sleep 60
docker ps
2. Now, run the ‘docker wait’ command to understand how it works. Run the below command:
docker wait con3 con1 con2
Explanation: – In the above example, We have put the con3 in the first place because this container is going to stop automatically, and we get the status code and cursor start blinking again as shown in the below snapshot: –
3. It will be like this until the other two containers stop working. So, open a new terminal and stop the remaining containers.
docker stop con1
docker stop con2
4. Now, if we go back and check the terminal where the ‘docker wait’ command was running, we can see that we get the prompt now as shown in the below snapshot: –
docker wait con3 con1 con2
Scenario: – Let’s understand how we can use this command in a script with a simple example.
- Let’s assume we have to run a few commands after a container stops. So we have one approach to check the status of the container using while loop with bash and break the loop once the container status changed to excited, but we need to write few extra lines of code that’s lead to a lot of bookkeeping.
In the below example, we are going to run a container in detached mode and check the status of the container; if we get the exit code ‘0’, it will output “Job has been completed successfully”, and if we get the exit code other than ‘0’ then it will output “Job has encountered some issue”.
Here is the code to achieve the above goal: –
#!/bin/sh
#below command will run a container named ‘my-con’ in detach mode and output ‘Hello’ on STDOUT #five times and remove it once container is stopped.
docker container run --rm -d --name my-con alpine ash -c "for i in 1 2 3 4 5; do echo 'Hello'; done"
#we have named the container ‘my-con’ in the above command so we are using that name in below #command, however, we can use contaienr ID but we need to get the id that required to write some #additional lines of code
exit_code="$(docker wait my-con)"
#Compare the status code
if [ $exit_code -eq "0" ]
then
echo “Job has been completed successfully.”
else
echo “Job has encountered some error.”
fi
- Save the above snippet in a file; here it is ‘test-wait.sh’ and add execute permission to the file as below: –
chmod +x test-wait.sh
./test-wait.sh
Explanation: – In the above snapshot, it shows container ID first, and then it says, “Job has been completed successfully,” which means the container stopped or terminated gracefully, and the exit code is 0.
Advantages of Docker wait.
- It helps us reduce the code while writing the script where we have to wait for the container to stop before moving further.
- We can use it to created dependencies in the code, as discussed in the above examples.
Rules and regulation
- If we have specified multiple containers and containers specified at the second place stops before the container specified in the first place, then we don’t get any status code until the first container is stopped.
- The container must run in the detached mode to use wait in the script.
Conclusion
The ‘docker wait’ command is a handy tool while working on any automation project. A similar command is ‘docker container wait’; it also does the same thing. So we can say that the ‘docker wait’ is a short command of ‘docker container wait’.
Recommended Articles
This is a guide to Docker wait. Here we discuss How the wait command works in docker and Examples along with the advantages, rules, and regulations. You may also have a look at the following articles to learn more –