How to remove Docker Containers / Docker Volume?

 docker containers and docker volume

You can use these steps to quickly remove docker objects like docker container and docker volume that are accumulating your disk space. This will help any docker user to finely organize their system from unwanted storage. Let’s see each one by one.

Remove Docker Containers:

Remove Docker ContainersWhen you try to stop them, the docker containers will not automatically delete until you use the container with the –rm flag.

Remove one or more Containers:

Use the docker container rm command to remove one or more Docker and follow the IDs of the container you want to remove.

Enter the command docker container ls with the –a option to get the list of all containers.

$ docker container ls –a

This how the output looks:

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

If you know the id, pass the CONTAINER ID of the containers you want to delete to the docker container rm command. For example, to delete the first two containers shown in the above output, you should run:

$ docker container rm cc3f2ff51cab cd20b396a061

If you get an error message like the below one, it signifies the container is up and running. Before removing the container, you must first stop it.

Output:

Error response from daemon: You cannot remove a running
container fc983ebf4771d42a8bd0029df061cb74dc12cb174530b2036987575b83442b47.
Stop the container before attempting removal or force remove.

Remove all stopped Containers:

Now invoke the docker container prune command to remove all stopped containers,

$ docker container prune

Output:

WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y

Now get a list of all stopped (non-running) containers that removed by docker container prune, follow the below command:

$ docker container ls -a --filter status=exited --filter
status=created

Remove Containers using filters:

Using the –filter option, the docker container prune command allows you to remove containers based on a specific condition.

By using multiple –filter options, you can use more than one filter, and currently, the supported filters are until and label.

For example, to delete all images generated more than 12 hours ago, run the command:

$ docker container prune --filter "until=12h"

Stop and remove all containers:

Now enter the command docker container stop along with container IDs to stop all running containers.

$ docker container stop $(docker container ls -aq)

To generate a list of all containers, use the command:

$ docker container ls –aq

Once all containers stopped running, use the docker container rm command with the container ID list to delete them.

$ docker container rm $(docker container ls -aq)

Remove Docker Volumes

Removing one or more volumes:

Now, run the command docker volume ls to identify the ID of the one or more volumes you want to remove.

$ docker volume ls

This is how the output looks:

DRIVER     VOLUME NAME
local      4e12af8913af888ba67243dec78419bf18adddc3c7a4b2345754b6db64293163
local      terano

If you’ve found the VOLUME NAME of the volumes to be removed, use the docker volume rm command to delete them. For example, to remove the first volume shown in the above output, run the following command:

$ docker volume rm
4e12af8913af888ba67243dec78419bf18adddc3c7a4b2345754b6db64293163

If you see an error similar to the given below, it signifies that an existing container uses the volume. So first, you have to remove the container before you remove the volume.

Output:

Error response from daemon: remove
4e12af8913af888ba67243dec78419bf18adddc3c7a4b2345754b6db64293163: volume is in
use - [c7188935a38a6c3f9f11297f8c98ce9996ef5ddad6e6187be62bad3001a66c8e]

Remove all unused volumes:

Run the command docker image prune, to remove all unused volumes:

$ 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] 

To bypass the prompt, use the -f or –force option.

Hope this was helpful, if you want to remove docker images or docker network you can refer here: How to remove Docker Images / Docker Network?

For any assistance related to docker, you can Contact Us.

To get updates follow us on Facebook, Twitter, LinkedIn

Subscribe to get free blog content to your Inbox
Loading

Written by actsupp-r0cks