Jason
Jason

Reputation: 2887

How to get Docker to stop using cached images or layers

I have a fairly simple Dockerfile that I am trying to build locally before pushing to Azure DevOps for CI/CD usage. The build is falling on the last line (the cause is not the purpose of this post) and I am trying to get the complete build process to "start over". No matter what I do, Docker continues to use cached layers (images??) despite me deleting everything and telling the system to not use cache.

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

I am executing the build using the following command

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 .

As you can see, I am specifying both no-cache and pull arguments.

Both docker image ls --all and docker container ls --all show nothing.

I have also executed the following commands

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

And maybe a few others that I cannot even remember as I have been researching this issue. Unless I edit the beginning lines of the Dockerfile first, no matter what I do, the output ends up being similar to

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

As you can see, some commands are still coming from cached results.

My docker version info is

 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

Upvotes: 1

Views: 586

Answers (1)

dakiniishi
dakiniishi

Reputation: 26

could be related to this bug: https://github.com/moby/buildkit/issues/1939

Try switching off buildkit by setting an environment variable DOCKER_BUILDKIT=0

Upvotes: 1

Related Questions