I want to build my project using Gradle docker image. Since I need to feed it proper repository URL and credentials, I decided to output them into ~/.gradle/gradle.properties
, just like you usually do it locally on your machine.
However I am stuck on an issue of not being able to create any files inside ~/.gradle
.
Here’s a sample Dockerfile:
FROM gradle:8.5-jdk21-alpine
RUN ls -la .
RUN echo hello > out
RUN ls -la .
WORKDIR .gradle
RUN ls -la .
RUN echo world > out
RUN ls -la .
Which gives the following output:
Step 1/8 : FROM gradle:8.5-jdk21-alpine
---> 5d527d32c7be
Step 2/8 : RUN ls -la .
---> Running in 5195db2c8c4f
total 12
drwxr-srwx 1 gradle gradle 4096 Jan 27 08:42 .
drwxr-xr-x 1 root root 4096 Jan 27 08:42 ..
drwxr-srwx 2 gradle gradle 4096 Jan 27 08:42 .gradle
---> Removed intermediate container 5195db2c8c4f
---> 18666307c9c5
Step 3/8 : RUN echo hello > out
---> Running in b7f9dfb48848
---> Removed intermediate container b7f9dfb48848
---> 0dded2505644
Step 4/8 : RUN ls -la .
---> Running in 7c7342768fd0
total 16
drwxr-srwx 1 gradle gradle 4096 Feb 9 17:02 .
drwxr-xr-x 1 root root 4096 Jan 27 08:42 ..
drwxr-srwx 2 gradle gradle 4096 Jan 27 08:42 .gradle
-rw-r--r-- 1 root gradle 6 Feb 9 17:02 out
---> Removed intermediate container 7c7342768fd0
---> a50b7ebba4f3
Step 5/8 : WORKDIR .gradle
---> Running in d2fc92439198
---> Removed intermediate container d2fc92439198
---> 1537b03de558
Step 6/8 : RUN ls -la .
---> Running in 68e6257e2cc3
total 8
drwxr-srwx 2 gradle gradle 4096 Jan 27 08:42 .
drwxr-srwx 1 gradle gradle 4096 Feb 9 17:02 ..
---> Removed intermediate container 68e6257e2cc3
---> 9ac58a7ff572
Step 7/8 : RUN echo world > out
---> Running in 41543dabd95f
---> Removed intermediate container 41543dabd95f
---> ca7b08f161fc
Step 8/8 : RUN ls -la .
---> Running in 27d1c45d32b6
total 8
drwxr-srwx 2 gradle gradle 4096 Jan 27 08:42 .
drwxr-srwx 1 gradle gradle 4096 Feb 9 17:02 ..
---> Removed intermediate container 27d1c45d32b6
---> a7ef272b3325
Why does this happen? Does this folder have some kind of special handling? Why are no errors raised? Is there a more idiomatic way to feed repository credentials to a gradle container?
EDIT: Found out from gradle dockerfile sources that this directory is acatually an anonymous volume mount. I tried changing group ownership of /var/lib/docker
to group docker
, which my user is part of, but that didn’t help either.
2
Answers
Figured out why this was happening with the help of this answer. Volume changes are thrown away after every step, which can be verified by modifying the
echo
command to also includels
in the same line:This gives the following output:
Evidently, the file does exist, but only at that specific step.
As for my use case, I'll probably end up simply creating
gradle.properties
in the project directory instead.I’m not sure that this is the best way to feed this information into your Docker image, but here’s something that does what I believe you want.
🗎
Dockerfile
That assumes that you have a
gradle.properties
file in your local directory, which is then being copied across into.gradle/
on the image.Other options for sharing this information with your image would be:
gradle.properties
file;gradle.properties
file from the host with a running container; orFor the purpose of illustration I created a
gradle.properties
with the following content:In Step 3/4 you can see that the file is indeed in the
.gradle
folder and in Step 4/4 you can see that the content of the file on the image is correct.