You can use these steps to immediately remove docker objects that are taking up disc space, such as docker container and docker volume. This will assist any Docker user in finely organizing their system and removing any unnecessary storage. Let us examine each one individually.
Remove Docker Containers
When you attempt to prevent them, the docker containers will not remove themselves unless you use the –rm flag on the container.
One or more containers must be removed:
To eliminate one or more Dockers, use the docker container rm command and consider the IDs of the containers you want to remove.
To get a list of all containers, use the command docker container ls with the –a option.
$ docker container ls –a
It’s how the output appears:
CONTAINER ID IMAGE COMMAND CREATED STATUS NAMES cc3f2ff51cab centos "/bin/bash" 2months ago Created competent_nightingale cd20b396a061 solita/ubuntu-systemd "/bin/bash -c 'exec …" 2months ago Exited (137) 2months ago systemd fb62432cf3c1 ubuntu "/bin/bash" 3months ago Exited (130) 3months ago jolly_mirzakhani
Pass the CONTAINER ID of the containers you want to delete to the docker container rm command if you know it. For example, to get rid of the first two containers in the above output, type:
$ docker container rm cc3f2ff51cab cd20b396a061
If you receive an error message similar to the one below, it means the container is up and running. You must first stop the container before removing it.
Error response from daemon: You cannot remove a running container fc983ebf4771d42a8bd0029df061cb74dc12cb174530b2036987575b83442b47. Stop the container before attempting removal or force remove.
Remove all containers that have come to a halt:
Run docker container prune to delete all stopped containers,
$ docker container prune
WARNING! This will remove all stopped containers. Are you sure you want to continue? [y/N] y
Now run the following command to get a list of all stopped (non-running) containers that were removed by the docker container prune:
$ docker container ls -a --filter status=exited --filter status=created
Filters can be used to remove containers:
The docker container prune command’s –filter option enables you to delete containers that meet a specified condition.
You can use more than one filter by using multiple –filter options, and the currently supported filters are until and label.
For example, run the command to delete all images created more than 12 hours ago:
$ docker container prune --filter "until=12h"
Halt and remove all containers:
To bring all running containers to a halt, use the command docker container halt with container IDs.
$ docker container stop $(docker container ls -aq)
Use the command to get a list of all containers:
$ docker container ls –aq
Use the docker container rm command with the container ID list to delete once all containers have stopped running.
$ docker container rm $(docker container ls -aq)
Remove Docker Volume
Getting rid of one or more volumes:
To clarify the ID of the one or more volumes you want to delete, run the command docker volume ls.
$ docker volume ls
The output will look like this:
DRIVER VOLUME NAME local 4e12af8913af888ba67243dec78419bf18adddc3c7a4b2345754b6db64293163 local terano
If you know the VOLUME NAME of the volumes you want to delete, use the docker volume rm command. Run the following command, for example, to remove the first volume shown in the above output:
$ docker volume rm 4e12af8913af888ba67243dec78419bf18adddc3c7a4b2345754b6db64293163
If you see an error like the one below, it means that the volume is already being used by another container. So, before you remove the volume, you must first remove the container.
Error response from daemon: remove 4e12af8913af888ba67243dec78419bf18adddc3c7a4b2345754b6db64293163: volume is in use - [c7188935a38a6c3f9f11297f8c98ce9996ef5ddad6e6187be62bad3001a66c8e]
Remove all volumes that aren’t in use:
Run the command docker image prune to remove all of the volumes that aren’t used.
$ docker volume prune
WARNING! This will remove all local volumes not used by atleast one container. Are you sure you want to continue? [y/N]
Use the –f or –force options to skip the prompt.
Get Docker support from experts. Our solutions help you to get away with all the issues when you perform at any stage of the application.