Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Use Pulled Image as a Cache with `docker buildx`

We're building Docker images in our CI, but we can't get docker buildx build to utilise a pulled image as a cache.

Here are the docker commands that are executed:

docker pull "ghcr.io/foo/bar/baz"
docker buildx build . --tag "ghcr.io/foo/bar/baz"
docker push "ghcr.io/foo/bar/baz"

How can we modify docker buildx build to use the pulled image as a cache, ensuring that not every RUN is executed if the command remains unchanged?

Here's our Dockerfile:

FROM amd64/alpine:3.16 as build0

RUN apk update && apk add autoconf bash [...]
FROM build0 as build1

RUN mkdir /tmp/build-deps && cd /tmp/build-deps && [...]

FROM build1 as build2
RUN cd /tmp/build-deps && wget ${patch_uri} && [...]

FROM build2 as build3
RUN mkdir /root/build-deps && [...]

FROM build3 as build4
RUN cd /tmp/build-deps/ && mkdir php-ext && cd php-ext && [...]
like image 987
Till Avatar asked Oct 21 '25 00:10

Till


1 Answers

Use the --cache-from option when building with docker buildx

docker pull "ghcr.io/foo/bar/baz"
docker buildx build . --tag "ghcr.io/foo/bar/baz" --cache-from="ghcr.io/foo/bar/baz"
docker push "ghcr.io/foo/bar/baz"

By specifying this option, Docker will re-use layers from the pulled image where the initial steps have not changed in the Dockerfile and the layers are the same.

like image 73
djmonki Avatar answered Oct 23 '25 11:10

djmonki