---
title: "Use Docker Stop Containers Without Screwing Things Up!"
description: "Learn how to use Docker stop containers to properly stop a Docker container or kill a Docker container!"
canonical: "https://adamtheautomator.com/docker-stop-containers/"
---

# Use Docker Stop Containers Without Screwing Things Up!

> Learn how to use Docker stop containers to properly stop a Docker container or kill a Docker container!

Source: https://adamtheautomator.com/docker-stop-containers/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Use Docker Stop Containers Without Screwing Things Up!](https://adamtheautomator.com/wp-content/uploads/2021/08/Use-Docker-Stop-Containers-Without-Screwing-Things-Up.jpg)

# Use Docker Stop Containers Without Screwing Things Up!

[![](https://secure.gravatar.com/avatar/f08b754bc0dce1c76685f6afa93185ecfb6f861fd14f993286e06bba69eaeb8b?s=192&d=mm&r=g)Adam Listek](https://adamtheautomator.com/author/alistek/)23 August 20216 min. read

Categories: [DevOps](/category/devops/)

Tags:[Docker](/tag/docker/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Ending Containers with the docker stop Containers Command](#ending-containers-with-the-docker-stop-containers-command)
*   [Stopping a Container Using Process Signals](#stopping-a-container-using-process-signals)
*   [Exiting Containers Immediately with docker kill](#exiting-containers-immediately-with-docker-kill)
*   [Stopping and Removing a Container with docker rm](#stopping-and-removing-a-container-with-docker-rm)
*   [Stopping Docker Containers with Docker Compose](#stopping-docker-containers-with-docker-compose)
*   [Conclusion](#conclusion)

Having trouble with Docker stop containers? Finding the correct way to stop a container is difficult with the ability to create many different containers. In this article, you will learn Docker commands to stop a container or all containers.

Not a reader? Watch this related video tutorial!

**_Not seeing the video? Make sure your ad blocker is disabled._**

Read on to learn more!

## Prerequisites

To follow along with this tutorial, be sure you have the following:

*   Windows 10 – The tutorial uses Windows to run Docker, but you can also apply the same general steps to Linux or macOS.
*   **[Docker Desktop](https://www.docker.com/products/docker-desktop)** – This tutorial uses version 3.5.2. The **[Docker engine](https://docs.docker.com/engine/install/)** is available if you’re on Linux as the GUI is not.
*   A running Docker container or containers.

Related:[How to Install and Use Docker on Ubuntu (In the Real World)](https://adamtheautomator.com/docker-ubuntu/)

## Ending Containers with the docker stop Containers Command

Let’s get started with the first command: the `docker stop` containers command and how it accomplishes stopping containers.

For example, perhaps you need to stop a running container with the name `mystifying_hofstadter` and an ID of `fb66ed502096`. As shown in the example below, stop the container with the `docker stop` containers command.

```bash
# Stop the container via the name
docker stop mystifying_hofstadter
# Stop the container via the ID (same container)
docker stop fb66ed50209
```

![Stopping a single Docker container via the container name or container ID.](https://adamtheautomator.com/wp-content/uploads/2021/08/Untitled-2021-08-22T114258.139.png)

Stopping a single Docker container via the container name or container ID.

What if you have multiple running containers, and you would like to stop those? The `docker stop` containers command supports stopping multiple container IDs, one at a time.

First, list all containers via the [`docker ps`](https://docs.docker.com/engine/reference/commandline/ps/) command and pass the container IDs to the `docker stop` containers command, as shown below.

```bash
# List all running containers
docker ps
# Stop the following Docker containers via the container ID
docker stop a427a21c5558 e9dbd816f24c 31269ef5999e
```

![Listing all running containers and then stopping them](https://adamtheautomator.com/wp-content/uploads/2021/08/Untitled-2021-08-22T114514.532.png)

Listing all running containers and then stopping them

What if you wish to stop all running containers without retrieving the container IDs manually and passing those to `docker stop`? The `docker ps -q` command will accomplish that. The `-q` parameter of `docker ps` command outputs only the IDs of the containers.

```powershell
# Create three containers. A semi-colon separates each command
docker run -d -t ubuntu; docker run -d -t ubuntu; docker run -d -t ubuntu
# List all running containers
docker ps -q
# Retrieve all running container IDs and pass those to docker stop
docker stop $(docker ps -q)
```

![Stopping multiple Docker containers at once.](https://adamtheautomator.com/wp-content/uploads/2021/08/Untitled-2021-08-22T114616.195.png)

Stopping multiple Docker containers at once.

> _The `$(docker ps -q)` construct above is a sub-expression in both Bash on Linux and PowerShell on Windows. A sub-expression instructs the shell to process one set of commands, `docker ps -q`, first and then return the set of results to the original command `docker stop`._

The `docker stop` containers command uses the SIGTERM signal. Linux signals inform a process of an event, but it is up to the process to decide how to act on a given signal.

When using the SIGTERM signal, Docker uses the signal to gracefully stop a process by waiting for a default `10` seconds before sending the SIGKILL signal, which terminates the process immediately.

## Stopping a Container Using Process Signals

You learned about Docker’s default behavior (stop signal), with the `docker stop` containers command, when stopping a container. But you may not want the default behavior. Instead, you can instruct Docker to use alternative stop signals.

With the `docker run` commands `--stop-signal` flag, you can specify the stop signal when starting the container. This flag sets the signal that will be sent to the running container when you want it to stop.

Related:[Related: How to Run Startup Commands in Docker Containers](https://adamtheautomator.com/dockerfile-entrypoint/)

In the example below, the `--stop-signal` flag takes the value `SIGQUIT`, which tells Docker to send the SIGQUIT signal when it is time to stop the container. The example also uses the following parameters:

*   `d` – Runs the container in detached (background) mode. The detached mode will not allow the container to be immediately interacted with by the user since the process is running in the background.
*   `t` – allocates a Pseudo-TTY (PTY) console, which emulates a real terminal console in the container and stops the Ubuntu container from immediately exiting. The Ubuntu container in the example requires a PTY console to stay active in the background.
*   “image name” – What Docker image to provision the container from, in this case, the `ubuntu` image.

```powershell
docker run -d -t --stop-signal SIGQUIT ubuntu
```

![Specifying SIGQUIT as the signal when starting and then stopping a Docker container.](https://adamtheautomator.com/wp-content/uploads/2021/08/Untitled-2021-08-22T114756.388.png)

Specifying `SIGQUIT` as the signal when starting and then stopping a Docker container.

> _You can also specify the stop signal using a [Dockerfile](https://docs.docker.com/engine/reference/builder/), which tells Docker how to build a container. You can add an entry to the Dockerfile specifying the stop signal with `STOPSIGNAL SIGQUIT`, for example. You can switch the `SIGQUIT` with any stop signal you would like._

## Exiting Containers Immediately with `docker kill`

There will be times when you will want to exit containers without allowing them a grace period. The [`docker kill`](https://docs.docker.com/engine/reference/commandline/kill/) command immediately stops a container.

The example below passes the container with ID `2aa318273db5` to `docker kill`. The container will immediately exit via the SIGKILL signal. The container will not be allowed to a graceful exit. As a result of the command, the container will exit, and the ID of the container will be shown.

```powershell
docker kill 2aa318273db5
```

> _Docker kill all containers with the command `docker kill $(docker ps -q)`. Notice that `(docker ps -q`) is a sub-expression here._

## Stopping and Removing a Container with `docker rm`

By default, when you create a container, that container is built the same as before: it is idempotent. Removing a container will have minimal impact, and with that in mind, `docker rm` can force-stop and remove a container. Docker issues a `SIGKILL` signal to the main process and removes the container from the list of available containers on your Docker installation.

> _If the container must keep the changed internal files or its state must be kept, then removing a container will destroy those changes. Beware of potential screw-ups!_

In the following example, perhaps you have started a container that was assigned the name `kind_galileo`. Before removing a container, the container must be stopped, as shown below. The `docker ps -a` command is displaying the status of `Exited` for the example container.

![Listing all containers, including stopped containers.](https://adamtheautomator.com/wp-content/uploads/2021/08/Untitled-2021-08-22T115025.240.png)

Listing all containers, including stopped containers.

Now that the container is stopped issuing the `docker rm` command will remove the container entirely.

```bash
docker rm kind_galileo
```

Instead of stopping the container to then remove the container, the combination of `--force` and `docker rm` will stop and remove the container in a single operation. The container, `laughing_elion`, will no longer be available to run.

```powershell
docker rm --force laughing_elion
```

> _If you do not specify the `--force` command, you will encounter the error message `Error response from daemon: You cannot remove a running container...`_

![Stopping and removing a container.](https://adamtheautomator.com/wp-content/uploads/2021/08/Untitled-2021-08-22T115157.962.png)

Stopping and removing a container.

## Stopping Docker Containers with Docker Compose

Docker commands are typically used on a single container, but you can create a service with multiple containers working in concert with each other. The Docker Compose tool allows you to configure a service comprised of multiple containers to work together.

As the containers within a service are designed to work together, it is best to stop the entire service itself gracefully instead of any individual container.

If you have a service running with multiple containers, `[[docker-compose stop](https://docs.docker.com/compose/reference/stop/)]` stops the service without removing the containers or the service. Follow along below to create a Docker Compose service, start the service, and ultimately stop the service gracefully.

1\. Create a directory to store your configuration file. In this example, the directory _C:\\Articles\\Ubuntu_ will store the file.

```bash
mkdir C:\Articles\Ubuntu
```

2\. Next, create the file _docker-compose.yml_ file, containing the following configuration. The below Docker Compose file creates a service with two Ubuntu containers, and when brought up, the containers do not immediately exit.

```yaml
# The specification version of docker-compose
version: "3.9"
# The collection of applications composing this service
services:
  # First Ubuntu Container
  container1:
    image: ubuntu
    # Same as the -t option and allocates a Psuedo-TTY, so the container does not immediately exit.
    tty: true
  # Second Ubuntu Container
  container2:
    image: ubuntu
    tty: true
```

3\. In a terminal session, navigate to your custom Docker configuration file directory, which in this example is _C:\\Articles\\Ubuntu_. Run the `docker-compose up -d` to generate and start the service created in the step before in the background as indicated by the `-d` option.

![Starting a Docker Compose service.](https://adamtheautomator.com/wp-content/uploads/2021/08/Untitled-2021-08-22T115628.071.png)

Starting a Docker Compose service.

4\. In the same terminal session and directory, issue the command `docker-compose stop` to end the service and containers gracefully.

![Stopping a Docker Compose service.](https://adamtheautomator.com/wp-content/uploads/2021/08/Untitled-2021-08-22T115718.749.png)

Stopping a Docker Compose service.

> _The `docker-compose` command, like the `docker` command, also has the `rm --force` and `kill` parameters. The `docker-compose` parameters work on the service instead of a single container. The `docker-compose kill` command also takes an option `-s` option to specify the stop signal._

## Conclusion

Docker containers are a powerful tool to create unique and segmented environments for development and production use. In this article, you have learned several ways Docker stops one or more, even all, containers. You also learned about the default stop signal Docker uses and how to change it.

With this knowledge, you now know the ins and outs of stopping Docker containers and how not to screw up when it is time to stop running containers. How do you plan to stop your containers from now on, having this newfound knowledge?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fdocker-stop-containers%2F&text=Use%20Docker%20Stop%20Containers%20Without%20Screwing%20Things%20Up!)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fdocker-stop-containers%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fdocker-stop-containers%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/02/featured_image-13.webp)

### [Azure Container Apps: Instant Preview Environments per PR](/build-ephemeral-preview-environments-every-pr/)

Build isolated preview environments for every pull request using Azure Container Apps revision labels and Azure DevOps pipelines with scale-to-zero economics.

![](https://adamtheautomator.com/wp-content/uploads/2025/11/55418e927e511ae263219c072e27d637c2a967a5036de7020b329582db775c26.png)

### [Automating Docker Container Health Checks with Python and Local Notifications](/docker-health-checks-python/)

Docker's built-in health checks are passive—they tell Docker when a container fails, but do they tell you? In this tutorial, we'll build a lightweight Python monitoring system that runs entirely on your infrastructure with zero external dependencies. You'll learn to detect container failures in real-time, send instant alerts, and maintain a complete audit log of every state change.

![](https://adamtheautomator.com/wp-content/uploads/2023/07/gitea-docker.jpg)

### [Gitea Docker: Your Ultimate Self-Hosted Git Solution](/gitea-docker/)

Learn how to install a Gitea Docker instance and self-host Git repositories securely in this ATA Learning tutorial!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
