docker stop all containers [SOLVED]


Written by - Ashwini Kanitkar
Reviewed by - Deepak Prasad

Docker is a popular open-source platform for creating, deploying, and managing applications in containers. Containers are lightweight, portable, and efficient units of software that package an application and its dependencies, allowing it to run consistently across different environments.

When working with Docker, you may have several containers running simultaneously. However, there may be instances where you need to stop all containers at once, such as when you want to perform maintenance or update your system. Stopping all containers ensures that all running applications are halted gracefully and their resources are freed up.

In this context, the "docker stop" command is used to stop one or more containers. However, when you have a large number of containers running, stopping each container individually can be time-consuming and tedious. In such cases, you can use the "docker stop" command with a specific parameter to stop all running containers at once.

This article will discuss how to use the "docker stop" command to stop all running containers in your Docker environment, and the implications of stopping containers on your running applications.

 

Docker lifecycle

In this section, let us understand the basics of Docker. The lifecycle of a Docker container involves several stages, including creation, running, pausing, stopping, and deletion.

  1. Created: The first stage is the creation of a Docker container, which involves creating an image using a Dockerfile or an existing image. The container is then created using the "docker create" command, but it is not yet running.
  2. Running: Once the container is created, it can be started using the "docker start" command. The container is now running, and the application within it is available to accept requests.
  3. Paused: The container can be paused using the "docker pause" command. This will pause the container's processes and freeze its state. This is useful when you need to temporarily free up resources, but you want to preserve the container's state.
  4. Stopped: The container can be stopped using the "docker stop" command. This will send a signal to the container to stop running and allow it to gracefully shut down. The container's state is saved, and it can be restarted later using the "docker start" command.
  5. Deleted: Finally, the container can be deleted using the "docker rm" command. This will remove the container from the Docker environment and free up any resources it was using. Note that the container must be stopped before it can be deleted.

docker stop all containers [SOLVED]

 

docker stop command

The "docker stop" command is used to stop one or more running Docker containers gracefully. It sends a signal to the container to stop running, and allows it to perform any necessary cleanup operations before shutting down.

The syntax for the "docker stop" command is as follows:

docker stop [OPTIONS] CONTAINER [CONTAINER...]
  • "OPTIONS" are optional flags that can be used to modify the behavior of the command. Some common options include:
  • -t, --time : Sets a timeout period in seconds for the container to stop before it is forcibly killed.
  • "CONTAINER" is the name or ID of the container(s) to stop. You can specify multiple container names or IDs separated by a space.

For example, to stop a container named "webapp", you would use the following command:

docker stop webapp

This would send a signal to the "webapp" container to stop gracefully.

If you want to stop multiple containers at once, you can specify their names or IDs separated by a space:

docker stop container1 container2 container3

Alternatively, you can stop all running containers at once using the following command:

docker stop $(docker ps -a -q)

By default, it waits for 10 seconds in order to wait before stopping the container. However you can increase it or decrease it depending on the need.

 

How to exclude some containers from stop command

To conditionally exclude some containers when stopping all running containers with the command docker stop $(docker ps -a -q), you can use the grep option to filter out containers based on certain criteria.

One way to do this is by using the grep command to exclude specific container IDs. For example, to exclude containers with IDs CONTAINER_ID_1 and CONTAINER_ID_2, you can use the following command:

docker ps -a --format "table {{.ID}}" | tail -n +2 | grep -v CONTAINER_ID_1 | grep -v CONTAINER_ID_2 | xargs docker stop

Here's how this command works:

  • The docker ps -a --format "table {{.ID}}" command formats the output of the docker ps -a command to display only the container IDs in a tabular format.
  • The tail -n +2 command excludes the first line of the output, which is the header row.
  • The first grep -v command excludes lines containing the CONTAINER_ID_1.
  • The second grep -v command excludes lines containing the CONTAINER_ID_2.
  • The xargs docker stop command stops all the containers that remain in the output.

This command should stop all containers except for those with IDs CONTAINER_ID_1 and CONTAINER_ID_2 and exclude the header row.

 

Difference between docker kill and docker stop

The "docker kill" and "docker stop" commands are both used to stop Docker containers, but they have different behaviors.

The "docker kill" command is used to forcibly stop a running container. It sends a signal to the container to terminate immediately, without allowing it to perform any cleanup operations or save its current state. This can lead to data loss or other unexpected behavior if the container was in the middle of performing critical operations when it was killed.

The syntax for the "docker kill" command is similar to that of the "docker stop" command:

docker kill [OPTIONS] CONTAINER [CONTAINER...]

The "OPTIONS" and "CONTAINER" arguments are the same as for the "docker stop" command.

The "docker stop" command, on the other hand, is used to gracefully stop a running container. It sends a signal to the container to stop running and allows it to perform any necessary cleanup operations before shutting down. This is the preferred way to stop a container, as it allows the container to save its current state and perform any necessary cleanup before exiting.

The syntax for the "docker stop" command is:

docker stop [OPTIONS] CONTAINER [CONTAINER...]

The "OPTIONS" and "CONTAINER" arguments are the same as for the "docker kill" command.

 

Let us understand this with a few examples in the next sections. We have this one container in Running state which we check using the command docker ps

$ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
7f91250f318d   nginx     "/docker-entrypoint.…"   25 seconds ago   Up 24 seconds   80/tcp    vigorous_goldstine

Now let us stop the nginx docker container with the ID 7f91250f318d using the command below

$ docker stop 7f91250f318d
7f91250f318d

Now let us confirm if the docker container stopped by listing the docker containers

$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

Here you can observe that no containers are running and hence the stop worked successfully.

 

Example to stop the docker container with -t option.

Let us re run the nginx docker with below command

$ docker run -d nginx
11b92fdc1c57daffdc0e50d6781f78e537bcf2ee0d3a8010416773cb6b3eddd6

Check the list of docker containers using docker ps

$ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
11b92fdc1c57   nginx     "/docker-entrypoint.…"   18 seconds ago   Up 15 seconds   80/tcp    thirsty_engelbart

Now, let us stop the docker container by specifying the -t option which waits for “t” seconds before killing it.

$ docker stop -t 10 11b92fdc1c57

 

Conclusion

The "docker stop" command is a Docker command that is used to stop one or more running containers gracefully. It is used to send a signal to the container(s) to stop running and allows them to perform any necessary cleanup operations before shutting down.

To stop a single container, you can use the command "docker stop <container-name>" where "<container-name>" is the name or ID of the container you wish to stop.

To stop multiple containers at once, you can specify their names or IDs separated by a space: "docker stop container1 container2 container3".

You can also stop all running containers at once by using the command "docker stop $(docker ps -q)". This command lists all running containers using "docker ps", and the "-q" flag is used to output only the container IDs. The result is then passed as an argument to the "docker stop" command, which stops all running containers at once.

The "docker stop" command can also accept options, such as a timeout period in seconds before the container is forcibly stopped using the "-t" or "--time" flag.

 

References

https://docs.docker.com/engine/reference/commandline/stop/

 

Ashwini Kanitkar

She brings many years of engineering experience in DevOps and Linux, specializing in CICD with Jenkins, AWS services, Docker, Kubernetes, and Terraform. She is also proficient in Shell and Python scripting. You can connect with her on <a href="https://www.facebook.com/golinuxcloud/" target="_blank" rel="nofollow noopener">Facebook</a> page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment