S O F T W A R E S P I C E

Loading

how to stop a docker container

Previously, we discussed running Docker containers. Also, we studied a couple of options used in the docker run command. Here, we’ll learn how to stop a Docker container. Assuming that we have a bunch of running containers and we’d like to stop them. For that purpose, we’ll use the docker stop command which stops a running container. Docker stop command sends SIGTERM(termination signal) to running container process. When a container is stopped, you can’t see it running while executing the docker ps command.

Stopping a Docker container is easy. You should know the container ID or its name to stop it.

Let’s do the drill of stopping Docker containers.

1. The docker stop Command

Here is the basic docker stop command to stop a running container.

docker stop container_ID

OR

docker stop container_Name

When you run the docker ps command, it will show us all the running containers on a server.

[email protected]:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5ead052cb249 httpd:latest "httpd-foreground" 6 minutes ago Up 6 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp webserver2
e8f6e1e8d595 nginx:latest "/docker-entrypoint.…" 23 hours ago Up 17 minutes 0.0.0.0:8090->80/tcp, :::8090->80/tcp webserver1
09e5cee1ee56 registry:2 "/entrypoint.sh /etc…" 12 days ago Up 17 minutes 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp registry

On my VM, I am currently having 3 containers in the running state. 

But if I run docker ps -a command, it will list all the containers even if they are in the exited state.

[email protected]:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5ead052cb249 httpd:latest "httpd-foreground" 4 hours ago Up 4 hours 0.0.0.0:8080->80/tcp, :::8080->80/tcp webserver2
a8ece1749700 hello-world "/hello" 19 hours ago Exited (0) 19 hours ago dreamy_moser
f2508d6d3be3 hello-world "/hello" 19 hours ago Exited (0) 19 hours ago naughty_bell
e8f6e1e8d595 nginx:latest "/docker-entrypoint.…" 26 hours ago Up 4 hours 0.0.0.0:8090->80/tcp, :::8090->80/tcp webserver1
09e5cee1ee56 registry:2 "/entrypoint.sh /etc…" 13 days ago Up 4 hours 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp registry

So, you can see each container has a unique Container ID and a name. 

Now let’s stop the Apache webserver container. 

[email protected]:~$ docker stop 5ead052cb249
5ead052cb249

The command returns the container ID if it was successfully executed.

Let’s check if the container has been stopped?

[email protected]:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e8f6e1e8d595 nginx:latest "/docker-entrypoint.…" 27 hours ago Up 4 hours 0.0.0.0:8090->80/tcp, :::8090->80/tcp webserver1
09e5cee1ee56 registry:2 "/entrypoint.sh /etc…" 13 days ago Up 4 hours 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp registry

You can see, there is no HTTPD container in the running state. We can see it was exited 2 minutes ago. It means the container has been killed and is not in active use. 

[email protected]:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5ead052cb249 httpd:latest "httpd-foreground" 4 hours ago Exited (0) 2 minutes ago webserver2

If we browse localhost on port 8080, it will give us theunable to connecterror. 

The above container’s name is webserver2, right?

Alternatively, we can stop it using the container name.

[email protected]:~$ docker stop webserver2
webserver2

It will also stop the container namely webserver2.

2. Gracefully Stopping a Container

The default time to wait for stopping a container before killing it is 10 seconds. It means, 10 seconds is the time for a graceful shutdown of a container. If a container is small and does not have huge applications running on it, it will be stopped immediately. But a container can take time if it’s large in size having complex and large applications running on it.

Therefore, in such conditions, we can set a time for a container to gracefully shut it down. We will use the --time option of giving time to wait before killing the container. That’s how we can use the command:

[email protected]:~$ docker stop --time 30 5ead052cb249
5ead052cb249

In any given circumstance, the container will be stopped after 30 seconds. For more options, check out the official guide.

3. How to Stop all Running Containers?

If we want to stop all the running containers with a single command, that is possible too. I have the following containers that are in the running and exit state:

[email protected]:~$ docker ps -a
CONTAINER ID   IMAGE           COMMAND                  CREATED        STATUS                      PORTS                                       NAMES
1b7eceecac19   ubuntu          "bash"                   2 hours ago    Exited (0) 18 seconds ago                                               vibrant_galileo
044646e4efe9   ubuntu:latest   "bash"                   2 hours ago    Exited (0) 17 seconds ago                                               myvm
5ead052cb249   httpd:latest    "httpd-foreground"       24 hours ago   Up 16 seconds               0.0.0.0:8080->80/tcp, :::8080->80/tcp       webserver2
a8ece1749700   hello-world     "/hello"                 39 hours ago   Exited (0) 16 seconds ago                                               dreamy_moser
f2508d6d3be3   hello-world     "/hello"                 39 hours ago   Exited (0) 15 seconds ago                                               naughty_bell
e8f6e1e8d595   nginx:latest    "/docker-entrypoint.…"   47 hours ago   Up 14 seconds               0.0.0.0:8090->80/tcp, :::8090->80/tcp       webserver1
09e5cee1ee56   registry:2      "/entrypoint.sh /etc…"   13 days ago    Up 14 seconds               0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   registry

To stop all the above containers, I’ll execute the following command.

docker stop $(docker ps -a -q)

As you can see, it has stopped all containers.

[email protected]:~$ docker stop $(docker ps -a -q)
1b7eceecac19
044646e4efe9
5ead052cb249
a8ece1749700
f2508d6d3be3
e8f6e1e8d595
09e5cee1ee56

So, the above command is a merger of two commands.

  • docker ps -a -q gets the container IDs.
  • docker stop being the parent command will stop the containers with their respective container IDs.  

Now if we want to check the running containers, we will run the docker ps command.

[email protected]:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

There are no containers running at this time.

Also, we can see all the containers in their stopped or exit state.

[email protected]:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1b7eceecac19 ubuntu "bash" 2 hours ago Exited (0) 4 minutes ago vibrant_galileo
044646e4efe9 ubuntu:latest "bash" 2 hours ago Exited (0) 4 minutes ago myvm
5ead052cb249 httpd:latest "httpd-foreground" 24 hours ago Exited (0) 3 minutes ago webserver2
a8ece1749700 hello-world "/hello" 39 hours ago Exited (0) 4 minutes ago dreamy_moser
f2508d6d3be3 hello-world "/hello" 39 hours ago Exited (0) 4 minutes ago naughty_bell
e8f6e1e8d595 nginx:latest "/docker-entrypoint.…" 47 hours ago Exited (0) 3 minutes ago webserver1
09e5cee1ee56 registry:2 "/entrypoint.sh /etc…" 13 days ago Exited (2) 3 minutes ago registry

Again, ps -a -q stands for “process status, -a for all containers, and -q for quit”.

I am repeating the command again.

docker stop $(docker ps -a -q)

Each and every container should stop after executing the above command. 

It’s good to practice this command for your learning purposes but never execute it on production Docker containers until you have a good grip on Docker.

4. A Quick Summary

So, in this article, we learned how to stop a Docker container. You need to pass the container ID or container name with the docker stop command. We also know how to stop all containers through a single command. For your exercise on this command, please go ahead and run some containers. Stop the containers using their IDs and names. Also, stop all containers with the above command we discussed in detail. 

If you want to know more about Docker in the meanwhile, go through the following simple guides:

Let me know if you have any questions in the comments section.

Happy Docking!!!

Leave a Comment

Recent Posts

how to stop a docker container
How to Run a Docker Container
How to Push a Docker Image to a Private Repository
How to push a docker image
How to Tag a Docker Image
Software Spice
Share via
Copy link
Powered by Social Snap