bohdan
bohdan

Reputation: 445

How to make Gradle cache persistent through Docker containers?

I have a Java(Gradle) project. A docker image is built in CI using Dockerfile:

FROM gradle:6.8.3-jdk11
COPY --chown=gradle:gradle . /home/gradle/app
WORKDIR /home/gradle/app
RUN gradle compileTestJava

Although gradle compileTestJava downloads all dependencies, when the container is run from the image, gradle cache turns out to be empty and all dependencies are being redownloaded.

How can I make gradle cache attached to the image so it would be used in containers?

Upvotes: 2

Views: 1405

Answers (2)

bohdan
bohdan

Reputation: 445

It appeared that .gradle folder is set as a VOLUME in gradle:6.8.3-jdk11 which prevents cache from being saved in the image.

The solution was to create a custom base gradle image without a volume.

Upvotes: 0

Sam
Sam

Reputation: 4284

Gradle caches artifacts in USER_HOME/.gradle folder. For this either you have to use Bind mounts or Volumes and attach your containers $USER_HOME/.gradle path to that volume.

For volumes,

docker volume create <your_volume_name>

And when running your container attach the volume like this

docker run -v <your_volume_name>:USER_HOME/.gradle yourImage:tag

or When you use a bind mount, a file or directory on the host machine is mounted into a container.

docker run --mount type=bind,source=<your_host_path>,target=/USER_HOME/.gradle yourImage:tag

Upvotes: 1

Related Questions