简体   繁体   English

当docker容器启动时,是否可以将丢失的文件添加到挂载卷?

[英]Is that possible adding the missing file to mount volume when docker container start?

I have docker image and there is a directory ' A ' which is mount in host directory ' hostA '.我有 docker 映像,并且有一个目录“ A ”,它安装在主机目录“ hostA ”中。 In directory **hostA** , there is a file file0 .在目录**hostA** ,有一个文件file0 When I build the docker image, in directy A , there is a file file1 .当我构建 docker 镜像时,在A 目录中,有一个文件file1 To be more clear, I show them as below:为了更清楚,我将它们显示如下:

In host: 
hostA:
   - file0
In docker container: 
A:
   - file1

When I start the docker container with the docker image.当我使用 docker 镜像启动 docker 容器时。 Since the dir A is mounted in host dir **hostA** and hostA existed when container starting, the file1 in A will not appear although it is build into the docker image.由于目录A挂载在主机目录**hostA**并且容器启动时hostA 已存在,因此A 中file1尽管已构建到 docker 映像中,但不会出现。 Instead file0 will be available in dir A since it is in hostA相反, file0将在 dir A 中可用,因为它在hostA 中

My question is whether there is an approach (either during building the docker image or starting the docker container), make the file1 be also available in dir A (and in host dir hostA ).我的问题是是否有一种方法(在构建 docker 映像期间或启动 docker 容器期间),使 file1 在 dir A (以及主机 dir hostA )中也可用。 It means after container starts, it will be这意味着容器启动后,它将是

In host: 
hostA:
   - file0
   - file1
In docker container: 
A:
   - file0
   - file1

Thanks in advance.提前致谢。

No, not as far as I know.不,据我所知没有。 As Docker volume mounts are working the same as regular unix mounts, you won't see the file in your Docker container.由于 Docker 卷挂载的工作方式与常规 unix 挂载相同,因此您不会在 Docker 容器中看到该文件。 Normally, you separate those directories which will change during the lifetime of your container (like your source code during local development) and those files which don't (eg node_modules when working with JavaScript.通常,您将在容器的生命周期内会更改的目录(例如本地开发期间的源代码)和不会更改的文件(例如,使用 JavaScript 时的 node_modules)分开。

I can outline you two ideas which get close to your desired solution and might work for you.我可以为您概述两个接近您想要的解决方案并且可能对您有用的想法。

Mounting to a different directory挂载到不同的目录

You could split your container directory A into AA and AB .您可以将容器目录A拆分为AAAB AA contains the file which were added during build time while AB contains files which you're mounting in your container. AA包含在构建期间添加的文件,而AB包含您在容器中安装的文件。 Then, you can reference the mounted file from the file in your container using relative paths like ../AB/some_file .然后,您可以使用相对路径(如../AB/some_file从容器中的文件中引用挂载的文件。

# /A/AA/file1.js
require '../AB/file0';
In host: 
hostA:
   - file0
In docker container: 
A:
  AA:
    - file1
  AB: # mounted
    - file0

Mounting a single file挂载单个文件

As another alternative, you can mount a single file.作为另一种选择,您可以挂载单个文件。 That's a valid solution if you only need to mount a couple of files (like configurations) into your container.如果您只需要将几个文件(如配置)挂载到容器中,那么这是一个有效的解决方案。

Docker:码头工人:

-v "PWD"/hostA/file0:/A/file0

docker-compose:码头工人组成:

somes_service:
  volumes:
    - hostA/file0:/A/file0

Still, the file from your container file1 won't be available on your host machine.尽管如此,您的容器file1将无法在您的主机上使用。

In that case, you could set the mounted file to be read only, as well.在这种情况下,您也可以将挂载的文件设置为只读。

hostA/file0:/A/file0:ro

Hope it helps.希望能帮助到你。

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

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