You are reading the article How Wait Command Works In Docker With Examples? updated in October 2023 on the website Cersearch.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 How Wait Command Works In Docker With Examples?
Introduction to Docker waitThe ‘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.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
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 waitLet’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 ps
2. Now, run the ‘docker wait’ command to understand how it works. Run the below command:
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 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: –
if [ $exit_code -eq “0” ] fi
Save the above snippet in a file; here it is ‘test-wait.sh’ and add execute permission to the file as below: –
./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.
ConclusionThe ‘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 ArticlesYou're reading How Wait Command Works In Docker With Examples?
Update the detailed information about How Wait Command Works In Docker With Examples? on the Cersearch.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!