I'm trying to generate and re-use a yarn install
cache when building a Docker image using Docker BuildKit. The yarn cache is persisted in the directory .yarn/cache
and should never be included in the final image (.yarn/cache
is relative to the build context root). The .yarn/cache
directory should be shared among multiple builds in order to always start from a warm cache and have a fast yarn install
command (even when we have a cache miss due to a change in package.json
). If we could have access to .yarn/cache
content after docker build
ends, will be easy to share between multiple builds, for example uploading it to an Amazon S3 or GCS bucket.
I've considered two options:
RUN --mount=type=bind
RUN --mount=type=cache
Described below why either of the two methods don't work.
(1) RUN --mount=type=bind
The (simplified) Dockerfile looks like this:
ENV YARN_CACHE_FOLDER ".yarn/cache"
COPY package.json yarn.lock ./
RUN --mount=type=bind,source=.yarn/cache,target=.yarn/cache,rw yarn install --frozen-lockfile
Unfortunately no data is present in .yarn/cache
directory after docker build
command ends.
The reason that no data is persisted is described in the rw
option documentation: Allow writes on the mount. Written data will be discarded
. If the written data is discarded, what's a working method for generating the cache the first time?
(2) RUN --mount=type=cache
Alternatively I considered using RUN --mount=type=cache
. Unfortunately there doesn't seem to be an easy way of persisting the cache in a local directory of the build host for being easily saved to an Amazon S3 or GCS bucket. If the cache is not persisted, we can't use it across different Cloud Builds if the Docker daemon state is not shared between them.
To say it in another way: what is the best method for sharing a cache directory between different docker build
that are running on different machines, without including this cache in the image? Is there any other way I'm missing here?
RUN --mount=type=bind
: allow to mount a directory as if it was local, but effectively doesn't allow to write to that directory, so I can't generate the cache on the first run.RUN --mount=type=cache
: allow to share the cache between multiple builds on the same machine, but if we're running multiple differentdocker build
(on different machines) it won't help because the cache is always empty.