Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

layer created implicitly by WORKDIR is not cached #3340

Open
mzihlmann opened this issue Oct 11, 2024 · 3 comments · May be fixed by #3341
Open

layer created implicitly by WORKDIR is not cached #3340

mzihlmann opened this issue Oct 11, 2024 · 3 comments · May be fixed by #3341

Comments

@mzihlmann
Copy link

mzihlmann commented Oct 11, 2024

Actual behavior
When WORKDIR is called on a non-existent directory, kaniko is kind enough to create that directory for you, resulting in a layer being added. However, kaniko does not cache that layer, which means that on every invocation a completely new image is emitted from that point onwards. Inside the same stage this is non-obvious as caching mechanism still pulls, so you get a 100% cache hitrate thereafter, but the image is completely new. In multistage builds or builds that depend on the newly emitted image, this is catastrophic, as they do consider the entire image's sha when determining whether a cache is hit or not, so this will invalidate the entire cache.

Workaround is simple but silly, we just need to make sure that the directory exists before calling WORKDIR

RUN mkdir /app
WORKDIR /app

Expected behavior

When WORKDIR is called on a non-existent directory, that directory is implicitly created and the resulting layer is pushed into cache.

To Reproduce

FROM ubuntu:latest
WORKDIR /app
RUN echo "whatever"

run this build multiple times, with cache, and observe that the layers change every time

docker inspect <image>

Triage Notes for the Maintainers

Description Yes/No
Please check if this a new feature you are proposing
Please check if the build works in docker but not in kaniko
Please check if this error is seen when you use --cache flag
Please check if your dockerfile is a multistage dockerfile
@mzihlmann
Copy link
Author

this is the underlying cause of #3246

@mzihlmann
Copy link
Author

problem is here https://github.com/GoogleContainerTools/kaniko/blob/main/pkg/commands/workdir.go#L74
we add to the snapshot but don't cache it.

@nogweii
Copy link

nogweii commented Nov 13, 2024

Just ran into this issue. Would love to see 3341 merged!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants
@nogweii @mzihlmann and others