Solving the DevOps Infrastructure Dilemma: Enabling developer velocity with control ūüí°

Register for the webinar here ‚Üí


How to Stop and Remove Docker Containers

docker stop container

Spacelift and Docker

Customize your workflow by bringing your own Docker image and using it as a runner to accelerate deployments that leverage third-party tools.

Book a demo

In this article, we will take a look at how to perform various useful command line operations in Docker, showing how to start, list, stop and remove containers, and the various options available.

We will cover:

  1. Prerequisites
  2. How to start a Docker container
  3. How to list the running containers
  4. How to stop a running Docker container
  5. How to force stop a Docker container
  6. How to stop all running Docker containers
  7. How to remove the containers
  8. How to stop and remove containers in Docker Compose
  9. Docker container stop vs. kill


If you don’t already have Docker installed, you can grab it from the official website:¬†Docker Download.

Once installed, to follow along with this article, pull the Ubuntu image from the Docker hub so we can use the image with the containers that we will manipulate using the command:

stop all containers docker

For other Docker commands, check out our Docker cheat sheet.

How to start a Docker container

To run a simple container based on the Ubuntu image we just pulled, run the command below:

docker run -it ubuntu /bin/bash

After running the container, you’ll be inside the container’s shell (in this example, a Bash shell). You can interact with it just like a regular Linux system.

When you’re done, you can exit the container by typing exit.

docker start container

Let’s take a look at the command options for¬†docker run.

This command is used to start a container, and the syntax looks like this:


In our example, the IMAGE we used was ubuntu , the -it OPTIONS we specified make the container interactive and allocate a terminal. The COMMAND we supplied was the command to run inside the terminal of the running container.

You can find out more about the options available to the Docker run command over on the official Docker reference pages.

How to list the running containers

To see a list of running containers, you can use the docker ps command. This command provides information about running containers, including their names, IDs, and status.

docker ps
docker stop all containers and remove

How to stop a running Docker container

To stop a running container, you can use the docker stop command and provide either the container name or container ID. You can find these from the information supplied in the docker ps command.

For example, as shown above, the container ID of my running Ubuntu container is ccfac1f88d1b:

docker stop ccfac1f88d1b

Run docker ps again to check the container is stopped.

When you run¬†docker stopthe main process inside the container will receive¬†SIGTERM, and after a grace period,¬†SIGKILL. The first signal can be changed with the¬†STOPSIGNAL instruction in the container’s Dockerfile or the¬†--stop-signal¬†option to¬†docker run.

The usage for the docker stop command:


You can also use the option -sto change the signal sent to the container, and -t to supply the number of seconds to wait before killing the container.

How to force stop a Docker container

To forcefully stop a Docker container, you can use the docker kill command. The main process inside the container is sent SIGKILL signal (default), or the signal that is specified with the --signal option, which can terminate the process abruptly.

When you run this command, you will notice the container is terminated much more quickly than when running docker stop as the container is not gracefully terminated, and the container does not have a chance to perform any cleanup tasks.

The usage is similar to the docker stop command:

docker kill

Be aware that this can potentially lead to data corruption or unexpected behavior in the container.

Because of this,docker kill should be used after command with caution, usually after trying docker stop first to allow the container to shut down cleanly and only resort to docker kill when the container is unresponsive or cannot be stopped by normal means.

How to stop all running Docker containers

To stop all running docker containers with a single command rather than individually, you can use a for loop in the shell:

docker stop $(docker ps -q)

Breaking this down, the docker ps -q command lists all the running containers showing the containerID only. This is then passed to the docker stop command.

Download The Practitioner’s Guide to Scaling Infrastructure as Code


How to remove the containers

If you want to remove the containers after stopping them, you can use the docker rm command in a similar manner:

docker rm $(docker ps -a -q)

The additional -a option in the docker ps -a -q command lists all containers, including stopped ones. After running the command, a list of the containerIDs that have been removed will be shown on the console:

docker remove container

Similar to the docker kill command, if required you can use the docker rm command with the --kill option to force the removal of a running container, using SIGKILL.

How to stop and remove containers in Docker Compose

In Docker Compose, you can stop and remove containers associated with your Compose project using the docker-compose down command. You can execute the docker-compose down command from the directory where your docker-compose.yml file is located.

I have a docker-compose.yaml file to create a simple Ubuntu container:

version: '3'
    image: ubuntu
    command: sleep infinity

I run docker compose up to create the container:

stop running containers docker

And then run docker compose down to remove it:

stop and remove all docker containers

This command not only stops the containers but also removes:

  • Containers for services defined in the Compose file.
  • Networks defined in the networks section of the Compose file.
  • The default network, if one is used.

Networks and volumes defined as external are never removed.

To remove named volumes declared in the ‚Äúvolumes‚ÄĚ section of the Compose file and anonymous volumes attached to containers, you can use the¬†-v¬†option.

Docker container stop vs. kill

Both commands will terminate containers, the main difference being that the stop command allows the container to terminate gracefully, and the kill command terminates it immediately.

You should generally prefer docker container stop when possible to ensure a clean shutdown of your containers but docker container kill can be useful when a container is not responding or needs to be stopped forcefully.

Key points

Manipulating containers on the command line and knowing how to start, stop, kill, and remove containers on demand with the available options is a great way to start learning and experimenting with Docker. Knowing the differences between the stop and kill commands as explained above is valuable, especially to avoid any issues when running in production!

We encourage you also to explore how Spacelift offers full flexibility when it comes to customizing your workflow. You have the possibility of bringing your own Docker image and using it as a runner to speed up the deployments that leverage third party tools. Spacelift’s official runner image can be found here.

The Most Flexible CI/CD Automation Tool

Spacelift is an alternative to using homegrown solutions on top of a generic CI. It helps overcome common state management issues and adds several must-have capabilities for infrastructure management.

Start free trial