If you haven’t had a chance to see Spring Boot in action, then you’re in for a treat, especially if the words simple and Java web app in the same sentence make you flinch. That was certainly my long standing reaction until I took a serious look at Boot.

For instance, a quick and dirty “hello world” Boot app is essentially more imports & annotations than actual code. Check it out:

That command will fire up an embedded web container with the request path / mapped to return the simple String “Hello to you, world”. You can define what port this application will run on via an application.properties file like so:

application.properties

server.port: 8080

Consequently, if I take my browser and point it to localhost:8080, I see the pedestrian, but oh-so-gratifying-when-you-see-it salutation.

Now that you’ve been introduced to the application I’d like to distribute as a Docker container, let me show you how to do it in 4 easy steps.

Keep in mind, however, that in order to use the gradle-docker plugin I use in this example, you’ll need to have Docker installed as the plugin shells out to the docker command.

Step 1: Apply some plugins

First and foremost, to Docker-ize your application, you’ll need to use two Gradle plugins: docker and application.

The application plugin is actually included automatically via the spring-boot plugin in my particular example, however, if you aren’t using Boot, then you’ll need to add the following two plugins to your build.gradle file:

apply plugin: 'application'
apply plugin: 'docker'

As the docker plugin is a 3rd party plugin, you’ll need to tell Gradle how to find it via a dependencies clause.

Specifying the classpath for the docker plugin

Now your Gradle script is ready to start Docker-ing. Next up, you’ll need to provide some clues so the plugin can create a valid Dockerfile.

Step 2: Provide some properties

The gradle-docker plugin doesn’t directly create a Docker container – it merely creates a Dockerfile and then shells out to the docker command to build an image. Consequently, you need to specify a few properties in your build.gradle file so that the corresponding Dockerfile builds a valid container that automatically runs your application.

You need to provide:

The class to run i.e. the class in your application that contains a main method

The target JVM version (default is Java 7)

Optionally, a group id, which feeds into the corresponding Docker tag.

Accordingly, my build.gradle defines all three properties like so:

Defining properties for the docker plugin

A few notes about these properties. Firstly, Java 8 isn’t currently available for this plugin. If you don’t specify a sourceCompatibility, you’ll get Java 7. Next, the group property isn’t required; however, it helps in Docker tagging. For example, my project’s baseName is dubbed galoshe; consequently, when the plugin creates a Docker image, it’ll tag that image with the pattern group/name. So in my case, the corresponding image is tagged aglover/galoshe.

Finally, the mainClassName shouldn’t be too surprising – it’s the hook into your application. In truth, the plugin will create a script that your resultant Docker image will invoke on startup. That script will essentially call the command:

java -classpath your_class_path your_main_class

At this point, you are almost done. Next up, you’ll need to specify any Dockerfile instructions.

Step 3: Specify any required Dockerfile instructions

Dockerfiles contain specialized instructions for the corresponding image they create. There are a few important ones; nevertheless, my Boot app only requires one: port, which is set via the exposePort method of the plugin.

Specifying port 8080

distDocker {
exposePort 8080
}

A few other aspects you can muddle with via the plugin are addFile which results in an ADD instruction, runCommand, which results in a RUN instruction, and finally setEnvironment, which creates an ENV instruction.

Now you’re done with your Gradle build. All that’s left to do is run your build and fire the image up!

Step 4: Build and run it

Provided you’ve configured the gradle-plugin properly, all that’s left to do is run your build. In this case, the command is simply distDocker.

Running my build

$ ./gradlew distDocker

The first time you run this command it’ll take a bit as various images will be downloaded. Subsequent runs will be lightning quick though.

After your build completes, your image will be created with the tag I noted earlier. In my case, the tag will be aglover/galoshe, which I can quickly see by running the images command:

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Join Us

With 1,043,221 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!