Updated April 14, 2023
Introduction of Docker Stop Container
Docker stop container is used to stop the Docker containers. We can stop one or more Docker containers at the same time by specifying the name or container ID. When we run this command, it first sends SIGTERM to the process running inside the container and waits for some time and it fails to stop the process inside the container then it sends SIGKILL signal to kill the process.
Syntax:
docker stop [OPTIONS] CONTAINER [CONTAINER…]
Options:
We only have one option to specify in this command i.e. ‘–time’ or ‘-t’.
–time, -t: we can control the grace period using this option which means we can specify when Docker daemon should send SIGKILL signal it SIGTERM does not work.
docker stop --help
How does Stop Container Command Work in Docker?
When we run the command ‘docker stop container’ from the Docker CLI, Docker CLI makes an API call to Docker daemon and Docker daemon sends a SIGTERM signal to the process running inside the container and waits for 10 seconds by default and sends the SIGKILL signal to kill the process. If the command executes successfully, it shows container ID as an output on STDOUT i.e. terminal.
Examples of Docker Stop Container
Let’s understand the command with the below examples. Create a few nginx containers and let’s play with it. We have below containers running right now: –
docker ps
- Simply stop a single container using its container ID: –
Syntax:
docker stop <container ID>
Example:
docker stop d207f7679ce5
- Let’s stop a single container using its name: –
Syntax:
docker stop <container_name>
Example:
docker stop jovial_panini
- Now, stop more than one container in the same command using its container ID: –
Syntax:
docker stop <container1 ID><container2 ID> ......
Example:
docker stop 034cb3abe35c11967042830d
- Let’s do the same thing using container name as below: –
Syntax:
docker stop <container1 name><container2 name> .....
Example:
docker stop nifty_bellelated_dubinsky
- Let’s say, we have multiple containers with names starting with the ‘web’ and we want to stop all the containers starting with the name ‘web’. We can use the ‘docker ps’ command with the ‘–filter’ and the ‘–format’ options as below: –
docker stop $(docker ps --filter "name=web" --format="{{.ID}}")
Explanation: In the above example, we can see that there are 3 containers named ‘web1’, ‘web2’ and ‘web3’ are running. We have to use the ‘filter’ option to filter out the containers with the name starting with ‘web’ and then only list the container ID of the containers so that the ‘docker stop’ command will get only the container ID to work on else whole output will be sent to the ‘docker stop’ command and command will fail. We can also use the container name instead of ID as below: –
docker stop $(docker ps --filter "name=web" --format="{{.Names}}")
We can also use the ‘-a’ flag to output all containers whether the containers are running or not, however, we are stopping the container here so that is not required because we can stop the running container only. We can use the ‘-a’ flag while starting multiple containers.
- We can use the same method as above to stop the container running with a specific Docker image. Create few containers with same Docker image and try to delete those at once using below command: –
docker stop $(docker ps --filter "ancestor=nginx" --format="{{.Names}}")
Explanation: In the above example, we can see that we just need to change the filter. By using the different filters, we can stop containers as per our requirement, however, the format must be the ‘Names’ or the ‘ID’.
- We can control the grace period before sending the SIGKILL signal to the process running inside the container using the ‘–time’ or ‘-t’ options as below: –
docker stop -t 20 web2
docker stop -t 20 web1
Explanation: In the above example, we don’t see any difference because the process running under these containers is stopped gracefully. We can use this if any process inside the container takes more than 10 secs to stop so that the docker daemon does not try to kill the process.
Note: We can use this option while stopping multiple containers or querying the Docker daemon as well.
Advantages of Docker Stop Container
Some of the advantages are given below:
- We can use this command to gracefully stop the process running inside the container and we can start the container later using the ‘docker start’ command.
- We can stop one or more containers by specifying the container name or ID in the same command.
- We can also query or filter the container as per our requirement and use the stop command to stop those containers.
Conclusion
The ‘docker stop’ command is easy to remember the command to stop the container. There is another command that is a little bit bigger but does the same thing and that command is ‘docker container stop <container name or ID>’. We can use either of these commands as per our comfort.
Recommended Articles
This is a guide to Docker Stop Container. Here we also discuss the introduction and how does stop container command work in docker? along with different examples and its code implementation. You may also have a look at the following articles to learn more –