Docker is an open-source containerization platform that allows you to quickly build, test, and deploy applications as portable containers that can run anywhere. In this quick tutorial, we will show you how to remove all stopped containers, all dangling images, and all unused networks in Docker. Also, how to stop and remove all containers by running few commands.
1./ Removing All Unused Objects
The docker system prune
command will remove all stopped containers, all dangling images, and all unused networks:
$ docker system prune
You’ll be prompted to continue, use the -f
or --force
flag to bypass the prompt.
WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all dangling images - all build cache Are you sure you want to continue? [y/N]
If you also want to remove all unused volumes, pass the --volumes
flag:
$ docker system prune --volumes
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all volumes not used by at least one container
- all dangling images
- all build cache
Are you sure you want to continue? [y/N] y
2./ Remove all stopped containers
01- Before removing any containers, you can get a list of all non-running (stopped) containers that will be removed using the following command:
$ docker container ls -a --filter status=exited --filter status=created
02- Now, to remove all stopped containers use the docker container prune
command:
$ docker container prune
You’ll be prompted to continue, use the -f
or --force
flag to bypass the prompt.
WARNING! This will remove all stopped containers. Are you sure you want to continue? [y/N] y
3./ Stop and remove all containers
01- First, you can get a list of all Docker containers on your system using the below command:
$ docker container ls -aq
02- Let’s now stop all running containers using the following command:
$ docker container stop $(docker container ls -aq)
02- Once all containers are stopped, you can remove them using the rm
command followed by the containers ID list.
$ docker container rm $(docker container ls -aq)
Conclusion
You successfully learned how to remove all stopped containers, all dangling images, and all unused networks in Docker. Also, how to stop and remove all containers.
What’s Next? You might want to check the following guides: