# Why gradle does not use cache in docker

**URL:** <https://discuss.gradle.org/t/why-gradle-does-not-use-cache-in-docker/33902>\
**Category:** Help/Discuss\
**Created:** [November 18, 2019, 1:33am UTC](https://discuss.gradle.org/t/why-gradle-does-not-use-cache-in-docker/33902 "2019-11-18T01:33:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kgmaven](https://avatars.discourse-cdn.com/v4/letter/k/b2d939/32.png) [@kgmaven](https://discuss.gradle.org/u/kgmaven)\
**Post date:** [November 18, 2019, 1:33am UTC](https://discuss.gradle.org/t/why-gradle-does-not-use-cache-in-docker/33902/1 "2019-11-18T01:33:16Z")

</div>

My Dockerfile:

```gradle
FROM gradle:jdk8 as gradle
WORKDIR /app

COPY settings.gradle build.gradle ./
COPY module-core/build.gradle ./module-core/
COPY gradle ./gradle
RUN gradle build --no-daemon --info --stacktrace #make gradle download dependencies 
COPY . .
RUN gradle :module-core:bootJar --no-daemon --info --stacktrace #should reuse the dependencies

```

As shown I copy the gradle configuration files without the source codes to make gradle download the dependencies, and I can make sure that gradle download a lot of dependencies through the log.

Then I copy the source codes and run the build again, I expect that gradle will use the cached depedencies , however I found it download them again.

What’s going on?

---

<div class="post-metadata">

**Author:** ![mark\_vieira](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_vieira/32/5396_2.png) [@mark\_vieira](https://discuss.gradle.org/u/mark_vieira)\
**Post date:** [November 21, 2019, 4:33am UTC](https://discuss.gradle.org/t/why-gradle-does-not-use-cache-in-docker/33902/2 "2019-11-21T04:33:06Z")

</div>

Because the Gradle cache lives in the `~/.gradle` on the host. When running in a Docker container that folder is going to be empty. You could mount that Gradle user home directory into the Docker container, but there are complications with that and it isn’t fully supported. However, there is preliminary work which will be available in Gradle 6.1 to make this possible.

> <https://github.com/gradle/gradle/issues/1338>
>
> Original issue: https://issues.gradle.org/browse/GRADLE-2690
> Highly votes issue: 24
> Expected Behavior
> The cache can be transferred from one machine to another and can be used in...

---

<div class="post-metadata">

**Author:** ![kgmaven](https://avatars.discourse-cdn.com/v4/letter/k/b2d939/32.png) [@kgmaven](https://discuss.gradle.org/u/kgmaven)\
**Post date:** [November 21, 2019, 5:17am UTC](https://discuss.gradle.org/t/why-gradle-does-not-use-cache-in-docker/33902/3 "2019-11-21T05:17:09Z")

</div>

> When running in a Docker container that folder is going to be empty

At the first build time it is ok if the folder is empty, but after the first build. Docker should reuse the cached layer where the `~/.gradle` should contain the jars.

---

<div class="post-metadata">

**Author:** ![mark\_vieira](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_vieira/32/5396_2.png) [@mark\_vieira](https://discuss.gradle.org/u/mark_vieira)\
**Post date:** [November 23, 2019, 1:07am UTC](https://discuss.gradle.org/t/why-gradle-does-not-use-cache-in-docker/33902/4 "2019-11-23T01:07:45Z")

</div>

> [@kgmaven](#):
>
> At the first build time it is ok if the folder is empty, but after the first build. Docker should reuse the cached layer where the `~/.gradle` should contain the jars.

This is only the case if you run multiple builds in the same container, or use persistent volumes between containers, otherwise, everything I spin up a new container that folder will be empty.

---

<div class="post-metadata">

**Author:** ![kgmaven](https://avatars.discourse-cdn.com/v4/letter/k/b2d939/32.png) [@kgmaven](https://discuss.gradle.org/u/kgmaven)\
**Post date:** [December 3, 2019, 3:22am UTC](https://discuss.gradle.org/t/why-gradle-does-not-use-cache-in-docker/33902/5 "2019-12-03T03:22:52Z")

</div>

But why the following work:

```gradle
FROM node:10-alpine as node
WORKDIR /app
COPY package*.json ./
RUN npm install 
ADD . .
RUN npm run build

```

The last line `npm run build` will reuse the node dependencies downloaded in the step `npm install`.

The only difference is that the node dependencies will be saved in the current directory while gradle dependencies will be put outside the current directory.

---

<div class="post-metadata">

**Author:** ![mark\_vieira](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_vieira/32/5396_2.png) [@mark\_vieira](https://discuss.gradle.org/u/mark_vieira)\
**Post date:** [December 3, 2019, 6:37pm UTC](https://discuss.gradle.org/t/why-gradle-does-not-use-cache-in-docker/33902/6 "2019-12-03T18:37:51Z")

</div>

Ok, I think I understand what you are referring to now. You are indeed running multiple builds in the same container, and the later builds still download dependencies? Could you perhaps run those builds with `--scan` and share the [build scan](https://guides.gradle.org/creating-build-scans/) URLs?

---

<div class="post-metadata">

**Author:** ![kgmaven](https://avatars.discourse-cdn.com/v4/letter/k/b2d939/32.png) [@kgmaven](https://discuss.gradle.org/u/kgmaven)\
**Post date:** [December 4, 2019, 2:19am UTC](https://discuss.gradle.org/t/why-gradle-does-not-use-cache-in-docker/33902/7 "2019-12-04T02:19:27Z")

</div>

Sorry but I can not paste the build scan because of company restriction.

But I tried to stop the build after the `gradle build` complete.

```gradle
FROM gradle:5.6.4-jdk8 as gradle
WORKDIR /app

COPY settings.gradle build.gradle ./
COPY module-core/build.gradle ./module-core/
COPY gradle ./gradle
RUN gradle build --no-daemon --info --stacktrace --scan
# RUN ....

```

Then use `docker run` to go inside the container where I expect to find a lot of jars in `/root/.gradle`, however the directory is empty.

Then I guess the following build steps have to down the files again and again.

---

<div class="post-metadata">

**Author:** ![mark\_vieira](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_vieira/32/5396_2.png) [@mark\_vieira](https://discuss.gradle.org/u/mark_vieira)\
**Post date:** [December 4, 2019, 5:00pm UTC](https://discuss.gradle.org/t/why-gradle-does-not-use-cache-in-docker/33902/8 "2019-12-04T17:00:29Z")

</div>

It’s possible there are different semantics regarding user home directories when _build_ a Docker image vs running a container.

One thing you might try is using an explicit Gradle user home directory by passing a location via `-g`. Try using the same location for both build invocations and see if that helps.

---

<div class="post-metadata">

**Author:** ![Keegan\_Witt](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/keegan_witt/32/542_2.png) [@Keegan\_Witt](https://discuss.gradle.org/u/Keegan_Witt)\
**Post date:** [March 1, 2020, 5:15pm UTC](https://discuss.gradle.org/t/why-gradle-does-not-use-cache-in-docker/33902/9 "2020-03-01T17:15:19Z")

</div>

The image has `VOLUME /home/gradle/.gradle`. This was done because CI containers tend to be ephemoral and this gives you the option to mount them in a volume so it can be re-used between containers (and generally child images are meant to be a non-project specific image that is used for many projects, and thus avoids having dependencies for any particular project in the image).

However, it is [not currently possible](https://github.com/moby/moby/issues/3465) to unset this decision in child images, so if you want the downloaded dependencies included in the image, the fix would be to choose another home directory as @mark_vieira has suggested . mrhaki describes some methods for doing that here: [https://mrhaki.blogspot.com/2010/09/gradle-goodness-changing-gradle-user.html](https://mrhaki.blogspot.com/2010/09/gradle-goodness-changing-gradle-user.html).
