首页 > 解决方案 > 在 dockerfile 中,如何从 docker 映像中删除目录及其内容?(视窗)

问题描述

我正在使用python:3.6.5-windowsservercore基本图像。

COPY . /app将构建上下文复制到C:\app. 这包括构建项目所需的某些密钥。

运行构建后,我想删除密钥文件夹(C:\app\keys),为此我使用:

RUN powershell.exe Remove-Item -Path C:\app\keys -Force

注意 - 我还尝试了以下每个替代方案:

RUN Remove-Item -Path C:\app\keys -Force
RUN RD /S /Q C:\app\keys

这给了我以下错误:

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

删除图像内目录的方法是什么?

标签: windowspowershelldockerdockerfiledocker-build

解决方案


解决方案:

RUN Remove-Item -Path C:\app\keys -Force -Recurse

有2个方面可以解决这个问题。

  1. 根据您的基本映像,您的默认 shell 是什么?bash 还是 powershell?就我而言,它是 powershell,因此无需在命令开头提及 powershell.exe。

  2. 原始命令不正确RUN Remove-Item -Path C:\app\keys -Force,因为该文件夹有子文件夹和文件。所以不得不提-Recurse


推荐阅读