How to Stop Docker Containers

This docker tutorial discusses methods to stop a single docker container, multiple docker containers or all running docker containers at once. You'll also learn to gracefully stop a docker container.

Abhishek Prakash
Abhishek Prakash

Table of Contents

This docker tutorial discusses methods to stop a single docker container, multiple docker containers or all running docker containers at once. You’ll also learn to gracefully stop a docker container.

To stop a docker container, all you have to do is to use the container ID or container name in the following fashion:

docker stop container_ID_or_name

You may also use docker container stop container_id_or_name command but that’s one additional word in the command and it doesn’t provide any additional benefits so stick with docker stop.

But there is more to stopping a docker container that you should know, specially if you are a Docker beginner.

Practical examples for stopping docker container

Stop Docker Containers

I’ll discuss various aspects around stopping a docker container in this tutorial:

  • Stop a docker container
  • Stop multiple docker containers at once
  • Stop all docker containers with a certain image
  • Stop all running docker containers at once
  • Gracefully stopping a docker container

Before you see that, you should know how to get the container name or ID.

You can list all the running docker containers with the docker ps command. Without any options, the docker ps command only shows the running containers.

abhishek@linuxhandbook:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
1bcf775d8cc7        ubuntu              "bash"              8 minutes ago       Up About a minute                       container-2
94f92052f55f        debian              "bash"              10 minutes ago      Up 10 minutes                           container-1

The output also gives you the container name and container ID. You can use either of these two to stop a container.

Now let’s go about stopping containers.

1. Stop a docker container

To stop a specific container, use its ID or name with docker stop command:

docker stop container_name_or_ID

The output should have been more descriptive but it just shows the container name or ID whichever you provided:

abhishek@itsfoss:~$ docker stop 1bcf775d8cc7

You can use the docker stop command on an already stopped container. It won’t throw any errors or a different output.

You can verify if the container has been stopped by using the docker ps -a command. The -a option shows all containers whether they are running or stopped.

abhishek@itsfoss:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
1bcf775d8cc7        ubuntu              "bash"              19 minutes ago      Exited (0) About a minute ago                       container-2
94f92052f55f        debian              "bash"              29 minutes ago      Up 29 minutes                                   container-1

If the status is Exited, it means the container is not running any more.

2. Stop multiple docker containers

You can stop multiple docker containers at once as well. You just have to provide the container names and IDs.

docker stop container_id_or_name_1 container_id_or_name_2 container_id_or_name_3

As previously, the output will simply show the name or ID of the containers:

abhishek@itsfoss:~$ docker stop container-1 container-2 container-3

3. Stop all containers associated with an image

So far what you saw stopping containers by explicitely mentioning their name or ID.

What if you want to stop all running containers of a certain docker image? Imagine a scenario where you want to remove a docker image but you’ll have to stop all the associated running containers.

You may provide the container names or IDs one by one but that’s time consuming. What you can do is to filter all the running containers based on their base image.

Just replace the IMAGE_NAME by your docker image name and you should be able to stop all running containers associated with that image.

docker ps -q --filter ancestor=IMAGE_NAME | xargs docker stop

The option -q shows only the container ID. Thanks to the wonderful xargs command, these container IDs are piped to the docker stop as argument.

4. Stop all running docker containers

You may face a situation where you are required to stop all running containers. For example if you want to remove all containers in Docker, you should stop them beforehand.

To do that, you can use something similar to what you saw in the previous section. Just remove the image part.

docker ps -q | xargs docker stop

5. Stop a container gracefully

To be honest, docker stops a container gracefully by default. When you use the docker stop command, it gives the container 10 seconds before forcefully killing it.

It doesn’t mean that it always takes 10 seconds to stop a container. It’s just that if the container is running some processes, it gets 10 seconds to stop the process and exit.

Docker stop command first sends the SIGTERM command. If the conainer is stopped in this period, it sends the SIGKILL command. A process may ignore the SIGTERM but SIGKILL will kill the process immediately.

I hope you know the difference between SIGTERM and SIGKILL.

What is SIGTERM? What’s the difference between SIGKILL & SIGTERM?
Both SIGTERM and SIGKILL are used for killing a process in Linux. But you should prefer using SIGTERM. Here’s why!

You may change this grace period of 10 seconds with the -t option. Suppose you want to wait 30 seconds before stopping the container:

docker stop -t 30 container_name_or_id

In the end…

I think that this much information covers the topic very well. You know plenty of things about stopping a docker container.

Stay tuned for more docker tips and tutorials. If you have questions or suggestions, please let me know in the comment section.

Join the conversation.