Docker如何在不将其提交到映像的情况下添加文件? [英] Docker how to ADD a file without committing it to an image?
问题描述
我有一个〜300Mb的压缩本地文件,我将其添加到docker映像中。然后,下一个状态将提取图像。
I have a ~300Mb zipped local file that I add to a docker image. The next state then extracts the image.
问题是ADD语句导致提交,该提交导致新的文件系统层使图像比图像大300Mb。
The problem is that the ADD statement results in a commit that results in a new file system layer makes the image ~300Mb larger than it needs to be.
ADD /files/apache-stratos.zip /opt/apache-stratos.zip
RUN unzip -q apache-stratos.zip && \
rm apache-stratos.zip && \
mv apache-stratos-* apache-stratos
问题:是否存在一种解决方法,可以在不引起提交的情况下添加本地文件?
Question: Is there a work-around to ADD local files without causing a commit?
一种选择是运行简单的Web服务器(例如 python -m SimpleHTTPServer
),然后启动docker build,然后使用 wget
检索文件,但这似乎有些混乱:
One option is to run a simple web server (e.g. python -m SimpleHTTPServer
) before starting the docker build, and then using wget
to retrieve the file, but that seems a bit messy:
RUN wget http://localhost:8000/apache-stratos.zip && \
unzip -q apache-stratos.zip && \
rm apache-stratos.zip && \
mv apache-stratos-* apache-stratos
另一种方法是提取在容器启动时而不是在构建时压缩文件,但我希望保持启动尽可能快。
Another option is to extract the zipped file at container start up instead of build time, but I would prefer to keep the start up as quick as possible.
推荐答案
根据文档,如果您从本地文件系统(而非URL)添加到Dockerfile中(带有目标路径,而不是路径+文件名),它将把文件解压缩到给定的目录中。
According to the documentation, if you pass an archive file from the local filesystem (not a URL) to ADD in the Dockerfile (with a destination path, not a path + filename), it will uncompress the file into the directory given.
如果< src>是本地tar归档文件,且压缩格式为
(身份,gzip,bzip2或xz),则将其解压缩为目录。
来自远程URL的资源未解压缩。复制或解压缩目录
时,其行为与tar -x相同:结果为
,并集为:
If <src> is a local tar archive in a recognized compression format (identity, gzip, bzip2 or xz) then it is unpacked as a directory. Resources from remote URLs are not decompressed. When a directory is copied or unpacked, it has the same behavior as tar -x: the result is the union of:
1)目标路径上存在的任何内容;以及2)
源树的内容,已解决冲突以支持 2。
1) Whatever existed at the destination path and 2) The contents of the source tree, with conflicts resolved in favor of "2." on a file-by-file basis.
尝试:
ADD /files/apache-stratos.zip /opt/
看看文件是否在那里,而无需进一步解压缩。
and see if the files are there, without further decompression.
这篇关于Docker如何在不将其提交到映像的情况下添加文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!