Why are volumes growing all the time?

I have this yml file.

version: '3.9'

services:
  postgresql-postgis:
    ...
    volumes:
      - data-postgresql:/var/lib/postgresql

  pgadmin:
    ...
    volumes:
      - type: bind
        source: ./volumes/data-pgadmin/servers.json
        target: /pgadmin4/servers.json

  java:
    ...
    volumes:
      - type: bind
        source: ./volumes/data-java
        target: /appfolder

volumes:
  data-postgresql:

networks:
  eb:

I use this setup code:

cd /...
docker ps -a
docker stop container-java-eb
docker stop container-pgadmin-eb
docker stop container-postgresql-postgis-eb
docker rm container-java-eb
docker rm container-pgadmin-eb
docker rm container-postgresql-postgis-eb
docker images
docker rmi image-java-eb:v.1.0
docker rmi image-pgadmin-eb:v.1.0
docker rmi image-postgresql-postgis-eb:v.1.0
docker ps -a
docker images
docker volume ls
docker-compose up --build

I have noticed that every time I launch a new setup the volumes grow. When I delete a container I expect to delete all volumes as well, with the exception of the physical files on the OS. Why when I use the command ‘docker volume ls’ does the volume list grow continuously? How can I improve my approach and eliminate the volume with each new setup? I would like to delete the volume but not the files on the OS. Obviously I expect a more precise answer than the following: ‘docker volume rm $(docker volume ls -q)’.

Don’t expect it. Remove a container and its volumes
Or use the Docker compose commands I suggested in the other topic:

What do you mean by files on the OS? When you delete a volume, it will delete the files since it is just deleting a folder on the host when it is a local volume.

1 Like

First of all thank you for your availability and kindness but above all thank you for the madness you dedicate to me.

A volume consists of 2 parts:

  1. file in container (file A)
  2. file on host system (file B)

When I delete all images and all containers I delete all A files but leave all B files.
To perform the deletion of all containers and all images I run these commands:

docker stop container-java-eb
docker stop container-pgadmin-eb
docker stop container-postgresql-postgis-eb
docker rm container-java-eb
docker rm container-pgadmin-eb
docker rm container-postgresql-postgis-eb
docker rmi image-java-eb:v.1.0
docker rmi image-pgadmin-eb:v.1.0
docker rmi image-postgresql-postgis-eb:v.1.0

Why when I run the following command after running the previous ones do I still get volumes?

docker volume ls
DRIVER    VOLUME NAME
local     124872baeec49af287beef1f245aa7c00e4690fcdf36327cd607bda982874bcb
local     bb74246a2f669effccd54e859eb9908077bd9178206f78ba068d55941490f2d0
local     cfb0703904442159028b30777360eefe093dc679e739bd7a30457e2637ec6602
local     f20c951c5d1ef750ce3432f9a76bb4357a7f4366b1db1c0f031fdbe07cff0fa4
local     server-docker-locale-01_data-postgresql

I have 3 volumes, I expect to always read only 3 volumes in the previous list. There is something I didn’t understand but I don’t know what. The above list consists of 5 lines. I have launched the installation commands 2 times so I deduce that every time I launch the installation commands a new record is created.

Another thing I’ve always asked myself: is it possible to give a name to a volume and then delete it? Basically a command similar to the one I use for images and containers.

All files in a volume is on the host filesystem. This is why I don’t understand this part of the question. When you use a smimple bind mount, then the files are not created by docker, so those will not be removed, but that is an other story, since you are talking about anonymous volumes. Just to clarify. When you create a volume, there is no “file inside the container and file on the host”. Those are the same files from two different poing of view. At least when you use Docker on linux without the Desktop, because the Desktop needs to complicate it more.

Maybe if you could show me an example and show me what exactly do you think a file on the host, I would understand.

I still don’t see any command to remove the volumes. Those volumes will not be removed automatically when you remove the container.

However, if your problem is that you don’t know where those volumes were defined, those could be defined in the Dockerfile of the image, or base image of your Dockerfile. You can use docker image inspect to check if an image has a volume . If it has, just define the same target path in your Docker compose file with a name, if you want to have a name.

Whether it has name or not, you can delete all volumes when you run docker-compose down --volumes

This is why I suggest to use that and don’t try to remove everything without docker-compose. Those commands are useful when somethinggoes wrong and you need to clear everything. Otherwise it is just creating more problem and requires more effort.

1 Like

I thought the volume files were double, thanks. I understand your speech.

I thought that by removing the container the link to the volume would be deleted. Using ‘docker-compose down --volumes’ I no longer have a growing list using ‘docker volume ls’. Fantastic!

I also understood something else. When I run ‘docker-compose up --build’ from a 100% clean system I get this:

gi@Ubuntu-22:~/Dropbox/DOCKER/Server-Docker-Locale-01$ docker volume ls
DRIVER    VOLUME NAME
local     2bb5abacffa9dbe33c48ea2cb79792cea075350d166ec822090ae20e3abc812f
local     server-docker-locale-01_data-postgresql
gi@Ubuntu-22:~/Dropbox/DOCKER/Server-Docker-Locale-01$ 

I deduce that 2bb5abacffa9dbe33c48ea2cb79792cea075350d166ec822090ae20e3abc812f is the record relating to 2 volumes and precisely to the following:

    volumes:
      - type: bind
        source: ./volumes/data-pgadmin/servers.json
        target: /pgadmin4/servers.json
    volumes:
      - type: bind
        source: ./volumes/data-java
        target: /appfolder

server-docker-locale-01_data-postgresql should be relative to this volume:

    volumes:
      - data-postgresql:/var/lib/postgresql

Basically when I wasn’t using docker-compose down --volumes I would add double volume every time. At this point a spontaneous question arises: Is it possible to assign a name to a volume in the .yml file? Is it also possible to decide to remove a volume with a specific name, assigned by me, without using docker-compose down --volumes? How?

As for the networks I have a network called ‘eb’ (I have to change this name, I am aware of it). What would be the command to search for existing networks and delete only one with a name assigned by me?

version: '3.9'

services:
  postgresql-postgis:
    ...
    networks:
      - eb

  pgadmin:
    ...
    networks:
      - eb

  java:
    ...
    networks:
      - eb

volumes:
  data-postgresql:

networks:
  eb:

Absolutely not.That is an anonymous volume. You will not find bind mounts among volumes.

You just did that. If you mean rename an anonymous volume, then no, it is not possible.

No. Even if the help inidicates that you could.

 »  docker-compose down --help

Usage:  docker compose down

Stop and remove containers, networks

Options:
      --remove-orphans    Remove containers for services not defined in the Compose file.
      --rmi string        Remove images used by services. "local" remove only images that don't have a custom tag ("local"|"all")
  -t, --timeout int       Specify a shutdown timeout in seconds (default 10)
  -v, --volumes volumes    Remove named volumes declared in the volumes section of the Compose file and anonymous volumes attached to containers.

It say --volumes volumes but it is really just --volumes without parameters.

You will have to open a new topic starting with how you tried to find a way, and why you could not solve it because I would not like to be an interactive documentation and this topic was not about networks initially. On the other hand I would just use docker-compose down to delete the networks and change the configuration, then run docker-compose up again.

I need to ask you again not to ask everything in one topic when you just have an other question because that will be end endless conversation. I know it is not easy when you have so many questions, but we need to keep the forum clean so people don’t need to ask the same thing again because they can’t find anything. In the future I also try not to answer unrelated questions which is hard for me. Also, please, always share how you tried to solve an issue like the one with deleting networks, so you can make sure that your post will not be ignored in favor of other posts where it is easier to answer when we have less time.

Thank you for your cooperation :slight_smile:

1 Like

I understand, thank you very much. So the anonymous volume I see could be the following?

VOLUME [/var/lib/pgadmin]

https://hub.docker.com/layers/dpage/pgadmin4/6.11/images/sha256-cdc0aa4d238dceffdfa9cf178774dee9b1b4b93182af50c271d5145a1e29a314