简体   繁体   English

docker-compose 只将主机上存在的文件挂载到容器

[英]docker-compose only mount files that exist on host to container

Is it possible to to mount a host directory in to a container but only allowing overwriting files that exist on the host?是否可以将主机目录挂载到容器中,但只允许覆盖主机上存在的文件?

Example github repo: https://github.com/UniBen/stackoverflow-59031249示例 github 仓库: https : //github.com/UniBen/stackoverflow-59031249

Eg Host:例如主机:

src/
  public/
    index.php (200kb)

Container:容器:

src/
  public/
    index.php (100kb)
  vendor/
  ...

Desired output: (The container file system merged with the mounted host system files which exists. Note the size of the index.php file.)期望的输出:(容器文件系统与存在的挂载主机系统文件合并。注意 index.php 文件的大小。)

src/
  public/
    index.php (200kb)
  vendor/
  ...

Actual output:实际输出:

src/
  public/
    index.php (200kb)

Example docker-compose.yml示例 docker-compose.yml

version: '3.2'

services:
    php:
      image: php
      volumes:
        - ./src:/src

Edit: So it looks like overlayfs implemented by docker is only used for bulding docker images and can not be used for volumes what so ever.编辑:所以看起来由 docker 实现的 overlayfs 仅用于构建 docker 图像,而不能用于卷。 I still think it's possible to specify a custom driver but not sure how.我仍然认为可以指定自定义驱动程序,但不确定如何指定。 As a temp fix I've done some fancy stuff with mapping stuff out of container, diffing it and putting it back in but not ideal.作为临时修复,我做了一些花哨的东西,将容器中的东西映射出来,对其进行区分并将其放回原处,但并不理想。

Is it possible to to mount a host directory in to a container but ...是否可以将主机目录挂载到容器中,但是...

No. The only Docker mounting option is a straight-up "replace this directory in the container with the equivalent directory from the host".不。唯一的 Docker 安装选项是直接“用主机中的等效目录替换容器中的此目录”。 There is no way to modify this, selectively hide subdirectories, or implement your "only if it already exists" rule.没有办法修改它,有选择地隐藏子目录,或者实现你的“只有它已经存在”的规则。

In the example you're showing, you probably don't want a volume at all.在您展示的示例中,您可能根本不需要卷。 Files like index.php and a vendor directory are application source code, and in typical use you'd write a Dockerfile, COPY index.php .index.phpvendor目录这样的文件是应用程序源代码,在典型的使用中,你会写一个 Dockerfile, COPY index.php . to move the file into the image, and then RUN composer ... to create the vendor tree.将文件移动到图像中,然后RUN composer ...创建供应商树。 This would be isolated from your host environment, so the vendor directory in the image would be separate from whatever existed on your host system.这将与您的主机环境隔离,因此映像中的vendor目录将与主机系统上存在的任何内容分开。

Actually, there are no options to control such behavior, eg, how data between source and dest will be handled.实际上,没有选项可以控制这种行为,例如,如何处理源和目标之间的数据。 But if David's answer is not really your case you could do something like this:但如果大卫的回答不是你的情况,你可以做这样的事情:

version: '3.2'

services:
  example:
    build:
      context: .
    volumes:
      - data:/src
      - ./src:/src/host

volumes:
  data:

How docker documentation says: docker 文档如何说:

If you start a container which creates a new volume, and the container has files or directories in the directory to be mounted the directory's contents are copied into the volume.如果您启动一个创建新卷的容器,并且该容器在要挂载的目录中有文件或目录,则该目录的内容将被复制到该卷中。

So after that, let's investigate a little bit:所以在那之后,让我们稍微调查一下:

/src # ls
file.a.txt  file.c.txt  host

/src # cat host/file.a.txt 
host

/src # cat file.a.txt 
container
/src # cat file.c.txt 
container

Data from from container is saved into data named volume.来自容器的data保存到名为 volume 的data The data from the host machine live in the host folder.来自主机的数据位于主机文件夹中。 Now you can copy from the host folder to the src with cp or rsync with any rules that you want.现在,您可以使用cprsync使用您想要的任何规则从主机文件夹复制到src

This is a quite fishy and artificial example, maybe it's a good idea to rethink current implementation.这是一个相当可疑和人为的例子,也许重新考虑当前的实现是个好主意。

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

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