How to Stop / Remove all Docker Containers and Images

stop and remove all docker containers

There are a lot of commands for running and managing a Docker container. If you are new to Docker then it is very difficult for you to deal with Docker commands. “Stop and remove all docker containers” is a day-to-day task of any Docker administrator. So it is essential for you to know how to remove and stop all docker containers. In this post, we will show you how to remove and stop all containers.

Prerequisites

  • A Linux system with Docker installed and running.
  • A root user or user account with Sudo privileges.

If you are a beginner and don’t know how to install Docker, read my guide on How to Install and Use Docker on Ubuntu 20.04.

If you want to host Docker on Cloud and don’t know how to choose the best Cloud hosting provider. You can read my guide on The 8 Best Docker Hosting Platforms in 2022.

Docker Stop All Containers

In order to stop all containers in Docker, you will need to know the container name or id first. To do so, list all containers using the following command:

docker container ps

You should get a list of all running container in the following output:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
2f66550f7515        httpd               "httpd-foreground"       23 seconds ago      Up 22 seconds       80/tcp              nifty_banzai
427fc4a27c41        nginx               "/docker-entrypoint.…"   49 seconds ago      Up 48 seconds       80/tcp              keen_lewin

In the above output, the CONTAINER ID column shows a list of all running containers by their ID and the NAMES column display all running containers by their NAMES. To stop a container by CONTAINER ID or NAMES, run the following command:

docker container stop 2f66550f7515
docker container stop nifty_banzai

To stop multiple containers by CONTAINER ID or NAMES, run the following command:

docker container stop 2f66550f7515 427fc4a27c41
docker container stop nifty_banzai keen_lewin

To stop all containers in one line, run the following command:

docker container stop $(docker container ps -aq)

Docker Remove All Containers

In Docker, we can not remove running containers. So we have stopped a running container in the previous step. To remove a container by CONTAINER ID or NAMES, run the following command:

docker container rm 2f66550f7515
docker container rm nifty_banzai

To remove multiple containers by CONTAINER ID or NAMES, run the following command:

docker container rm 2f66550f7515 427fc4a27c41
docker container rm nifty_banzai keen_lewin

To remove all stopped containers in a one-line, run the following command:

docker container rm $(docker container ps -aq)

Docker Remove All Images

You can only remove Docker images that are not referenced by any existing container. Before starting, list all docker images using the following command:

docker image ls

Output:

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              35c43ace9216        3 weeks ago         133MB
httpd               latest              464fdc577ef4        4 weeks ago         138MB

To remove a single image run the following command:

docker rmi nginx

To remove multiple images run the following command:

docker rmi nginx httpd

To remove all images run the following command:

docker rmi $(docker images -q)

Docker System Prune

The docker system prune command is used to remove and clean the following:

  • all dangling images
  • all dangling build-cache
  • all stopped containers
  • all networks not used by at least one container

You can run is as shown below:

docker system prune

You should get the following output:

WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all dangling images
  - all dangling build cache

Are you sure you want to continue? [y/N] y
Deleted Containers:
11ab401de8724911e5062d2763e892c53afc5aa9bb726ea2b744aa321fe1ea00
6e263d05c62fb925245677bd991902b6b23e6a3d97eb21161d235d44a7e51237
6023d6806a315b53656ec6dea2712a4a610710228b127fe7e8d8762221ffa764

Total reclaimed space: 1.114kB

You can use the -a option to remove all unused images not just the dangling ones:

docker system prune -a

You should get the following output:

WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all images without at least one container associated to them
  - all build cache

Are you sure you want to continue? [y/N] y
Deleted Images:
untagged: nginx:latest
untagged: nginx@sha256:f3693fe50d5b1df1ecd315d54813a77afd56b0245a404055a946574deb6b34fc
deleted: sha256:35c43ace9216212c0f0e546a65eec93fa9fc8e96b25880ee222b7ed2ca1d2151
deleted: sha256:61f2666cb67e4572a31412367fa44567e6ac238226385762ea65670ed39034a8
deleted: sha256:622fb7fb6a35078e3a2d446bb0e74c6a0cd500e3a211fd17ecbbcea5377ded38
deleted: sha256:69a8591f1aaa7d694fa79a187886f6690e6e51e8c2bc91727be01a9e87daacd2
deleted: sha256:8a451c701633832102e10093db7545eada8e5639a1b35bb14afaf48601948802
deleted: sha256:2edbde38832e9e0e07d113df74817dc736fd49ea2f9c0d7ce8e40e3446b49b82
untagged: httpd:latest
untagged: httpd@sha256:3c252c919ef2445a6a41dde913a56202754821af87c049c4312bf81bdbc6df4b
deleted: sha256:464fdc577ef4d4ba06050b76a95ffee72d280f7aaa4291f7f4827fca7a00ed0f
deleted: sha256:348b15fd0a83e1f96e750ef672749f97dce7252b663476f2c2a319d558366564
deleted: sha256:4286506113774e2af265f03840376158f583af3f20d8e640e7372a9eb02f1d29
deleted: sha256:eeb7fd4866cf8556f922f8c042a4f435a95b47fad1d4703153a64eecab1c8915
deleted: sha256:0a4fe942b250209eec2ce90a03a786947d5581ab33ce3709710a372e33e56377
deleted: sha256:9eb82f04c782ef3f5ca25911e60d75e441ce0fe82e49f0dbf02c81a3161d1300

Total reclaimed space: 201.4MB

By default, the docker system prune command does not remove unused volumes. To remove all unused volumes, run the following command:

docker system prune --volumes

You can also remove all stopped containers using the following command:

docker container prune

You should see the following output:

WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
45c0c5552c1fe9e026a7483ec9e0b83800ec17bceb1a9937b3a30f4e64dde92c
b20aa22c4a36bca00be000546f6cc5c5faacede0c2cf21b92e75d52976dfbf6b

Total reclaimed space: 1.114kB

Conclusion

In the above post, you learn how to stop and remove all containers. I hope this will help you to perform day-to-day tasks. You should read all Docker-related articles at docker tutorial If you are a beginner. 

Recommended Reading

About Hitesh Jethva

I am Hitesh Jethva Founder and Author at LinuxBuz.com. I felt in love with Linux when i was started to learn Linux. I am a fan of open source technology and have more than 15+ years of experience in Linux and Open Source technologies.

View all posts by Hitesh Jethva