Building a Gradle Docker image

I like to use Gradle as building tool for my Java Open JDK projects. Unfortunately is there no official image available on Docker Hub. That is why I started to build my own Gradle image.

Dockerfile

I build my image on the official openjdk image with tag 8 and Gradle 3.3. Here is my simple Dockerfile.

FROM openjdk:8

RUN wget -q https://services.gradle.org/distributions/gradle-3.3-bin.zip \
    && unzip gradle-3.3-bin.zip -d /opt \
    && rm gradle-3.3-bin.zip

ENV GRADLE_HOME /opt/gradle-3.3
ENV PATH $PATH:/opt/gradle-3.3/bin

docker build

The image can be build easely with the following command. Note that you can change the tag name gradle also to gradle-3.3 for example.

docker build -t gradle .

docker run

I will give a short overview of some Gradle commands that can be executed in the Docker container.

gradle init

Let us start by running the gradle init command for a Java project in the container.

docker run --rm -v "$PWD":/usr/src/project -w /usr/src/project gradle gradle init --type=java-library
Starting a Gradle Daemon (subsequent builds will be faster)
:wrapper
:init

BUILD SUCCESSFUL

Total time: 3.283 secs

The command should have generated a directory structure like this.

.
|-- gradle/
|   `-- wrapper/
|       |-- gradle-wrapper.jar
|       `-- gradle-wrapper.properties
|-- src/
|   |-- main/
|   |   `-- java/
|   |       `-- Library.java
|   `-- test/
|       `-- java/
|           `-- LibraryTest.java
|-- build.gradle
|-- gradlew*
|-- gradlew.bat
`-- settings.gradle

gradle check

The command gradle check is executing the default test of the class Library.

docker run --rm -v "$PWD":/usr/src/project -w /usr/src/project gradle gradle -q check

gradle build

An other common task is gradle build.

docker run --rm -v "$PWD":/usr/src/project -w /usr/src/project gradle gradle -q build
Next Previous