首页 > 解决方案 > 如何让 Docker 停止使用缓存的图像或图层

问题描述

我有一个相当简单的Dockerfile,我正在尝试在本地构建它,然后再推送到 Azure DevOps 以供 CI/CD 使用。构建落在最后一行(原因不是这篇文章的目的),我试图让完整的构建过程“重新开始”。无论我做什么,尽管我删除了所有内容并告诉系统不要使用缓存,Docker 仍继续使用缓存层(图像??)。

FROM mcr.microsoft.com/dotnet/sdk:5.0-alpine AS build
WORKDIR /src

# Personal access token to access Artifacts feed

ARG ACCESS_TOKEN
ARG ARTIFACTS_ENDPOINT

# Install the Credential Provider to configure the access

RUN apk add bash

RUN wget -qO- https://aka.ms/install-artifacts-credprovider.sh | bash

# Configure the environment variables

ENV NUGET_CREDENTIALPROVIDER_SESSIONTOKENCACHE_ENABLED true
ENV VSS_NUGET_EXTERNAL_FEED_ENDPOINTS "{\"endpointCredentials\": [{\"endpoint\":\"${ARTIFACTS_ENDPOINT}\", \"password\":\"${ACCESS_TOKEN}\"}]}"

COPY . .

WORKDIR /src/Announcements.UI

RUN dotnet restore -s ${ARTIFACTS_ENDPOINT} -s https://api.nuget.org/v3/index.json
RUN dotnet build --no-restore -c Release -o /app/build

FROM build as publish

RUN dotnet publish --no-restore -c Release -o /app/publish

FROM nginx:alpine AS final
WORKDIR /usr/share/nginx/html

COPY --from=publish /app/publish/wwwroot .
COPY nginx.conf /etc/nginx/nginx.conf

我正在使用以下命令执行构建

docker builder build --build-arg ARTIFACTS_ENDPOINT=https://pkgs.dev.azure.com/some-url --build-arg ACCESS_TOKEN=some-token --pull --no-cache --progress plain  -f .\Announcements.UI\Dockerfile .

如您所见,我同时指定了no-cachepull参数。

两者都docker image ls --all没有docker container ls --all显示。

我还执行了以下命令

docker builder prune
docker build prune
docker container prune
docker system prune --all
docker system prune --volumes --all

也许还有一些我什至不记得的其他人,因为我一直在研究这个问题。除非我先编辑 Dockerfile 的开头行,否则无论我做什么,输出最终都会类似于

#1 [internal] load build definition from Dockerfile
#1 sha256:cbdbb2aa8016147768da39aaa3cf4a88c42355ea185072e6aec7d1bf37e95a95
#1 transferring dockerfile: 32B done
#1 DONE 0.0s

#2 [internal] load .dockerignore
#2 sha256:935b1c32fc5d8819aedb2128b068f58f226088632b15af29f9cf4caac88bd889
#2 transferring context: 35B done
#2 DONE 0.0s

#3 [internal] load metadata for docker.io/library/nginx:alpine
#3 sha256:b001d263a254f0e4960d52c837d5764774ef80ad3878c61304052afb6e0e9af2
#3 ...

#4 [internal] load metadata for mcr.microsoft.com/dotnet/sdk:5.0-alpine
#4 sha256:84368650d7715857ce3c251a792ea9a04a39b0cbc02b73ef90801d4be2c05b0f
#4 DONE 0.3s

#3 [internal] load metadata for docker.io/library/nginx:alpine
#3 sha256:b001d263a254f0e4960d52c837d5764774ef80ad3878c61304052afb6e0e9af2
#3 DONE 0.5s

#11 [internal] load build context
#11 sha256:123b3b80e7c135f4e2816bbcb5f2d704566ade8083a56fd10e8f2c6825b85db4
#11 transferring context: 6.41kB 0.0s done
#11 DONE 0.1s

#10 [build 4/8] RUN wget -qO- https://aka.ms/install-artifacts-credprovider.sh | bash
#10 sha256:d9ba90bfb15e2bafe5ba7621cd63377af7ec0ae7b30029507d2f41d88a17b7ed
#10 CACHED

#9 [build 3/8] RUN apk add bash
#9 sha256:a05883759b40f77aae0edcc00f1048f10b8e8c066a9aadd59a10fc879d4fd4df
#9 CACHED

#12 [build 5/8] COPY . .
#12 sha256:46509fe5fec5bb4235463dc240239f2529e48bcdc26909f0fb6861c97987ae93
#12 CACHED

#15 [build 8/8] RUN dotnet build --no-restore -c Release -o /app/build
#15 sha256:5f43c86c41c36f567ea0679b642bc7a5a8adc889d090b9b101f11440d7f22699
#15 CACHED
...

如您所见,一些命令仍然来自缓存结果。

我的docker版本信息是

 docker version
Client: Docker Engine - Community
 Cloud integration: 1.0.7
 Version:           20.10.2
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        2291f61
 Built:             Mon Dec 28 16:14:16 2020
 OS/Arch:           windows/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.2
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       8891c58
  Built:            Mon Dec 28 16:15:28 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.3
  GitCommit:        269548fa27e0089a8b8278fc4fc781d7f65a939b
 runc:
  Version:          1.0.0-rc92
  GitCommit:        ff819c7e9184c13b7c2607fe6c30ae19403a7aff
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

标签: dockerdockerfile

解决方案


可能与此错误有关:https ://github.com/moby/buildkit/issues/1939

尝试通过设置环境变量 DOCKER_BUILDKIT=0 来关闭 buildkit


推荐阅读