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
unzip
should be installed, since it is required forsdkman
installer. In newbash
console 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
sdkman
inmsys2
environment. The reason is explained in corresponding GitHub Issue
- To fix it open file
~/.sdkman/bin/sdkman-init.sh
and 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
sdkman
is ready, open newbash
console and install latestGradle
version.$ sdk install gradle
- Check that
Gradle
was 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
Gradle
task.$ gradle init --type groovy-library
- After generation
Library.groovy
andLibraryTest.groovy
could 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 main
could be omitted inGroovy
Although
gradle init
generates 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 localGradle
installation.Enable Docker support for project build script
When using
Gradle
you don’t need to deal withDockerfile
and other things for creatingDocker
image. There’re plugins for this :))To enable and customize them just add some lines to
build.gradle
as 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
Docker
imageCustomize 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
application
plugin(2) Fixing docker machine URL for Java API
(3)
Docker
image tag nameBuild image and run Docker container
Must run
start.sh
script fromDocker
for windows installation before proceed to further steps. Please refer to previous post for details.- Execute command to create
Docker
image.$ 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 run
and inspect the output to match expected fromHello.groovy
class.$ 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