Thursday, September 27, 2018

"back" to the future Java11: How to create a alpine linux based Docker image with Gradle

This short post is focused on how to build the small docker image based on alpine linux. 
The created image will contain the OpenJDK and Gradle build tool. 
Although you can't download directly the binaries for Alpine from the main page (jdk.java.net/11). You can find the link to download them by using for example google.

To download OpenJdk 11+28 for alpine click here. The binary file uses musl-libc instead of gclib with others.

Let's create a Dockerfile in the older where you have downloaded your OpenJDK binaries.
$ vi Dockerfile 

add following context


FROM alpine:3.8

# source: https://download.java.net/java/early_access/alpine/28/binaries/openjdk-11+28_linux-x64-musl_bin.tar.gz
# variables
ENV JDK_FILE openjdk-11+28_linux-x64-musl_bin.tar.gz
ENV JAVA_HOME /opt/java
ENV GRADLE_VERSION 4.10
ENV GRADLE_HOME /usr/local/gradle-$GRADLE_VERSION
ENV PATH=$GRADLE_HOME/bin:$JAVA_HOME/bin:$PATH

COPY ${JDK_FILE} /tmp
RUN mkdir /opt; cd /opt; \
    tar zxf /tmp/${JDK_FILE} \
    && ln -s jdk-11 java \
    && rm -f /tmp/${JDK_FILE}

RUN java -version

# Donwload and Install Gradle
RUN apk upgrade --update \
&& apk add curl

RUN \
    cd /usr/local && \
    curl -L https://services.gradle.org/distributions/gradle-${GRADLE_VERSION}-bin.zip -o gradle-${GRADLE_VERSION}-bin.zip && \
    unzip gradle-${GRADLE_VERSION}-bin.zip && \
    rm gradle-${GRADLE_VERSION}-bin.zip

now we can create and the docker image which does contain Gradle 4.10 and OpenJDK 11+28. For such purposes we do use command:

docker build -f ./Dockerfile -t openjdk11_g4_10 .


after while we can check newly created image by using 
$ docker ps
command

REPOSITORY                                          TAG                 IMAGE ID            CREATED             SIZE
openjdk11_g4_10                                     latest              3e04307e2976        6 hours ago         592MB
to attach sh shell to the container:
docker run -t -i openjdk11_g4_10 /bin/sh 

and we can run the java -version and gradle -version commands.
output:

/ # java -version
openjdk version "11" 2018-09-25
OpenJDK Runtime Environment 18.9 (build 11+28)
OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)

and 

# gradle -version 
                                                                    
Welcome to Gradle 4.10!

Summary:
The docker container may help you with testing your java application in isolation. You can easily check expected behaviour.  
Happy Coding!

No comments: