kaniko icon indicating copy to clipboard operation
kaniko copied to clipboard

Cache will be invalidated if there is an instruction in Dockerfile that doesn't cause any changes in file system

Open phongvq opened this issue 3 years ago • 1 comments

Actual behavior

  • Docker cache will not work if there is an instruction in Dockerfile that doesn't produce any changes in the file system (e.g. instruction RUN echo helloworld) (cache of any layers after that instruction will be invalidate in the next build)

Expected behavior

  • Caching works in case there is an instruction Dockerfile that not produce changes in filesystem

To Reproduce

  1. put RUN echo helloworld in Dockerfile
FROM node:14.18.2

RUN npm i -g [email protected]

WORKDIR /app
COPY package.json .
COPY package-lock.json .

# echo abc will not cause any change in filesystem
RUN echo abc

################## cache of layers after this line cannot be re-used in the next build. 
COPY .npmrc .

RUN npm ci

COPY . .
RUN GENERATE_SOURCEMAP=false npm run build
  1. Run following command:
/kaniko/executor --context ${CI_PROJECT_DIR} \
      --cache=true \
      --snapshotMode=redo \
      --use-new-run=true \
      --cache-copy-layers=true \
      --dockerfile ${CI_PROJECT_DIR}/${TOPCV_DOCKERFILE_PATH} \
      ${docker_destination} \
      ${docker_build_args}
  1. Re-execute the command to observe caching behaviour

Additional Information

  • kaniko version: latest
  • flag --cache-copy-layers set to true
  • switch --snapshotMode flag from redo to full does not show the issue
  • use Gitlab Regsitry as cache + container registry
  • kaniko logs

build no. 1

INFO[0050] Running: [/bin/sh -c echo abc]
abc
INFO[0051] No files changed in this command, skipping snapshotting.

build no. 2

INFO[0007] No cached layer found for cmd RUN echo abc

Triage Notes for the Maintainers

Description Yes/No
Please check if this a new feature you are proposing
  • - [x]
Please check if the build works in docker but not in kaniko
  • - [x]
Please check if this error is seen when you use --cache flag
  • - [x]
Please check if your dockerfile is a multistage dockerfile
  • - [ ]

phongvq avatar Jul 23 '22 04:07 phongvq

Based on the logs, I guess the cause is:

  • first build, RUN echo abc doesn't cause file system change -> no snapshot was done
  • next build, cache of RUN echo abc was not found -> cache of all layer after echo layer will not be valid -> and all instruction will be executed.

Please correct me if I am wrong. Thanks very much.

phongvq avatar Jul 23 '22 04:07 phongvq