ADD和COPY合并一个目录的内容和已经build立的目录

我想组装一个图像与我的dockerfile一起存储的一些文件。 我所期望的COPY或ADD是一种合并行为。 但是,如果使用这些指令,文件层次结构将被覆盖。 因此,如果父path中的文件位于path中,则不再可用。 (如果我想将一些文件保存在/ etc / mycoolstuff / filehere)

发生这种情况时,我无法再使用apt-get

我也看到了一些build议,将我的文件转换为焦油打包,但没有观察到任何不同。

为了清楚起见,这是其中一个问题(如果我改变顺序,build立,但无论如何它结束了,导致儿童图像将无法再使用apt):

ADD build/image-base.tgz / RUN apt-get clean -y && \ apt-get update && \ apt-get install unzip -y --no-install-recommends && \ apt-get install gosu -y --no-install-recommends && \ apt-get clean -y && \ apt-get autoclean -y && \ apt-get autoremove -y && \ rm -rf /var/lib/apt/lists/* Failed to fetch http://archive.ubuntu.com/ubuntu/dists/xenial/InRelease Temporary failure resolving 'archive.ubuntu.com' Failed to fetch http://archive.ubuntu.com/ubuntu/dists/xenial-updates/InRelease Temporary failure resolving 'archive.ubuntu.com' Failed to fetch http://archive.ubuntu.com/ubuntu/dists/xenial-backports/InRelease Temporary failure resolving 'archive.ubuntu.com' Failed to fetch http://security.ubuntu.com/ubuntu/dists/xenial-security/InRelease Temporary failure resolving 'security.ubuntu.com' Failed to fetch http://ppa.launchpad.net/webupd8team/java/ubuntu/dists/xenial/InRelease Temporary failure resolving 'ppa.launchpad.net' Some index files failed to download. They have been ignored, or old ones used instead. 

你想达到什么目的? 你真的想覆盖根目录吗? 那么你最好从头开始构build图像。 要做到这一点,你的Dockerfile必须FROM scratch

关于这个主题的官方Docker教程,包含多个发行版的脚本:

https://docs.docker.com/engine/userguide/eng-image/baseimages/

经过一番挖掘,我find了一个解决方法,用COPY和一个RUN来实现我正在寻找的行为。

 COPY build/image-base.tgz / RUN tar -xvf image-base.tgz --no-overwrite-dir 

不幸的是,这会创build一个新层,基础焦油将保持在那里。 如果它只是一些文本configuration文件,这不是一个问题。

解决COPY指令的一个奇怪的方法是使用wget,然后解压,这样我们可以删除tar文件。 如果我们在一个构build环境中存储工件并为它们创buildURL,那么wget将会诀窍…在这种情况下,我们只需要一个RUN,保持房子干净。