Home » DevOps » JVM microservices – JVM based application as Docker container

About Evgeny Shepelyuk

Evgeny Shepelyuk

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.

  1. First of all unzip should be installed, since it is required for sdkman installer. In new bash console execute.
    $ pacman -S unzip
  2. Then install sdkman.
    $ export SDKMAN_DIR="$HOME/.sdkman" && curl -s get.sdkman.io | bash
  3. After the installation there can be issues running sdkman in msys2 environment. The reason is explained in corresponding GitHub Issue
     

    1. To fix it open file ~/.sdkman/bin/sdkman-init.sh and find the line.
      if [[ -n "${CANDIDATE_NAME}" && -h "${CANDIDATE_DIR}" ]]; then
    2. Replace it with.
      if [[ -n "${CANDIDATE_NAME}" && -d "${CANDIDATE_DIR}" ]]; then
  4. Now sdkman is ready, open new bash console and install latest Gradle version.
    $ sdk install gradle
  5. Check that Gradle was installed correctly (may require opening new bash).
    $ 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

  1. Project folder structure can be generated by running Gradle task.
    $ gradle init --type groovy-library
  2. After generation Library.groovy and LibraryTest.groovy could be removed.
  3. 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 in Groovy

    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 local Gradle installation.

    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 image

    (2) 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 plugin

    (2) Fixing docker machine URL for Java API

    (3) Docker image tag name

    Build image and run Docker container

    Must run start.sh script from Docker for windows installation before proceed to further steps. Please refer to previous post for details.

    1. Execute command to create Docker image.
      $ gradle dockerBuildImage
    2. Check new image is available by running docker image. Command output should include new image tagged with eshepelyuk/hellodockergradle (setting from build.gradle).
      $ docker images
      REPOSITORY                        TAG                 IMAGE ID            CREATED                  VIRTUAL SIZE
      ..
      eshepelyuk/hellodockergradle   latest              daa12bd8bb4f        About a minute ago   649 MB
      ..
    3. Start container using docker run and inspect the output to match expected from Hello.groovy class.
      $ docker run eshepelyuk/hellodockergradle
      Hello from Docker by Groovy and Gradle

    Full project’s code is available at My GitHub

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of