Docker cheat sheet, How To Remove Docker Containers, Images, Volumes, and Networks
Docker allows you to quickly build, test and deploy applications as portable, self-sufficient containers that can virtually run everywhere.
Docker doesn’t remove unused objects such as containers, images, volumes, and networks unless you explicitly tell it to do so. As you work with Docker, you can easily accumulate a large number of unused objects that consume significant disk space and clutter the output produced by the Docker commands.
This guide serves as a “cheat sheet” to help Docker users keep their system organized and to free disk space by removing unused Docker containers, images, volumes, and networks.
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
Removing Docker Images
Remove one or more specific images
Use the docker images
command with the -a
flag to locate the ID of the images you want to remove. This will show you every image, including intermediate image layers. When you’ve located the images you want to delete, you can pass their ID or tag to docker rmi
List
$ docker images -a
Remove
$ docker rmi Image Image
Remove dangling images
Docker images consist of multiple layers. Dangling images are layers that have no relationship to any tagged images. They no longer serve a purpose and consume disk space.
List
$ docker images -f dangling=true
Remove
$ docker images purge
Removing images according to a pattern
You can find all the images that match a pattern using a combination of docker images
and grep
. Once you’re satisfied, you can delete them by using awk
to pass the IDs to docker rmi
.
Note that these utilities are not supplied by Docker and are not necessarily available on all systems:
List
$ docker images -a | grep "pattern"
Remove
$ docker images -a | grep "pattern" | awk '{print $3}' | xargs docker rmi
Remove all images
All the Docker images on a system can be listed by adding -a
to the docker images
command. Once you’re sure you want to delete them all, you can add the -q
flag to pass the Image ID to docker rmi
command.
List
$ docker images -a
Remove
$ docker rmi $(docker images -a -q)
Removing Docker Containers
Docker containers are not automatically removed when you stop them unless you start the container using the --rm
flag.
Remove one or more containers
To remove one or more Docker images use the docker container rm
command followed by the ID of the containers you want to remove.
docker container ls -a
The output should look something like this:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cc3f2ff51cab centos "/bin/bash" 2 months ago Created competent_nightingale
cd20b396a061 solita/ubuntu-systemd "/bin/bash -c 'exec …" 2 months ago Exited (137) 2 months ago systemd
fb62432cf3c1 ubuntu "/bin/bash" 3 months ago Exited (130) 3 months ago jolly_mirzakhani
Remove a container upon exit
If you know when you’re creating a container that you won’t want to keep it around once you’re done, you can run docker run –rm to automatically delete it when it exits.
Run and Remove:
$ docker run --rm image_name
Remove all exited containers
You can locate containers using docker ps -a
and filter them by their status like: created
, restarting
, running
, paused
, or exited
.
To review the list of exited containers, use the -f
flag to filter based on status. When you’ve verified you want to remove those containers, using -q
to pass the IDs to the docker rm
command.
List
$ docker ps -a -f status=exited
Remove
$ docker rm $(docker ps -a -f status=exited -q)
Remove containers using more than one filter
Docker filters can be combined by repeating the filter flag with an additional value. This results in a list of containers that meet either condition. For example, if you want to delete all containers marked as either Created (a state which can result when you run a container with an invalid command) or Exited>, you can use two filters:
List
$ docker ps -a -f status=exited -f status=created
Remove
$ docker rm $(docker ps -a -f status=exited -f status=created -q)
Remove containers according to a pattern
You can find all the containers that match a pattern using a combination of docker ps
and grep
.
When you’re satisfied that you have the list you want to delete, you can use awk
and xargs
to supply the ID to docker rmi
.
List
$ docker ps -a | grep "pattern”
Remove
$ docker ps -a | grep "pattern" | awk '{print $3}' | xargs docker rmi
Stop and remove all containers
You can review the containers on your system with docker ps
. Adding the -a
flag will show all containers.
When you’re sure you want to delete them, you can add the -q
flag to supply the IDs to the docker stop and docker rm
commands:
List
$ docker ps -a
Remove
$ docker stop $(docker ps -a -q)
$ docker rm $(docker ps -a -q)
Removing Volumes
Remove one or more specific volumes - Docker 1.9 and later
Use the docker volume ls
command to locate the volume name or names you wish to delete. Then you can remove one or more volumes with the docker volume rm
command:
List
$ docker volume ls
Remove
$ docker volume rm volume_name volume_name
Remove dangling volumes - Docker 1.9 and later
Since the volume get exist independent from containers, when a container is removed, a volume is not automatically removed at the same time.
When a volume exists and is no longer connected to any containers, it’s called a dangling volume.
You can use the docker volume ls
command with a filter to limit the results to dangling volumes, and you can remove them all with docker volume prune
command.
List
$ docker volume ls -f dangling=true
Remove
$ docker volume prune
Remove a container and its volume
If you created an unnamed volume, it can be deleted at the same time as the container with the -v
flag.
Note:
- This only works with unnamed volumes. When the container is successfully removed, its ID is displayed.
- No reference is made to the removal of the volume. If it is unnamed, it is silently removed from the system.
- If it is named, it silently stays present.
Remove
$ docker rm -v container_name