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
.$ curl -s get.sdkman.io | bash
-
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.groovypackage ua.eshepelyuk.blog println "Hello from Docker by Groovy and Gradle" (1)
1 declaration of public static void main
could be omitted inGroovy
Although |
Enable Docker support for project build script
When using Gradle
you don’t need to deal with Dockerfile
and other things for creating Docker
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
image2 Docker support plugin - Customize 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
plugin2 Fixing docker machine URL for Java API 3 Docker
image tag name
Build image and run Docker container
Must run |
-
Execute command to create
Docker
image.$ gradle dockerBuildImage
-
Check new image is available by running
docker images
. 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 |