首页 > 解决方案 > Docker build 在构建多阶段 docker 文件时不会删除临时镜像

问题描述

我有一个 docker 文件,它在临时映像中构建我的 .Net Core API,然后使用生成的文件创建一个映像。据我所知,应该自动删除临时图像,但在我的测试中它没有被删除。我使用适用于 Windows 的 Docker 桌面。运行前和运行后的docker文件、镜像列表如下;

泊坞窗文件;

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-alpine AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-alpine
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "CoreAPI1.dll"]

运行前的图像列表;

PS D:\temp\CoreAPI1\CoreAPI1> docker image ls                                                                           
REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
mcr.microsoft.com/dotnet/core/sdk      2.2-alpine          3a2253204e79        4 weeks ago         1.48GB
mcr.microsoft.com/dotnet/core/aspnet   2.2-alpine          820b2f3a9c7a        4 weeks ago         168MB
PS D:\temp\CoreAPI1\CoreAPI1>

测试的命令;

docker build -t coreapi1 .
docker build --rm -t coreapi1 .

构建 docker 文件后的镜像列表;

PS D:\temp\CoreAPI1\CoreAPI1> docker image ls                                                                           
REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
coreapi1                               latest              d8cb00730c52        3 minutes ago       168MB
<none>                                 <none>              1105d14991b3        3 minutes ago       1.48GB
mcr.microsoft.com/dotnet/core/sdk      2.2-alpine          3a2253204e79        4 weeks ago         1.48GB
mcr.microsoft.com/dotnet/core/aspnet   2.2-alpine          820b2f3a9c7a        4 weeks ago         168MB
PS D:\temp\CoreAPI1\CoreAPI1>

查看带有名称和标签的图像none

Docker 版本是 19.03.2。可能是什么原因?如何防止在构建后留下这个悬空图像?

标签: dockerdockerfile

解决方案


docker image rm [OPTIONS] IMAGE [IMAGE...]

选项

--force , -f        Force removal of the image
--no-prune      Do not delete untagged parents

推荐阅读