如何将文件放在Dockerfile内的共享卷上? [英] How to place files on shared volume from within Dockerfile?
问题描述
Dockerfile在操作系统映像中构建我的工具链,然后通过下载要放置在主机共享卷上的软件包。通常从那里我会登录图像并执行命令来构建。这是问题。我可以在Docker文件中下载源码,但是如何将它存入共享卷。
基本上我有...
添加http:// ... /文件mydir
VOLUME / home / me / mydir
但是当然我收到错误'无法挂载音量超过现有的文件...
我正在做错了吗?
你会发生错误,但你已经怀疑了。
如果你想要源文件在主机文件系统上,摆脱Dockerfile中的 VOLUME
指令,不要在构建时下载源文件,这是你想要做的您可能想要使用一对脚本来设置您的图像:
-
将文件下载到特定位置的文件,说
/ build
。 -
另一个实际运行构建过程。
li>
有了这些,您可以先将源文件下载到lo阳台在主机文件系统上,如:
docker run -v / path / on / my / host:/ build myimage fetch -sources
然后,您可以通过运行以下方式构建它们:
docker run -v / path / on / my / host:/ build myimage build-sources
使用此模型:
-
您正在试图用大量的在图像构建过程中。这几乎不是你想要的,因为存储在卷中的数据被明确地从图像中排除,并且构建过程不允许您方便地将主机目录安装在容器内。
-
您可以将文件下载到主机上的持续位置,并将其提供给您进行编辑或重新构建,或任何其他操作。
-
您可以多次运行构建过程,无需每次重新下载源文件。
我认为这样做会做的很多,但如果不符合你的需要,或者有什么不清楚的话,让我知道。
I have a Dockerfile which builds an image that provides for me a complicated tool-chain environment to compile a project on a mounted volume from the host machines file system. Another reason is that I don't have a lot of space on the image.
The Dockerfile builds my tool-chain in the OS image, and then prepares the source by downloading packages to be placed on the hosts shared volume. And normally from there I'd then log into the image and execute commands to build. And this is the problem. I can download the source in the Dockerfile, but how then would I get it to the shared volume.
Basically I have ...
ADD http://.../file mydir
VOLUME /home/me/mydir
But then of course, I get the error 'cannot mount volume over existing file ..."
Am I going about this wrong?
You're going about it wrong, but you already suspected that.
If you want the source files to reside on the host filesystem, get rid of the VOLUME
directive in your Dockerfile, and don't try to download the source files at build time. This is something you want to do at run time. You probably want to provision your image with a pair of scripts:
One that downloads the files to a specific location, say
/build
.Another that actually runs the build process.
With these in place, you could first download the source files to a location on the host filesystem, as in:
docker run -v /path/on/my/host:/build myimage fetch-sources
And then you can build them by running:
docker run -v /path/on/my/host:/build myimage build-sources
With this model:
You're trying to muck about with volumes during the image build process. This is almost never what you want, since data stored in a volume is explicitly excluded from the image, and the build process doesn't permit you to conveniently mount host directories inside the container.
You are able to download the files into a persistent location on the host, where they will be available to you for editing, or re-building, or whatever.
You can run the build process multiple times without needing to re-download the source files every time.
I think this will do pretty much what you want, but if it doesn't meet your needs, or if something is unclear, let me know.
这篇关于如何将文件放在Dockerfile内的共享卷上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!