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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Running this application is as easy as typing:
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:
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
Step 1: Apply some plugins
First and foremost, to Docker-ize your application, you’ll need to use two Gradle plugins:
The gradle-docker plugin by Transmode is actually 1 of 2 available plugins for Dockering with Gradle. The other plugin by Ben Muschko of Gradleware is a bit more advanced with additional features, however, I find the Transmode plugin the easiest and quickest to get going.
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
docker plugin is a 3rd party plugin, you’ll need to tell Gradle how to find it via a
1 2 3 4 5 6
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
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
- The target JVM version (default is Java 7)
- Optionally, a group id, which feeds into the corresponding Docker tag.
build.gradle defines all three properties like so:
1 2 3
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
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:
At this point, you are almost done. Next up, you’ll need to specify any
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.
Consequently, to ensure my Docker container exposes port 8080 as defined in my
application.properites file, I’ll add the following clause to my
1 2 3
A few other aspects you can muddle with via the plugin are
addFile which results in an
runCommand, which results in a
RUN instruction, and finally
setEnvironment, which creates an
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
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
1 2 3 4
I can subsequently run my image like so:
I can naturally go to my browser, hit localhost:8080 and find myself quite satisfied that my image runs a nifty greeting.
Of course, I would need to publish this image for others to use it; nevertheless, as you can see, the gradle-plugin allows me to quickly create Docker containers for Java apps.