Docker如何添加文件而不将其提交到图像? [英] Docker how to ADD a file without committing it to an image?

查看:20
本文介绍了Docker如何添加文件而不将其提交到图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个约 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?

一种选择是在开始 docker 构建之前运行一个简单的 Web 服务器(例如 python -m SimpleHTTPServer),然后使用 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 中的 ADD(带有目标路径,而不是路径 + 文件名),它会将文件解压缩到给定的目录中.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆