[英]In a dockerfile, how to delete a directory and its contents from docker image? (windows)
I'm using the python:3.6.5-windowsservercore
base image.我正在使用
python:3.6.5-windowsservercore
基本映像。
COPY. /app
COPY. /app
copies the build context into C:\app
. COPY. /app
将构建上下文复制到C:\app
中。 This includes certain secret keys required for building the project.这包括构建项目所需的某些密钥。
After running the build, I want to delete keys folder (C:\app\keys), for this I use:运行构建后,我想删除密钥文件夹(C:\app\keys),为此我使用:
RUN powershell.exe Remove-Item -Path C:\app\keys -Force
Note - I have also tried each of following alternatives:注意 - 我还尝试了以下每个替代方案:
RUN Remove-Item -Path C:\app\keys -Force
RUN RD /S /Q C:\app\keys
This gives me following error:这给了我以下错误:
Step 10/14 : RUN powershell.exe Remove-Item -Path C:\app\keys -Force
---> Running in 4e22124332b1
Remove-Item : Object reference not set to an instance of an object.
At line:1 char:1
+ Remove-Item -Path C:\app\keys -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Remove-Item], NullReferenceEx
ception
+ FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShe
ll.Commands.RemoveItemCommand
The command 'powershell -Command $ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue'; powershell.exe Remove-Item -Path C:\app\keys -Force' returned a non-zero code: 1
What is the way to delete directory inside the image?删除图像内目录的方法是什么?
Solution:解决方案:
RUN Remove-Item -Path C:\app\keys -Force -Recurse
There are 2 aspects to resolve this.有2个方面可以解决这个问题。
Based on your base image, what is your default shell?根据您的基本映像,您的默认 shell 是什么? Bash or powershell?
Bash 还是 powershell? In my case it was powershell, so there is no need to mention powershell.exe at the beginning of the command.
在我的例子中,它是 powershell,所以不需要在命令开头提到 powershell.exe。
The original command was incorrect RUN Remove-Item -Path C:\app\keys -Force
because the folder had sub-folders and files.原始命令不正确
RUN Remove-Item -Path C:\app\keys -Force
因为文件夹有子文件夹和文件。 So you have to mention -Recurse
所以不得不提
-Recurse
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.