简体   繁体   English

在 dockerfile 中,如何从 docker 映像中删除目录及其内容? (视窗)

[英]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个方面可以解决这个问题。

  1. 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。

  2. 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.

相关问题 在Windows上以递归方式删除目录的内容 - Delete contents of a directory recursively on Windows 删除特殊目录Python的内容(Windows) - Delete Contents of a Special Directory Python (Windows) Dockerfile - 从 .NET Core Web API 中的 windows 目录添加文件 - Dockerfile - add file from a windows directory in the .NET Core Web API 如何创建一个Dockerfile在Windows 2016容器中运行Docker? - How to Create a Dockerfile to Run Docker in a Windows 2016 Container? 如何计划从下载目录删除数据-Windows - How to schedule delete data from download directory - Windows 在Windows上使用docker-machine将远程计算机目录挂载到其容器 - Mount remote machine directory to its container with docker-machine on windows 在Windows Java中,如何取消设置只读文件夹及其所有内容? - In Windows Java, how do I unset read only from a folder and all its contents? 如何在脚本的本地目录中包含Windows窗体的图像 - How to include an image for windows forms from the script's local directory 如何将 Windows 桌面上的目录配置为 Docker 卷? - How to configure as Docker volume a directory on the Desktop in Windows? Windows Docker Dockerfile 复制文件夹内的文件 - Windows Docker Dockerfile COPY file inside folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM