Docker Run cache mount does not cache apt-get dependencies

31 views Asked by At

I was using the following dockerfile for a container and I realised that the apt-get packages were not getting cached by docker.

#(This is a minimal example, these steps take place with other steps  
installing other software in between, they need to be separate)  
FROM ubuntu

RUN --mount=type=cache,target=/var/cache/apt \
    apt-get -y update && \
    apt-get install -y \
    build-essential checkinstall cmake pkg-config yasm \
    git gfortran
RUN --mount=type=cache,target=/var/cache/apt \
    apt-get dist-upgrade -y && \
    apt-get install -y --no-install-recommends openjdk-17-jdk

In this file, when I change the dependencies in the first RUN command and build the container again docker build -t test . all the packages are downloaded again (as seen in the network requests of my system).

It seems that the cache is overwritten by the second RUN command? Or is it that docker doesn't persist the cache over builds? Though this link says that only the difference in packages would be downloaded again.
I am using Docker version 26.0.0, build 2ae903e
Buildkit is the default builder with this docker, so it is not about using # syntax=docker/dockerfile:x.x.x

0

There are 0 answers