Docker Debian Container



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. HAProxy - The Reliable, High Performance TCP/HTTP Load Balancer.

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:

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

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.

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:

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

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.

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.

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

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.

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.

5. Stop a container gracefully

DebianDocker Debian Container

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.

Docker

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 Debian Container

Docker

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.

Debian Docker Container Location

Become a Member for FREE

Docker Create Debian Container

Become a member to get the regular Linux newsletter (2-4 times a month) and access member-only contents.

Join the conversation.