JVM microservices – JVM based application as Docker container
Continuation of Windows and Docker integration guide. The goal of this post is to show quick and easy way of creating new JVM project and run it as a Docker container on Windows.
In the previous post I’ve described how to setup Docker environment on Windows powered PC and run existing Docker container. Now it’s time to create own JVM project, create Docker image and run it.
When it comes to quickly prototype something on JVM platform – Groovy is a great language choice. Furthermore, using Groovy in this guide is a perfect example of Docker powered JVM microservices polyglot nature. For a build tool my natural choice is Gradle, that will be used in this guide as well.
Setup Gradle using sdkman
Gradle can be installed and added to PATH manually, but there exists sdkman tool that drastically simplifies installation of Gradle and other utilities. So, I’d suggest to spend some minutes to setup it and use for Gradle installation.
- First of all
unzipshould be installed, since it is required forsdkmaninstaller. In newbashconsole execute.$ pacman -S unzip
- Then install
sdkman.$ export SDKMAN_DIR="$HOME/.sdkman" && curl -s get.sdkman.io | bash
- After the installation there can be issues running
sdkmaninmsys2environment. The reason is explained in corresponding GitHub Issue
- To fix it open file
~/.sdkman/bin/sdkman-init.shand find the line.if [[ -n "${CANDIDATE_NAME}" && -h "${CANDIDATE_DIR}" ]]; then - Replace it with.
if [[ -n "${CANDIDATE_NAME}" && -d "${CANDIDATE_DIR}" ]]; then
- To fix it open file
- Now
sdkmanis ready, open newbashconsole and install latestGradleversion.$ sdk install gradle
- Check that
Gradlewas installed correctly (may require opening newbash).$ gradle --version ------------------------------------------------------------ Gradle 2.9 ------------------------------------------------------------ Build time: 2015-11-17 07:02:17 UTC Build number: none Revision: b463d7980c40d44c4657dc80025275b84a29e31f Groovy: 2.4.4 Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013 JVM: 1.8.0_45 (Oracle Corporation 25.45-b02) OS: Windows 8.1 6.3 amd64
Sample Groovy application
- Project folder structure can be generated by running
Gradletask.$ gradle init --type groovy-library
- After generation
Library.groovyandLibraryTest.groovycould be removed. - Create main application class.ua.eshepelyuk.blog.Hello.groovy
package ua.eshepelyuk.blog println "Hello from Docker by Groovy and Gradle" (1)
(1) declaration of
public static void maincould be omitted inGroovyAlthough
gradle initgenerates Gradle wrapper scripts, I won’t use them in this guide. For real-life projects I’d suggest to use wrapper and don’t rely on localGradleinstallation.Enable Docker support for project build script
When using
Gradleyou don’t need to deal withDockerfileand other things for creatingDockerimage. There’re plugins for this :))To enable and customize them just add some lines to
build.gradleas described below.Enable plugins
build.gradle
plugins { id 'groovy' id 'application' (1) id 'com.bmuschko.docker-java-application' version '2.6.1' (2) }(1) Plugin for building runnable application that can be embedded into
DockerimageCustomize plugins
build.gradle
mainClassName = 'ua.eshepelyuk.blog.Hello' (1) docker { url = System.env.DOCKER_HOST.replaceAll("tcp", "https") (2) javaApplication { tag = "eshepelyuk/hellodockergradle:latest" (3) } }(1) Entry point for
applicationplugin(2) Fixing docker machine URL for Java API
(3)
Dockerimage tag nameBuild image and run Docker container
Must run
start.shscript fromDockerfor windows installation before proceed to further steps. Please refer to previous post for details.- Execute command to create
Dockerimage.$ gradle dockerBuildImage
- Check new image is available by running
docker image. Command output should include new image tagged witheshepelyuk/hellodockergradle(setting frombuild.gradle).$ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE .. eshepelyuk/hellodockergradle latest daa12bd8bb4f About a minute ago 649 MB ..
- Start container using
docker runand inspect the output to match expected fromHello.groovyclass.$ docker run eshepelyuk/hellodockergradle Hello from Docker by Groovy and Gradle
Full project’s code is available at My GitHub
Reference: JVM microservices – JVM based application as Docker container from our JCG partner Evgeny Shepelyuk at the jk’s blog blog. - Execute command to create







