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 ( 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:
# variables
ENV JDK_FILE openjdk-11+28_linux-x64-musl_bin.tar.gz
ENV JAVA_HOME /opt/java

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

    cd /usr/local && \
    curl -L${GRADLE_VERSION} -o gradle-${GRADLE_VERSION} && \
    unzip gradle-${GRADLE_VERSION} && \
    rm gradle-${GRADLE_VERSION}

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

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.

/ # 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)


# gradle -version 
Welcome to Gradle 4.10!

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

No comments: