13

I'm using the new experimental docker buildkit syntax to do a multistage build, as so:

Dockerfile:

RUN --mount=type=cache,target=/home/build/.build-cache,gid=1000,uid=1001 ./build

bash:

DOCKER_BUILDKIT=1 docker build .

Works great locally. On CI I get a new docker environment every time, so no caching.

I can export and import files into the env, but I don't know where the cache is located. Any ideas?

Or should I be exporting/importing the cache via some docker command? I've read https://docs.docker.com/engine/reference/commandline/build/#specifying-external-cache-sources and https://github.com/moby/buildkit#export-cache but it's not clear to me which is buildkit specific, which docker specific or if either really applies to this cache mounted into the Dockerfile RUN command.

I have added a public gist here of a failing test demonstrating what I was hoping for: https://gist.github.com/Mahoney/85e8549892e0ae5bb86ce85339db1a71/6308f1bdb062a8982017193b96d61ec00a7698c5

And this later revision works, but I'm not happy with it - too much bootstrapping: https://gist.github.com/Mahoney/85e8549892e0ae5bb86ce85339db1a71

Robert Elliot
  • 1,372
  • 13
  • 20
  • You can use buildx w/ docker like `docker buildx build ...` and export / import the layer cache(s) from a registry, filesystem, or docker inline in your final image. https://medium.com/titansoft-engineering/docker-build-cache-sharing-on-multi-hosts-with-buildkit-and-buildx-eb8f7005918e and https://github.com/docker/buildx have more info. – jbielick Apr 22 '20 at 17:31
  • Thanks, will review - I felt there must be something I'm missing. – Robert Elliot May 15 '20 at 20:08
  • 1
    Just to follow up on this - the problem is that using gradle in a multi project build, layer caching isn't that useful, as I pretty much have to do a `COPY . .`, blowing away all layer caching every time anything changes. There are ways to gain from layer caching, but they are a pain to maintain. Sharing the gradle user home directory is by some distance the most sane way to cache. – Robert Elliot Jul 01 '20 at 15:53

1 Answers1

5

There doesn't seem to be any way to extract this specific cache from the general docker working files.

However, you can of course backup the whole of /var/lib/docker. This doesn't work for CircleCI's remote docker engine, because you don't have sudo access, but does work for GitHub Actions where you do.

See here for an example: https://github.com/Mahoney-playground/docker-cache-action

Robert Elliot
  • 1,372
  • 13
  • 20