简体   繁体   English

相对路径不适用于 docker-compose.yml 中的命名卷

[英]Relative path not working with named volumes in the docker-compose.yml

I need to make a named volume use a relative path to the folder where the docker-compose command is executed.我需要使命名卷使用执行docker-compose命令的文件夹的相对路径。

Here is the volume definition in the docker-compose.yml这是 docker-compose.yml 中的卷定义

volumes:
  esdata1:
   driver: local 
   driver_opts:
      type: none
      device: ./esdata1
      o: bind

It seems that docker-compose do not create the folder if it does not exist , but even when the folder is created before lauching docker I'm always getting this error: 如果文件夹不存在,docker -compose似乎不会创建该文件夹,但即使在启动 docker 之前创建了该文件夹,我也总是收到此错误:

ERROR: for esdata  Cannot create container for service esdata: error while mounting volume with options: type='none' device='./esdata1' o='bind': no such file or directory

NOTE: This is maybe silly, but esdata is the service that use the named volume注意:这可能很愚蠢,但esdata是使用命名卷的服务

  esdata:
    ...
    volumes:
      - esdata1:/usr/share/elasticsearch/data
    ...

What I'm missing here?我在这里缺少什么?

Maybe the relative path ./ does not point to the folder where the docker-compose is executed (I've tried with ~/ to use a folder relative the user's home but I got the same error).也许相对路径./不指向执行docker-compose的文件夹(我已经尝试使用~/使用相对于用户家的文件夹,但我遇到了同样的错误)。

Thanks in advance,提前致谢,

PS: If I use an absolute path it works like a charm PS:如果我使用绝对路径,它就像一个魅力

I encountered exactly the same issue.我遇到了完全相同的问题。 It seems that you have done nothing wrong.看来你没有做错什么。 This is just not yet implemented in Docker : https://github.com/docker/compose/issues/6343这还没有在 Docker 中实现: https : //github.com/docker/compose/issues/6343

Not very nice for portability...便携性不是很好...

If you use a named bind mount like this, you must include the full path to the file, eg:如果您使用这样的命名绑定挂载,则必须包含文件的完整路径,例如:

volumes:
  esdata1:
   driver: local 
   driver_opts:
      type: none
      device: /home/username/project/esdata1
      o: bind

That folder must also exist in advance.该文件夹也必须提前存在。 This is how the linux bind mount syscall works, and when you pass flags like this, you are talking directly to Linux without any path expansion by docker or compose.这就是 linux 绑定挂载系统调用的工作方式,当您传递这样的标志时,您是在直接与 Linux 对话,而无需 docker 或 compose 进行任何路径扩展。

If you just want to mount the directory from the host, using a host volume will be expanded for the relative path by compose:如果您只想从主机挂载目录,则使用主机卷将通过 compose 扩展为相对路径:

  esdata:
    ...
    volumes:
      - ./esdata1:/usr/share/elasticsearch/data
    ...

While the host volume is easier for portability since the path is automatically expanded, you will lose the feature from named volumes where docker initializes an empty named volume with the image contents (including file permissions/ownership).虽然由于路径自动扩展,主机卷更易于移植,但您将失去命名卷的功能,其中 docker 使用图像内容(包括文件权限/所有权)初始化一个空的命名卷。

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

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