且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Docker中将主机文件夹与容器文件夹合并?

更新时间:2022-10-23 16:08:10

您可以将卷从主机安装到与扩展文件夹不同的位置,然后在启动脚本中,您可以将内容复制到容器的目录中。您将需要重建主机一次。



例如:

  Dockerfile 
RUN cp startup-script / usr / local / bin / startup-script
CMD / usr / local / bin / startup-script

启动脚本:
#!/ bin / bash
cp / mnt / extensions / path / to / wikipedia / extensions
/ path / to / old-startup-script $ @

docker运行-d -v / home / admin / wikimedia / extensions:/ mnt / extensions wikipedia

是解决这个问题的一种方法,另一种方式是维护一个单独的扩展数据容器,然后安装它并将其维护在***容器之外。它必须拥有所有的扩展名。



你可以这样开始:

  docker run -d -v / home / admin / wikimedia / extensions:/ path / to / wikipedia / extensions --name extensions busybox tail -f / dev / null 
docker run -d - 卷 - 从扩展名***


I have Wikimedia running on Docker. Wikimedia's extensions reside in extensions/ folder which initially contain built-in extensions (one extensions = one subfolder)

Now I wish to add new extensions. However I don't prefer the option of modifying the Dockerfile or creating new commit on the existing container.

Is it possible to create a folder in the host (e.g. /home/admin/wikimedia/extensions/) which is to be merged (not to overwrite) with the extension folder in the container? So whenever I want to install new extension, I just copy the extension folder to the host /home/admin/wikimedia/extensions/

You can mount a volume from your host to a separate location than the extension folder, then in your startup script, you can copy the contents to the container's directory. You will need to rebuild your host once.

For example:

Dockerfile:
  RUN cp startup-script /usr/local/bin/startup-script
  CMD /usr/local/bin/startup-script

startup-script:
   #!/bin/bash
   cp /mnt/extensions /path/to/wikipedia/extensions
   /path/to/old-startup-script $@

docker run -d -v /home/admin/wikimedia/extensions:/mnt/extensions wikipedia

That is one way to get around this problem, the other way would be to maintain a separate data container for extensions, then you will mount this and maintain it outside of the wikipedia container. It would have to have all the extensions in it.

You can start one like so:

 docker run -d -v /home/admin/wikimedia/extensions:/path/to/wikipedia/extensions --name extensions busybox tail -f /dev/null
 docker run -d --volumes-from extensions wikipedia