Stopping a Docker Container

In this guide, we will show you how to stop a Docker container using the terminal.

Docker Stop Container


There are times when you may no longer want a particular Docker container running on your system. In that case, you will want to stop the container.

Stopping a container allows it to shut down gracefully. A graceful shutdown gives the process time to stop safely and save any necessary data. It is very similar to running the shutdown command on your system.

To shut down a Docker container all you need is access to the terminal and a user that can access the Docker daemon. If your user isn’t a part of the “docker” group, you will need to instead rely on the superuser.

Of course, if you aren’t a fan of using the terminal, there are other ways to manage the Docker containers on your system. Software like Portainer and Dockge allow you to stop your containers with just a couple of clicks.

How to Stop a Docker Container

Over the next few steps, we will show and explain how to stop a Docker container from the terminal, including how to get the required Docker name or ID.

If you are following this guide from a desktop system, you can often open the terminal by pressing CTRL + ALT + T on your keyboard.

Getting the Name or ID of the Container You Want to Stop

1. To stop a running Docker container, you will need to know its name or ID. These values indicate to the Docker command-line tool what container it needs to stop.

Luckily, Docker makes listing running containers as simple as running the following command.

docker container list

2. After using the above command, you should be given a list of containers currently running on your system.. In our case, we have two containers running, Syncthing and Dockge.

You will be interested in the container ID and the container name. Both are unique values that can identify a specific container and be used to stop them.

For this example, we will be using the container name “pimyubu-dockge-1” to stop our Dockge container. However, we could also use the ID “e29e30d5beb1” instead if we wanted.

CONTAINER ID   IMAGE                                                     COMMAND                  CREATED        STATUS                  PORTS                                       NAMES
de5d14be1adb   syncthing/syncthing                                       "/bin/entrypoint.sh …"   23 hours ago   Up 23 hours (healthy)                                               syncthing
e29e30d5beb1   louislam/dockge:1                                         "/usr/bin/dumb-init …"   24 hours ago   Up 24 hours (healthy)   0.0.0.0:5001->5001/tcp, :::5001->5001/tcp   pimyubu-dockge-1

Using the Terminal to Stop a Docker Container

3. Stopping a Docker container from the terminal is as simple as using the following command. With this command all you need to do is specify the ID or name of a container.

When you run this command, Docker will send a “SIGTERM” to a container’s main process, allowing it to shut down gracefully. If the container doesn’t stop after a grace period, Docker will send a “SIGKILL,” which will terminate the container regardless of what is occurring.

By default, the grace period before a terminate signal is sent is 10 seconds. In most cases, this should be more than enough time for a Docker container to stop gracefully.

docker container stop <CONTAINER>

You can also write this command as we have shown below. This is an alias of the above command, but Docker is slowly pushing more people to use the full command instead.

docker stop <CONTAINER>

4. For this example, let us stop the Docker container named “pimyubu-dockge-1“.

To stop this container, we must use “docker container stop” followed by the containers name “pimyubu-dockge-1“, as we have showcased below.

docker container stop pimyubu-dockge-1

Once the above command has run, it will print out the name or container that you passed in.

pimyubu-dockge-1

5. To verify the container has been stopped, we can get Docker to list all the running containers again.

docker container list

Below, you can see that the Dockge container is no longer running on our system, and we have successfully stopped it.

Please note that if a container has its restart setting set to “always,” it will automatically restart the next time the Docker daemon starts.

CONTAINER ID   IMAGE                                                     COMMAND                  CREATED        STATUS                  PORTS                                       NAMES
de5d14be1adb   syncthing/syncthing                                       "/bin/entrypoint.sh …"   23 hours ago   Up 23 hours (healthy)                                               syncthing

Conclusion

If you have gotten to this point in the tutorial, you should now know how to stop a Docker container while using the terminal.

Docker makes most tasks, such as stopping containers, really simple and easy to remember. The hardest part is ensuring you get the ID or name of the container you want to stop.

Please feel free to comment below if you have had any issues getting a container to stop on your system.

If you found this quick guide helpful, you should explore the many other Docker guides. We also have a guide that shows you how you can easily kill all running containers on your system.

Leave a Reply

Your email address will not be published. Required fields are marked *