简体   繁体   中英

How to remove unnamed volumes when docker compose down?

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?

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/

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM