How to remove unnamed volumes when docker compose down?
Asked Answered
C

4

7

I have a docker-compose file which describes several services. All services have volumes attached to them, however only one has the volume named. When I run docker compose down I want to automatically delete the not named volumes while at the same time create all volumes that are missing.

services:
  service1:
    image: some/image:1
    volumes:
      - named-volume:/home/user1

  service2: 
    image:  some/image:2
    #volumes: not declared volumes that are named automatically with a hash

volumes:
  named-volume:
    name: volume-for-service1

The first time I run docker compose up I want to automatically create all volumes (named and unnamed) and when I run docker compose down I want that unnamed volumes to be deleted while the named one (volume-for-service1) to be preserved. Next time I run docker compose up it should only create the unnamed volumes as the named one already exists.

I have tried:

  • docker compose down -v which removed no volume
  • docker compose down --remove-orphans which removed no volume
  • docker compose down --rmi local which removed no volume
  • docker-compose down -v which removed the named volume
  • docker-compose down --remove-orphans which removed no volume
  • docker-compose down --rmi local which removed no volume

OS: Windows 10 x64

I don't quite get it. What command should I run to achieve desired results?

Calk answered 7/4, 2021 at 14:49 Comment(1)
How are you creating the volumes for the other services if not in the docker-compose.yml?Abbate
G
3

Try using --renew-anon-volumes flag when bringing up the services and use --volumes when bringing down the services

> docker-compose --renew-anon-volumes up
> docker-compose --volumes down

Refer the docker compose documentation

 -V, --renew-anon-volumes   Recreate anonymous volumes instead of retrieving
                            data from the previous containers.
    -v, --volumes           Remove named volumes declared in the `volumes`
                            section of the Compose file and anonymous volumes
                            attached to containers.

https://docs.docker.com/compose/reference/down/

Goodard answered 7/4, 2021 at 16:16 Comment(2)
Doing docker-compose --volumes down will delete the named volume as well!Dreadnought
This is not what OP wanted; "I want that unnamed volumes to be deleted while the named one (volume-for-service1) to be preserved" - this will not preserve the named oneEarlineearls
S
2

To prevent removing named volumes, you should define them as external in the config file:

volumes:
  volume-for-service1:
    name: volume-for-service1
    external: true

But you have to initially create them outside the config file somewhere else, either through:

docker volume create volume-for-service-1

or in a separate config file.

Reference: https://docs.docker.com/compose/compose-file/#external-1

Shoplifter answered 16/6, 2022 at 11:50 Comment(0)
C
0

I'm not aware of a way to remove unnamed volumes automatically, but you can match its hash and remove it with a small script. To reuse your docker-compose.yml example, first you get the container name given the service name with:

docker-compose ps service2 # this is the one with unnamed volume in your example

Output could be something like:

NAME                      COMMAND                  SERVICE             STATUS
project-service2-1        "docker-entrypoint.s…"   service2            exited (0)

Then using the container name you can find its unamed volume hash:

docker inspect -f '{{ (index .Mounts 0).Name }}' project-service2-1

Now before deleting the volume you need to bring the container down or the volume would be in use.

docker-compose down
docker volume rm $volume  # replace the "volume" var with the inspect output

Now that we saw the steps, let's try to make it a little script (slightly adjusted):

service_name=service2 # set the variable accordingly
container_id=$(docker-compose ps $service_name --quiet)
volume_name=$(docker inspect -f '{{ (index .Mounts 0).Name }}' $container_id)
docker-compose down
docker volume rm -f $volume_name
Checker answered 23/9, 2022 at 16:25 Comment(0)
L
0

By default, "docker volume prune" only removes anonymous volumes.

https://docs.docker.com/reference/cli/docker/volume/prune/

Laural answered 4/9 at 9:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.