You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jun 3, 2025. It is now read-only.
Repository navigation
This repository was archived by the owner on Jun 3, 2025. It is now read-only.
Trying to figure out some way of sharing cache between builds, though about mounting some directory like /cache inside the build container, so we can then have a shared cache there for things like pip, npm or cargo.
Will be possible to implement something like that?
Layer cache is invalidated on any change so wanted to mount shared directory and keep for example python pip cache there, so it doesn't require to fetch that from network even one dependency has changed.
Something like -v for docker to mount a directory/image (which is also not supported for docker build)
@urbaniak going on the example of pip caching (which I don't know a ton about) I'm assuming pip looks for certain directories to check if a cache already exists. I would imagine you could just mount a volume at that directory into the kaniko docker container.
IIRC kaniko has some special handling for mounted volumes so I don't think it would cause issues for the image build if those cache files aren't directly referenced by the docker build, but I'm not positive.
In any case, I see no reason why it shouldn't work so if it doesn't we can certainly look into it
I second this need, what you explained @cvgw is correct:
pip, maven, npm, yarn and so on have cache directories for downloaded packages
usually one would instruct the CI to cache such directory so that it can be reused between builds
When using kaniko, the build itself has no access to the host filesystem, so it would be neat to be able to instruct kaniko to mount a certain directory into the build container so that pip and co can take advantage of it.
Reacted by Victor Noël, Seb Parkitny, Dmitry Fedorov, Kingdon Barrett, Guillaume Déflache, Ben Lowman, Unai Tens, Herman, Leonardo Melo, Nestor Manrique and 82 more
Reacted by Seb Parkitny, Dmitry Fedorov, Herman, Nestor Manrique, Alex Pavlov, ryan4yin | 二花, Yoann MALLEMANCHE, Daniel Andrei Mincă, John Amicangelo, Pierre and 9 more
@glen-84 I'm not exactly sure how this feature interact with the host filesystem? Because the use case I had in mind take advantage of the fact that the host filesystem (i.e., the CI machine) will have a directory with the cache available. I'm not sure if --mount=type=cache allows for this, does it?
If I understand correctly, the Docker engine on the host would manage the directory (sort of like a named volume). If you follow the link, they show an example of caching Go packages.
@glen-84 ok, so in the case of kaniko, since there are no docker engine on the host, I suspect it would mean adding an option to the kaniko cli to specify which directory to use when --mount=type=cache is used inside the image. This would elegantly allow to choose the desired directory in a CI context.
Still there would be some thinking to do as to how this interacts with the from and source option I suppose...
My thoughts were to "reuse" the syntax, as opposed to designing something completely specific to Kaniko. Kaniko could be updated to understand these types of mounts, and to manage the directories outside of the image (i.e. inside the Kaniko container).
Reacted by krzysztof urbaniak, Seb Parkitny, Daniel Schemmel, Nestor Manrique, Davide Madrisan, Michael Hixson, Capt. Blaster and David Roth
This is a must have feature. At minimum it should not err when buildkit flags are present. Right now i use a step that runs sed on the dockerfile to remove those options but I can not do this from skaffold (skaffold generates a single step).
Reacted by ryan4yin | 二花, pvalders, coperni and David Roth
Mounting a local directory as a volume inside Kaniko is a valid use case and can be achieved with a little bit of overhead.
In Google Cloud Build the working directory is already automatically mounted in the Kaniko (and any other container) under /workspacesee here, you can reference directly the /workspace directory and access your cache.
If you want to mount additional directories (e.g. because you want the cache in a specific location) you can always mount a volume in Kaniko, for example with:
Hey folks, kaniko is in maintenance mode and I am not actively working on it. That said, I am open for to help folks get this feature in.
@tejal29 I'm a bit motivated to do something regarding this but I'm a bit lost in the code base.
I had a quick look around an my naive thought is that one would could just remove the mount syntax from the parsed command for now in pkg/commands/run.go. How close or wrong am I?
Is there a solution out for this? Looks like it's been a year since the last commenter, but I can't imagine why there wouldn't be support for cache mounts?
I was able to get around this limitation using ONBUILD in the Dockerfile. This example is creating and using a cache for Poetry inside the container and then copying it into the GitLab project root so it can be uploaded to the remote cache. Subsequent builds pull it down to the project root so it can be uploaded as part of the docker context and copied into the container. By default it won't use the cache so it doesn't break when running locally and in GitLab I pass in BUILD_ENV=ci as a build arg which causes it to copy the cached directory into the container.
ARG BUILD_ENV=local
# Install poetry and setup environment# This is done in an upstream stage because the ONBUILD COPY will be inserted directly after# the FROM of the downstream build and we don't want to have to re-install poetry every time# the cache is downloaded (every build).# see https://docs.docker.com/engine/reference/builder/#onbuildFROM python:3.7-slim-bullseye as poetry
ENV POETRY_VERSION=1.5.0
RUN pip install poetry==${POETRY_VERSION}
ENV VIRTUAL_ENV /venv
RUN python -m venv ${VIRTUAL_ENV}
ENV PATH="${VIRTUAL_ENV}/bin:${PATH}"ENV POETRY_NO_INTERACTION=true \
POETRY_VIRTUALENVS_IN_PROJECT=false \
POETRY_VIRTUALENVS_PATH=${VIRTUAL_ENV} \
POETRY_VIRTUALENVS_CREATE=false
# If running on CI, copy the poetry cache from the GitLab project root to the containerFROM poetry as poetry_ci
ONBUILDCOPY .cache/pypoetry /root/.cache/pypoetry/
# If running on local, don't do anythingFROM poetry as poetry_local
# Install the projectFROM poetry_${BUILD_ENV} as venv
COPY pyproject.toml poetry.lock ./
RUN touch README.md && \
poetry install --only main
# Build final imageFROM python:3.7-slim-bullseye as final
ENV PATH="/venv/bin:${PATH}"COPY --from=venv /venv /venv
# Copy in the app, set user, entrypoint, etc
The --ignore-path is needed if you use multi-stage builds and the path you want to cache isn't in the final target that's built. If you're not using multi-stage builds, I don't think you need to pass that since the path will be available in the filesystem after kaniko has finished running but then you'd have the cache in your image which you probably don't want. The files are then copied to the project root which is where it needs to be for GitLab to be able to cache it.
Reacted by Victor Noël, boris-pfahringer-bdr, Myles Steinhauser, bcmyguest and Hieast
Another alternative that we are using for per-process caching which doesn't require the cache to be filled in advance (however it does not work for the first install of each project, but we are ok with that, it can be mixed with @trevorlauder approach).
We just store somewhere the last image built (SSM parameters) for each project. And then using multistage copy we just copy the cache folder (poetry in our case) from the latest-known valid image (in our case we just copy the venv to speed it up even more).
PREVIOUS_IMAGE can be scratch or the "name" of the previous built image for that project.
FROM --platform=linux/amd64 $PREVIOUS_IMAGE as previous_image
RUN mkdir -p /project/.venv
COPY .aws /root/.aws
COPY . /project_new
FROM --platform=linux/amd64 $PYTHON_BASE_IMAGE as base_image
COPY --from=previous_image /root/.aws /root/.aws
COPY --from=previous_image /project_new /project
# Restore the project folder from the previous image to get its venv
COPY --from=previous_image /project/.venv /project/.venv
I don't think we actually need to copy the cache folder explicitly as in @trevorlauder's approach above.
Two realizations were key for me:
Since the executor binary is simply running as a process inside the host container without any redirection, it has, by default, access to every folder in the host container. AFAIU the /kaniko folder is only special in the sense that it is not added to the final image. But you are free to create any additional folders in the host container for the build and then discard them from the final image with --ignore-path
kaniko automatically discards mounted folders on the host from the final image (via DetectFilesystemIgnoreList). In the case of Gitlab CI, the cache: directive mounts the cache into the provided subfolder within ${CI_PROJECT_DIR}, so we don't even need to explicitly --ignore-path it.
All in all, to make it work, just tell Gitlab CI to mount the cache as usual into the host container, then use it inside the Dockerfile during build (I am using build args to pass the cache path).
It will be accessible because of 1., and it will not go into the final image because of 2.
If it is read-write (default), the Gitlab-managed cache will also automatically be updated afterwards.
Here is my stripped-down config for employing a pip cache during kaniko build within Gitlab CI.
FROM python:3.12-slim
# Path to pip cache on host containerARG PIP_CACHE_DIR
COPY requirements.txt /app/
WORKDIR /app
RUN pip install -r requirements.txt
For compatibility with local docker builds, I believe one could add --mount=type=cache to the RUN pip install command - it will be ignored by kaniko. (untested)
Reacted by JMSwag, premonts, IndranilNandy, Haydn Evans, Myles Steinhauser, Daniel Andrei Mincă, Kernie, tyler92 and Maicon Vieira
Thanks to @pe224 i followed his approach and it worked.
Let me add one more thing when you source code is located in the root of ${CI_PROJECT_DIR} for gitlab and you also have a test job for instance before the build job and you want m2 cache to be shared between jobs.
if you do something like this for a java maven project
Then you will also copy the m2 cache in a docker layer and not use the host one this means it will not be updated on the host and not cached by gitlab runner.
What i did on my side is move my code to code folder and do this instead
Activity
tejal29 commentedon Jan 17, 2020
We do have a cache warmer feature for caching base layers.
https://github.com/GoogleContainerTools/kaniko#caching
The intermediate layers are caches remotely. Are you looking for caching layers locally?
Would that help?
urbaniak commentedon Jan 17, 2020
Layer cache is invalidated on any change so wanted to mount shared directory and keep for example python pip cache there, so it doesn't require to fetch that from network even one dependency has changed.
Something like -v for docker to mount a directory/image (which is also not supported for
docker build)cvgw commentedon Jan 17, 2020
@urbaniak going on the example of pip caching (which I don't know a ton about) I'm assuming pip looks for certain directories to check if a cache already exists. I would imagine you could just mount a volume at that directory into the kaniko docker container.
IIRC kaniko has some special handling for mounted volumes so I don't think it would cause issues for the image build if those cache files aren't directly referenced by the docker build, but I'm not positive.
In any case, I see no reason why it shouldn't work so if it doesn't we can certainly look into it
victornoel commentedon Feb 14, 2020
I second this need, what you explained @cvgw is correct:
When using kaniko, the build itself has no access to the host filesystem, so it would be neat to be able to instruct kaniko to mount a certain directory into the build container so that pip and co can take advantage of it.
glen-84 commentedon May 1, 2020
What about supporting RUN --mount=type=cache?
victornoel commentedon May 2, 2020
@glen-84 I'm not exactly sure how this feature interact with the host filesystem? Because the use case I had in mind take advantage of the fact that the host filesystem (i.e., the CI machine) will have a directory with the cache available. I'm not sure if
--mount=type=cacheallows for this, does it?glen-84 commentedon May 2, 2020
@victornoel,
If I understand correctly, the Docker engine on the host would manage the directory (sort of like a named volume). If you follow the link, they show an example of caching Go packages.
See also https://stackoverflow.com/a/57934504/221528.
(PS. You might want to consider up-voting this issue to indicate your interest.)
victornoel commentedon May 2, 2020
@glen-84 ok, so in the case of kaniko, since there are no docker engine on the host, I suspect it would mean adding an option to the kaniko cli to specify which directory to use when
--mount=type=cacheis used inside the image. This would elegantly allow to choose the desired directory in a CI context.Still there would be some thinking to do as to how this interacts with the
fromandsourceoption I suppose...glen-84 commentedon May 2, 2020
My thoughts were to "reuse" the syntax, as opposed to designing something completely specific to Kaniko. Kaniko could be updated to understand these types of mounts, and to manage the directories outside of the image (i.e. inside the Kaniko container).
7 remaining items
dimovnike commentedon Jul 9, 2021
This is a must have feature. At minimum it should not err when buildkit flags are present. Right now i use a step that runs sed on the dockerfile to remove those options but I can not do this from skaffold (skaffold generates a single step).
tejal29 commentedon Jul 10, 2021
Hey folks, kaniko is in maintenance mode and I am not actively working on it.
That said, I am open for to help folks get this feature in.
nahum-litvin-hs commentedon Aug 25, 2021
im also very interested in this feature.
gianluca-venturini commentedon Oct 19, 2021
Mounting a local directory as a volume inside Kaniko is a valid use case and can be achieved with a little bit of overhead.
In Google Cloud Build the working directory is already automatically mounted in the Kaniko (and any other container) under
/workspacesee here, you can reference directly the/workspacedirectory and access your cache.If you want to mount additional directories (e.g. because you want the cache in a specific location) you can always mount a volume in Kaniko, for example with:
at this point we just need to pre-populate the volume with the cache content, for example downloading it from a bucket:
or from a local directory:
Hope this helps until Cloud Build will allow to mount a local directory as a volume (maybe it already does, but I wasn't able to find a way).
byrnedo commentedon Dec 9, 2021
@tejal29 I'm a bit motivated to do something regarding this but I'm a bit lost in the code base.
I had a quick look around an my naive thought is that one would could just remove the mount syntax from the parsed command for now in pkg/commands/run.go. How close or wrong am I?
guillaume-d commentedon Apr 29, 2022
FYI Buildah >= 1.24 (which is shipped with Podman >= 4) supports RUN --mount=type=cache.
anthonyalayo commentedon May 10, 2023
Is there a solution out for this? Looks like it's been a year since the last commenter, but I can't imagine why there wouldn't be support for cache mounts?
https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/reference.md#run---mounttypecache
trevorlauder commentedon May 27, 2023
I was able to get around this limitation using
ONBUILDin the Dockerfile. This example is creating and using a cache for Poetry inside the container and then copying it into the GitLab project root so it can be uploaded to the remote cache. Subsequent builds pull it down to the project root so it can be uploaded as part of the docker context and copied into the container. By default it won't use the cache so it doesn't break when running locally and in GitLab I pass in BUILD_ENV=ci as a build arg which causes it to copy the cached directory into the container.In GitLab:
The
--ignore-pathis needed if you use multi-stage builds and the path you want to cache isn't in the final target that's built. If you're not using multi-stage builds, I don't think you need to pass that since the path will be available in the filesystem after kaniko has finished running but then you'd have the cache in your image which you probably don't want. The files are then copied to the project root which is where it needs to be for GitLab to be able to cache it.TinoSM commentedon Sep 6, 2023
Another alternative that we are using for per-process caching which doesn't require the cache to be filled in advance (however it does not work for the first install of each project, but we are ok with that, it can be mixed with @trevorlauder approach).
We just store somewhere the last image built (SSM parameters) for each project. And then using multistage copy we just copy the cache folder (poetry in our case) from the latest-known valid image (in our case we just copy the venv to speed it up even more).
PREVIOUS_IMAGE can be scratch or the "name" of the previous built image for that project.
giepa commentedon Jan 4, 2024
Need support for
-mount=type=cacheas wellSilentCatD commentedon Mar 21, 2024
Any update on this?
IndranilNandy commentedon Jun 7, 2024
This is a must needed feature still missing, which is making it hard to choose Kaniko.
pe224 commentedon Jun 11, 2024
I don't think we actually need to copy the cache folder explicitly as in @trevorlauder's approach above.
Two realizations were key for me:
executorbinary is simply running as a process inside the host container without any redirection, it has, by default, access to every folder in the host container. AFAIU the/kanikofolder is only special in the sense that it is not added to the final image. But you are free to create any additional folders in the host container for the build and then discard them from the final image with--ignore-pathDetectFilesystemIgnoreList). In the case of Gitlab CI, thecache:directive mounts the cache into the provided subfolder within${CI_PROJECT_DIR}, so we don't even need to explicitly--ignore-pathit.All in all, to make it work, just tell Gitlab CI to mount the cache as usual into the host container, then use it inside the Dockerfile during build (I am using build args to pass the cache path).
It will be accessible because of 1., and it will not go into the final image because of 2.
If it is read-write (default), the Gitlab-managed cache will also automatically be updated afterwards.
Here is my stripped-down config for employing a pip cache during kaniko build within Gitlab CI.
gitlab-ci.yml
Dockerfile
For compatibility with local docker builds, I believe one could add
--mount=type=cacheto theRUN pip installcommand - it will be ignored by kaniko. (untested)premonts commentedon Jun 13, 2024
Thanks to @pe224 i followed his approach and it worked.
Let me add one more thing when you source code is located in the root of ${CI_PROJECT_DIR} for gitlab and you also have a test job for instance before the build job and you want m2 cache to be shared between jobs.
if you do something like this for a java maven project
Then you will also copy the m2 cache in a docker layer and not use the host one this means it will not be updated on the host and not cached by gitlab runner.
What i did on my side is move my code to code folder and do this instead
if you don't want to have code folder i assume you can still do something like this
This way the cache stays on the host and will be updated during the build.