Installing and using Gradle in a docker image/container

20,840

Solution 1

I solved the problem using the ENV docker instructions (link to the documentation).

ENV GRADLE_HOME=/app/gradle-2.4
ENV PATH=$PATH:$GRADLE_HOME/bin

Solution 2

This command /bin/bash -c "source $HOME/.bashrc" means that you create a new non-interactive process and run a command in it to set environment variables there. Which does not affect the parent process. As soon as variables are set, process exits. You can check this by running something like this:

RUN /bin/bash -c "source $HOME/.bashrc; env"
RUN env

What should be working is this option:

RUN source ~/.bashrc

And the reason why it works when you log in, is because the new process reads already updated ~/.bashrc.

Solution 3

You can use multi-stage builds and the Gradle Docker image (no need to install Gradle...) to build the application then use the result in the runtime container:

# Build
FROM gradle AS build

WORKDIR /appbuild
COPY . /appbuild

RUN gradle --version
# here goes your build code

Once the Gradle build is done, switch to the runtime container:

# Runtime
FROM openjdk:8-jre-alpine

# more stuff here...

COPY --from=0 appbuild/<somepath>/some.jar application.jar

# more stuff here...

The COPY command copies the build artifacts from the build phase to the runtime container (in this case a jar file).

Solution 4

I was trying to install same version with JDK 11.0.7 but gradle-2.4 does not work. and got below error


FAILURE: Build failed with an exception.

* What went wrong:
Could not determine java version from '11.0.7'.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

I install later version to fix the above issue after installation.
Posting as an answer might help someone else.

FROM openjdk:11.0.7-jdk
RUN apt-get update && apt-get install -y unzip
WORKDIR /gradle
RUN curl -L https://services.gradle.org/distributions/gradle-6.5.1-bin.zip -o gradle-6.5.1-bin.zip
RUN unzip gradle-6.5.1-bin.zip
ENV GRADLE_HOME=/gradle/gradle-6.5.1
ENV PATH=$PATH:$GRADLE_HOME/bin
RUN gradle --version
Share:
20,840
TPPZ
Author by

TPPZ

Updated on July 09, 2022

Comments

  • TPPZ
    TPPZ almost 2 years

    I am getting this strange error at the end of the process of creating a docker image from a Dockerfile:

    /bin/sh: 1: gradle: not found
    INFO[0003] The command [/bin/sh -c gradle test jar] returned a non-zero code: 127
    

    The relevant part of the Dockerfile:

    FROM debian:jessie
    [...]
    RUN curl -L https://services.gradle.org/distributions/gradle-2.4-bin.zip -o gradle-2.4-bin.zip
    RUN apt-get install -y unzip
    RUN unzip gradle-2.4-bin.zip
    RUN echo 'export GRADLE_HOME=/app/gradle-2.4' >> $HOME/.bashrc
    RUN echo 'export PATH=$PATH:$GRADLE_HOME/bin' >> $HOME/.bashrc
    RUN /bin/bash -c "source $HOME/.bashrc"
    RUN gradle test jar
    [...]
    

    The command I am using is: docker build -t java_i .

    The strange thing is that if:

    • I run a container from the previous image commenting out RUN gradle test jar (command: docker run -d -p 9093:8080 -p 9094:8081 --name java_c -i -t java_i),
    • then I log into that container (command: docker exec -it java_c bash),
    • then I manually check the gradle environment variables finding them,
    • then I manually run that commented out command from within the running container (gradle test jar):

    I eventually get the expected output (the compiled java code in the build folder).

    I am using Docker version 1.6.2

  • TPPZ
    TPPZ almost 9 years
    Thank you for the hint! But I was already trying that kind of thing (I forgot to mention sorry) and the problem I think is that source is not a normal command but a shell builtin, the error I get: /bin/sh: 1: source: not found INFO[0002] The command [/bin/sh -c source ~/.bashrc] returned a non-zero code: 127
  • Debaprasad
    Debaprasad almost 8 years
    Can you post the dockerfile after changes?
  • TPPZ
    TPPZ over 7 years
    Just replace RUN echo 'export GRADLE_HOME=/app/gradle-2.4' >> $HOME/.bashrc RUN echo 'export PATH=$PATH:$GRADLE_HOME/bin' >> $HOME/.bashrc RUN /bin/bash -c "source $HOME/.bashrc" with ENV GRADLE_HOME=/app/gradle-2.4 ENV PATH=$PATH:$GRADLE_HOME/bin
  • Jordan Gee
    Jordan Gee over 3 years
    Yes indeed, the answer helps people to install a newer version than gradle-2.4 when using Java 11. 👍