docker volume prune not deleting unused volumes - docker-volume

My understanding is docker volume prune should delete volumes which are no longer associated with containers.
Locally I have
$ docker volume ls | head -n 2
DRIVER VOLUME NAME
local 0d4cd922a4ed3e970726b1edb860c7dda3ae1e47f812585d9517d512ae70d5cf
I confirm it doesn't have an associated container via
$ docker ps -a --filter volume=0d4cd922a4ed3e970726b1edb860c7dda3ae1e47f812585d9517d512ae70d5cf
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
therefore I expect this volume to be deleted, but alas
$ docker volume prune
WARNING! This will remove all local volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
$ docker volume ls | head -n 2
DRIVER VOLUME NAME
local 0d4cd922a4ed3e970726b1edb860c7dda3ae1e47f812585d9517d512ae70d5cf
what am I missing ?

Related

docker volume prune: Dont delete named volumes

I have created a volume
docker volume create postgresql_db
Now i am using it in my docker-compose.xml
services:
postgresql:
image: "postgres:11-alpine"
volumes:
- type: volume
source: postgresql_data
target: /var/lib/postgresql/data
volume:
nocopy: true
environment:
PGDATA: '/var/lib/postgresql/data/pgdata'
networks: # connect to the bridge
- postgresql_network
command: ["postgres", "-c", "log_statement=all","-c", "log_destination=stderr"]
volumes:
postgresql_data:
external: true
Sometimes i want to stop and clean the docker system.
So I try to run
docker stop $(docker ps -aq) # stop all containers
docker rm $(docker ps -a -q) # remove all containers
docker container prune
docker image prune
docker network prune
docker volume prune #<-- remove all dangling volumes also
this also delete postgresql_data i.e named volume
how to avoid that.
Well, according to Docker's documentation, you can simply filter your volumes (by label, for instance):
docker volume prune --filter label!=postgresql_db
then all volumes that are not (look at the !) named postgresql_db and are not attached to 1 or more containers will be removed.
There's no option to the docker volume prune command for this. There's also no metadata on the volume that indicates it's an anonymous volume, it's just a named volume with a random hex name. The best I do is list the dangling volumes and delete the ones with a long hex name. As long as your named volumes are not exactly 64 hex characters, they won't be included in this:
docker volume ls -qf dangling=true | egrep '^[a-z0-9]{64}$' | \
xargs --no-run-if-empty docker volume rm
It worked for me.
Delete all unnamed volumes:
docker volume prune --filter "label!=com.docker.compose.volume"
docker rm offers the flag -v which is deleting all anonymous volumes corresponding to this container.
If you want to prune the dangling ones as well (not associated to any container you're currently running) do the docker volume prune before stopping and removing the running ones.
Therefore:
docker volume prune #<-- remove all dangling volumes also
docker stop $(docker ps -aq) # stop all containers
docker rm -v $(docker ps -a -q) # remove all containers
docker container prune
docker image prune
docker network prune

How to cleanup docker containers and images on linux machines

We have Linux redhat machine with docker and docker compose
Now we want to clean all containers and images - like we have scratch new docker
As I understand to get that , we need to perform the following procedure with this order:
Am I right with this procedure? , or I missing something?
1. docker stop <CONTAINER ID>
2. docker container rm <CONTAINER ID>
3. docker image rm <IMAGE ID>
example
first find - CONTAINER ID
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
654fa81f4439 confluentinc/cp-enterprise-control-center:5.0.0 "/etc/confluent/dockā€¦" 9 minutes ago Up 9 minutes 0.0.0.0:9021->9021/tcp control-center
1)
stop container
docker stop 654fa81f4439
654fa81f4439
2)
delete container
docker container rm 654fa81f4439
654fa81f4439
3)
find image ID
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
confluentinc/cp-enterprise-control-center 5.0.0 e0bd9a5edb95 15 months ago 617MB
delete image
ocker image rm e0bd9a5edb95
Untagged: confluentinc/cp-enterprise-control-center:5.0.0
Untagged: confluentinc/cp-enterprise-control-center#sha256:2e406ff8c6b1b8be6bf01ccdf68b14be0f0759db27c050dddce4b02ee0894127
Deleted: sha256:e0bd9a5edb9510a326934fa1a80a4875ab981c5007354de28f53bfb3e11bc34a
Deleted: sha256:c23255297f6d75f156baf963786d3ded1d045b726d74ed59c258dc8209bac078
Deleted: sha256:6cab492e72ca2578897b7ceecb196e728671158e262957f3c01e53fd42f6f8b4
In short, yes, it is the correct procedure to clear all the containers and images.
But you can do it more easily. For example:
Stop all containers at
once:
docker container stop $(docker container ls -aq)
Remove all stopped containers: docker container prune --force
Remove all unnused images: docker image prune --all --force
Sometimes Docker volumes are used for containers to persist data. You may want to clean them too (docker volume prune --force).
Others Docker resources may be left on your system (such as networks and build caches).
You can appeal to docker system prune to remove all unused data:
$ docker system prune --all --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 images without at least one container associated to them
- all build cache
Are you sure you want to continue? [y/N] y

Find which container uses a Docker Volume

In running docker volume ls the response contains:
DRIVER VOLUME NAME
local 09033b4b22135832411ff485d5adc90e778316c1b9ba27bf8032cb65b8de557b
local 2ae85f4d2270716f936c4ef06e1a9408f90826258c7489a150125ff9d13ce79c
local 58497ac069495708d3bd17aab2b16b9a02badd245de4614a05a7133bdf0efb34
local eb958485be4ae9c8a71a01a4f03c7cbf9f76c3a8fb2622ded58cf96542373b0d
local efb5d345f272b32f409800e4804e22a93759c5668c8cb43e1887b5943ea217f9
How can I correlate those volume names to the containers using them?
I'm trying to understand where the volumes came from and if I can remove them.
Simple example starting a centos container using a named volume
docker run --rm -dit -v my-vol:/my-vol centos
You can make use of the --filter flag to see which container(s) the volume is being used by[1]:
docker ps -a --filter volume=my-vol
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f70a651c472 centos "/bin/bash" 24 seconds ago Up 23 seconds distracted_swirles
If you want to remove volumes that are not being used by any containers, you can use docker volume prune
docker volume prune
WARNING! This will remove all local volumes not used by at least one container.
Are you sure you want to continue? [y/N]
[1] https://docs.docker.com/engine/reference/commandline/ps/#filtering
[2] https://docs.docker.com/engine/reference/commandline/volume_prune/

clean docker volume with no reclaimable space on local volumes

I am running Jenkins and Docker, and Jenkins periodically stops working due to a lack of space.
Up til now, docker's cleanup command:
docker system prune -a
cleared up enough space to resolve this error; however, the available space was smaller each time. What I noticed is that
docker system df
produces the following output:
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 4 4 882.9MB 79.15MB (8%)
Containers 6 6 16.45kB 0B (0%)
Local Volumes 4 4 31.95GB 0B (0%)
Build Cache 0B 0B
as you can see, Local Volumes is taking up a giant 32 GB, so
is this normal?
how can I safely reduce the size of Local Volumes?
Thanks
Yes it is normal, when you run $docker system prune by default it doesn't prune the volumes, you have to add extra option to trigger it:
use:
$ docker system prune --volumes
or
$docker volume prune
So here is a temporary solution that does not involve removing or partitioning the volume - the key is to stop the active container before prune:
find the container id:
docker ps
stop the that (or all) containers:
docker container stop <id>
then prune:
docker system prune -a
and then, if you get a get "getsockopt: connection refused" error, I believe you need to recreate the docker registry:
docker run -d -p 5000:5000 --restart=always --name registry registry:2
This cleans out most of the volume, until it fills up again. I would appreciate it if someone could address why it fills up in the first place

Kill docker-compose containers without docker-compose.yml file

I have some docker containers provisioned by docker-compose with restart: always flag. But I accidentally delete docker-compose.yml file.
How do I delete those containers so that they will not restart automatically?
Without the compose file, docker-compose can't guess the name of the containers that need to be killed. You need to do that manually using docker command:
docker container ls
docker rm -f <container-name-or-id>
You can use docker ps -a to list all the running containers. After you can use docker stop container-name to stop individually the containers you want and after you can use docker rm $(docker ps -a -q) to delete and remove all the stopped containers.
Also if you want to delete the docker images as well you can use docker images to list the existing images and after docker rmi image-name to delete the image.
I had a similar issue - I didn't want to fetch the .yml files just to remove the containers. As docker-compose adds labels to the containers, we can use these to filter the matching containers and then remove them.
To remove all the containers that docker-compose created for project ProjectName (normally the name of the folder where the docker-compose.yml was) in shell (bash or similar), execute the following:
docker ps --filter 'label=com.docker.compose.project=ProjectName' --format {{.ID}} | xarg -n 1 docker rm --force --volumes
docker ps with the filter provides the list of containers, the format is provided so that only container IDs are printed
xargs -n 1 - takes the list of container IDs and executes the target command on each of them providing the container ID as the argument to the target
docker rm removes existing containers
--force - to also stop running containers (otherwise running containers would not be stopped or removed)
--volumes - to also remove any anonymous volumes associated with the containers

Resources