简体   繁体   English

docker-compose.yml命名卷的语法

[英]docker-compose.yml Syntax for Named Volumes

What is the docker-compose.yml syntax for specifying the host path of a named volume? 什么是docker-compose.yml语法,用于指定命名卷的主机路径?

docker-compose.yml: 泊坞窗,compose.yml:

volumes:
  myvolume:  # how do I specify path here?
  othervolume:

services: # etc...

I've checked the docs , but I can't find it. 我检查了文档 ,但我找不到它。 Honestly, I don't know how anyone uses this stuff. 老实说,我不知道有人使用这些东西。

The common scenario with compose volumes is to use the default named volume which maps to the local volume driver and places volumes in /var/lib/docker/volumes. 撰写卷的常见方案是使用默认的命名卷,该卷映射到本地卷驱动程序并将卷放在/ var / lib / docker / volumes中。 Not what you're looking for, but this is the easy option for many: 不是你想要的,但对许多人来说这是一个简单的选择:

version: '3'
volumes:
  myvolume:
  othervolume:    
services:
  myservice:
    volumes:
      - myvolume:/volume/path

The common method to map a host volume is to specify the path directly, no name needed on the volume. 映射主机卷的常用方法是直接指定路径,不需要卷上的名称。 Again, not what you're asking for, but it's very easy to implement. 再次,不是你要求的,但它很容易实现。 This is a bind mount under the covers: 这是封面下的绑定安装:

version: '3'
services:
  myservice:
    volumes:
      - ./path:/volume/path

If you want a named volume and host volume together, what you're looking for is a named volume configured to use a bind mount. 如果您希望将命名卷和主机卷放在一起,那么您要查找的是配置为使用绑定装入的命名卷。 This has the downside of failing if the directory does not preexist, but has the upside that docker can init an empty directory to the contents of the image. 如果目录没有预先存在,这有失败的缺点,但是有一个好处,即docker可以为图像的内容初始化一个空目录。

version: '3'
volumes:
  myvolume:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: /host/path/to/volume
services:
  myservice:
    volumes:
      - myvolume:/container/volume/path

Note, the downside of bind mounts is that it places files that are managed by containers, with the uid/gid from the container, inside a path likely used by other users on the host, often with a different uid/gid on the host. 请注意,绑定挂载的缺点是它将容器管理的文件与容器中的uid / gid一起放置在主机上其他用户可能使用的路径中,通常在主机上使用不同的uid / gid。 The result is permission issues either on the host or inside the container. 结果是主机上或容器内的权限问题。 You need to align uid/gid's between the two to avoid this. 您需要在两者之间对齐uid / gid以避免这种情况。

In the same reference docs you can check complete expanded version of sample docker-compose.yml 在相同的参考文档中,您可以检查示例docker-compose.yml的完整扩展版本

https://docs.docker.com/compose/compose-file/#compose-file-structure-and-examples https://docs.docker.com/compose/compose-file/#compose-file-structure-and-examples

See db section for named volume mounting. 有关命名卷安装,请参阅db部分。

  db:
    image: postgres:9.4
    volumes:
      - db-data:/var/lib/postgresql/data
    networks:
      - backend
    deploy:
      placement:
        constraints: [node.role == manager]

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

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