windows Dockerfile - 在一个 RUN 命令中删除一个文件,它仍然存在于下一个 RUN 命令中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35962538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:29:41  来源:igfitidea点击:

Dockerfile - removing a file in one RUN command, it is still present in the next RUN command

windowsdocker

提问by Dzhuneyt

I have a Dockerfile (https://gist.github.com/hasMobi/e198555704ee57e84399) that have these two commands in sequence:

我有一个 Dockerfile ( https://gist.github.com/hasMobi/e198555704ee57e84399),其中有这两个命令:

RUN rm -frv /usr/share/nginx/html/*
RUN ls /usr/share/nginx/html/

When I look at the console while building hte image, I can clearly see 2 files being removed from that folder, but when the next RUN command comes, it lists the directory's contents and the files are still there?:

当我在构建 hte 映像时查看控制台时,我可以清楚地看到从该文件夹中删除了 2 个文件,但是当下一个 RUN 命令出现时,它列出了目录的内容并且文件还在那里?:

Step 6 : RUN rm -fry /usr/share/nginx/html/* 
 ---> Running in b9a69992e4e0 
removed '/usr/share/nginx/html/index.html' 
removed '/usr/share/nginx/html/index.php' 
 ---> 2bfe01cbd007 
Removing intermediate container b9a69992e4e0 
Step 7 : RUN is /usr/share/nginx/html/ 
 ---> Running in 08396f6029e1 
index.html 
index.php 
 ---> a6471052519d 

What is going on here? I understand that each RUN command creates a separate layer and one is isolated from the other, but isn't the second RUN command supposed to inherit the file system in the exact state that it was in the previous RUN command (with the 2 files gone)?

这里发生了什么?我知道每个 RUN 命令都会创建一个单独的层,并且一个层与另一个层是隔离的,但是第二个 RUN 命令不应该继承上一个 RUN 命令中的确切状态的文件系统(两个文件消失了) )?

回答by PaulC

I had a similar issue:

我有一个类似的问题:

   RUN rm -rf /full/path
   RUN ln -s /other /full/path

This fails because "/full/path" still exists on the second RUN. This workaround works:

这将失败,因为“/full/path”在第二次 RUN 中仍然存在。此解决方法有效:

   RUN rm -rf /full/path; ln -s /other /full/path

I don't understand the behavior but was able to work around it in my case.

我不理解这种行为,但在我的情况下能够解决它。

回答by ntwrkguru

Basically, the ADD commands from the base image are overwriting the RUN commands in your Dockerfile. See thisfor more information.

基本上,来自基础镜像的 ADD 命令会覆盖 Dockerfile 中的 RUN 命令。有关更多信息,请参阅内容。

Note: The first encountered ADD instruction will invalidate the cache for all following instructions from the Dockerfile if the contents of have changed. This includes invalidating the cache for RUN instructions. See the Dockerfile Best Practices guide for more information.

注意:如果 Dockerfile 的内容发生变化,第一个遇到的 ADD 指令将使 Dockerfile 中所有后续指令的缓存无效。这包括使 RUN 指令的缓存无效。有关更多信息,请参阅 Dockerfile 最佳实践指南。

You may consider forking the source base image and using your customized version instead.

您可以考虑分叉源基础镜像并改用您的自定义版本。