skip to Main Content

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


  1. Chosen as BEST ANSWER

    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 include ls in the same line:

    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; ls -la .;
    RUN ls -la .
    

    This gives the following output:

    Step 1/8 : FROM gradle:8.5-jdk21-alpine
     ---> 5d527d32c7be
    Step 2/8 : RUN ls -la .
     ---> Running in 1d99342cfce8
    total 12
    drwxrwsrwx    1 gradle   ping          4096 Jan 27 08:42 .
    drwxrwxr-x    1 root     ping          4096 Jan 27 08:42 ..
    drwxrwsrwx    2 gradle   ping          4096 Jan 27 08:42 .gradle
     ---> Removed intermediate container 1d99342cfce8
     ---> 0c77d08c8085
    Step 3/8 : RUN echo hello > out
     ---> Running in c64200a76484
     ---> Removed intermediate container c64200a76484
     ---> a29955cb9ff5
    Step 4/8 : RUN ls -la .
     ---> Running in 7af67fd34245
    total 16
    drwxrwsrwx    1 gradle   ping          4096 Feb 17 07:03 .
    drwxrwxr-x    1 root     ping          4096 Jan 27 08:42 ..
    drwxrwsrwx    2 gradle   ping          4096 Jan 27 08:42 .gradle
    -rw-r--r--    1 root     ping             6 Feb 17 07:03 out
     ---> Removed intermediate container 7af67fd34245
     ---> e5099e079c8b
    Step 5/8 : WORKDIR .gradle
     ---> Running in 021a47532a8b
     ---> Removed intermediate container 021a47532a8b
     ---> 00d4b7be91d9
    Step 6/8 : RUN ls -la .
     ---> Running in e40f3c04e52b
    total 8
    drwxrwsrwx    2 gradle   ping          4096 Jan 27 08:42 .
    drwxrwsrwx    1 gradle   ping          4096 Feb 17 07:03 ..
     ---> Removed intermediate container e40f3c04e52b
     ---> 7623abe1f032
    Step 7/8 : RUN echo world > out; ls -la .;
     ---> Running in 82ad883884a6
    total 12
    drwxrwsrwx    2 gradle   ping          4096 Feb 17 07:03 .
    drwxrwsrwx    1 gradle   ping          4096 Feb 17 07:03 ..
    -rw-r--r--    1 root     ping             6 Feb 17 07:03 out
     ---> Removed intermediate container 82ad883884a6
     ---> a01a29be26cb
    Step 8/8 : RUN ls -la .
     ---> Running in 04675a6228fc
    total 8
    drwxrwsrwx    2 gradle   ping          4096 Jan 27 08:42 .
    drwxrwsrwx    1 gradle   ping          4096 Feb 17 07:03 ..
     ---> Removed intermediate container 04675a6228fc
     ---> 72a7145007f5
    

    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.


  2. 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

    FROM gradle:8.5-jdk21-alpine
    
    COPY gradle.properties .gradle/gradle.properties
    # These commands are just to validate that the file is there.
    RUN ls .gradle
    RUN cat .gradle/gradle.properties
    

    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:

    • just use a project-level gradle.properties file;
    • using a volume mount to share a gradle.properties file from the host with a running container; or
    • pass in these settings as environment variables.

    For the purpose of illustration I created a gradle.properties with the following content:

    org.gradle.daemon=true
    org.gradle.parallel=true
    org.gradle.configureondemand=true
    

    enter image description here

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search