简体   繁体   English

docker 组合下来时如何删除未命名的卷?

[英]How to remove unnamed volumes when docker compose down?

I have a docker-compose file which describes several services.我有一个 docker-compose 文件,它描述了几个服务。 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.当我运行docker compose down我想自动删除未命名的卷,同时创建所有丢失的卷。

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.我第一次运行docker compose up我想自动创建所有卷(命名和未命名),当我运行docker compose down我希望删除未命名的卷,同时保留命名的卷( volume-for-service1 )。 Next time I run docker compose up it should only create the unnamed volumes as the named one already exists.下次我运行docker compose up它应该只创建未命名的卷,因为命名的卷已经存在。

I have tried:我努力了:

  • docker compose down -v which removed no volume docker compose down -v不删除任何卷
  • docker compose down --remove-orphans which removed no volume docker compose down --remove-orphans没有删除任何卷
  • docker compose down --rmi local which removed no volume docker compose down --rmi local which remove no volume
  • docker-compose down -v which removed the named volume docker-compose down -v删除了命名卷
  • docker-compose down --remove-orphans which removed no volume docker-compose down --remove-orphans没有删除任何卷
  • docker-compose down --rmi local which removed no volume docker-compose down --rmi local which remove no volume

OS: Windows 10 x64操作系统: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在启动服务时尝试使用--renew-anon-volumes标志,在关闭服务时使用--volumes

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

Refer the docker compose documentation请参阅 docker 撰写文档

 -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/ 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参考: 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.我不知道自动删除未命名卷的方法,但您可以匹配其 hash 并使用小脚本将其删除。 To reuse your docker-compose.yml example, first you get the container name given the service name with:要重用您的docker-compose.yml示例,首先您要获得给定服务名称的容器名称:

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

Output could be something like: Output 可能类似于:

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:然后使用容器名称可以找到它的未命名卷 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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