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 the “unable to connect” error.
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
.