How to stop and remove all docker containers?

In Docker, we cannot remove a running container, stop it first.

Stop all running containers.

Terminal

$ sudo docker stop $(sudo docker ps -aq)

Remove all stopped containers.

Terminal

$ sudo docker rm $(sudo docker ps -aq)

1. Stop Container

1.1 List all containers sudo docker ps

Terminal

$ sudo docker ps

CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS                    NAMES
9ab3de2442e2        spring-boot:1.0     "java -jar app.jar"   3 seconds ago       Up 3 seconds        0.0.0.0:443->8443/tcp    intelligent_mclean
9a2fe01d9df9        spring-boot:1.0     "java -jar app.jar"   28 seconds ago      Up 27 seconds       0.0.0.0:8080->8080/tcp   quizzical_curran

1.2 Stop a container by CONTAINER ID or NAMES

Terminal

$ sudo docker stop 9ab3de2442e2

$ sudo docker stop intelligent_mclean

1.3 Stop multiple containers by CONTAINER ID or NAMES

Terminal

$ sudo docker stop 9ab3de2442e2 9a2fe01d9df9

$ sudo docker stop intelligent_mclean quizzical_curran

1.4 Display container IDs for all containers.

Termimal

$ sudo docker ps -aq

9ab3de2442e2
9a2fe01d9df9

1.5 Combine sudo docker ps -aq with the stop command; we can stop all containers in one line.

Terminal

$ sudo docker stop $(sudo docker ps -aq)

2. Remove Container

2.1 Remove a container by CONTAINER ID or NAMES

Terminal

$ sudo docker rm  9ab3de2442e2

$ sudo docker rm intelligent_mclean

2.2 Remove multiple containers by CONTAINER ID or NAMES

Terminal

$ sudo docker rm 9ab3de2442e2 9a2fe01d9df9

$ sudo docker rm intelligent_mclean quizzical_curran

2.3 Remove all stopped containers.

Terminal

$ sudo docker rm $(sudo docker ps -aq)

3. docker system prune

This docker system prune clean the following stuff:

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

This purge command will save a lot of the hard disk space.

Terminal

$ sudo docker system prune

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:
10b3f08a93df89818976a29c26f5ccefbda1e2dc2e0205a02e9a3306590a1455
89d9cb3e102dbadc9d314bea0db5feb3110931727e2da0b88d6d88d7d78f4400
49e429f26db40615557a85fdf758cd66afa208dba03f74aea860bb04bb4772b2
3e36a2c0c241f8b815ddc73f398cae97962a6799c244fd5169cd845023c5b657
...

Deleted Images:
deleted: sha256:fe111236265a30b8ec54390defb5da60cc7b76a0d3ff0e883691b8b3c663f2e9
deleted: sha256:2e591f44cd334c7896ed01660d45099f4f58169d0584163b6009dea56c3abcbe
deleted: sha256:8b1c255bf0e627c789fc35b3b3b0a1e5033f6be612e01dca08eb1aa4fd161364
...

Total reclaimed space: 251.3MB

References

author image

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by

Comments

avatar