Find which container uses a Docker Volume - docker

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/

Related

docker volume prune not deleting unused volumes

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 ?

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

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

How do I run a docker container based on its name from `docker ps -a`?

By default docker leaves a bunch of dead volumes around.
$ docker ps -a
61e99f563834 jolly_swanson user/name:version "command" 52 seconds ago Exited (130) 51 seconds ago
Why doesn't docker run jolly_swanson restart that container with its old data? I feel like I must be missing something from the documentation.
You seem to be confusing images and containers. Docker leaves dead containers around, not images (and not volumes either).
docker run creates a new container from an existing image. So docker run jolly_swanson does not work because jolly_swanson is the name of a container, not an image.
To start an existing container, use start, e.g. docker start jolly_swanson.

Docker how to get volumes used by a container

I'm using Docker version 1.10. How can I get the volumes used by a container?
I know I can get the containers by:
docker ps
And I can inspect them with:
docker inspect $containerID
I also know that the volume API is available, so I can also do:
docker volume ls
and
docker volume inspect $volumeID
But I can't find any link information between them. What should I use?
You can get the detail volume information of a container by
docker inspect --format="{{.Mounts}}" $containerID
If I create a volume named "volumehello", and start a container named "hello" which use "volumehello":
docker volume create --name volumehello
docker run -it -d --name=hello -v volumehello:/tmp/data hello-world
Then we can get the volume information of "hello" container by running:
docker inspect --format="{{.Mounts}}" hello
We will get:
[{volumehello /var/lib/docker/volumes/volumehello/_data /tmp/data local z true rprivate}]
volumehello is the volume name
/var/lib/docker/volumes/volumehello/_data is the host location of the volume
/tmp/data is the mapped location of the volume within the container

Resources