我有一个docker文件,该文件可以在临时映像中构建.Net Core API,然后使用生成的文件创建映像。据我所知,应该自动删除临时映像,但在我的测试中并未删除。我将Docker Desktop用于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>
查看带有名称和标签无的图像。
Docker版本为19.03.2。 可能是什么原因? 如何防止在构建后留下该悬空的图像?
答案 0 :(得分:1)
docker image rm [OPTIONS] IMAGE [IMAGE...]
选项
--force , -f Force removal of the image
--no-prune Do not delete untagged parents
答案 1 :(得分:0)
for image in $(docker images -f "dangling=true" -q)
do
docker rmi -f $image
done
或docker images -q -f "dangling=true" | xargs docker rmi
这里的关键是"dangling=true"
过滤器,它准确显示了构建阶段使用的那些中间图像。