Search

Dark theme | Light theme

November 12, 2015

Ratpacked: Deploy Application As Docker Container

If we use the Ratpack Gradle plugin for our project then we automatically get the Gradle application plugin. We can use this together with the Gradle Docker application plugin to deploy our Ratpack application as a Docker container very easily.

To make it work we must apply the com.bmuschko.docker-java-application in our Gradle build file. With this plugin we can define some configuration properties in the docker.javaApplication configuration block. We can set a base image instead of the default java base image. The default image tag is a made up of the project group name, application name and version. To set the exposed port number we use the port property.

// File: build.gradle
plugins {
    // Use Docker Java application plugin
    id 'com.bmuschko.docker-java-application' version '2.6.1'

    id 'io.ratpack.ratpack-groovy' version '1.1.1'
    id 'com.github.johnrengelman.shadow' version '1.2.2'
    id 'idea'
    id 'eclipse'
}

repositories {
    jcenter()
}

dependencies {
    runtime 'org.slf4j:slf4j-simple:1.7.12'
    testCompile "org.spockframework:spock-core:1.0-groovy-2.4"
}

group 'ratpack-sample'
version '1.0'

docker {
    url = project.hasProperty('dockerUrl') ?
                project.getProperty('dockerUrl') :
                System.env.DOCKER_HOST.replace('tcp', 'https')

    // Configuration for Java application image.
    javaApplication {

        // Default value for maintainer is 
        // System.properties['user.name'].
        maintainer = "Hubert Klein Ikkink <mrhaki>"

        // Exposed port.
        port = 5050
    }
}

To create a Docker image with our application we invoke the task dockerBuildImage. This task has dependencies on other tasks to create a distribution of our Ratpack application and prepare it for Docker. Intermediate results can be found in the build/docker directory.

$ gradle dockerBuildImage          
:compileJava UP-TO-DATE
:compileGroovy UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:startScripts UP-TO-DATE
:distTar UP-TO-DATE
:dockerCopyDistResources UP-TO-DATE
:dockerDistTar
:dockerBuildImage
Building image using context '/Users/mrhaki/Projects/mrhaki.com/blog/posts/samples/ratpack/docker/build/docker'.
Using tag 'ratpack-sample/ratpack-app:1.0' for image.
Created image with ID 'aeab6bb816d1'.

BUILD SUCCESSFUL

Total time: 12.816 secs
$ docker images
REPOSITORY                          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ratpack-sample/ratpack-app          1.0                 aeab6bb816d1        9 minutes ago       658.2 MB

When the build is finished we have a Docker image ratpack-sample/ratpack-app:1.0. We can create and run a Docker container with this image, so we can access our Ratpack application. In the following example we create a container ratpack-sample and bind the container port 5050 to 80 on the Docker host.

$ docker run -d --name ratpack-sample -p 80:5050 ratpack-sample/ratpack-app:1.0
49cc42d4d1bd0b0b5bd30a66428cbfe64eee684f92c67965c98ffb425bb88d1c
$

Now we can access the application with http://localhost/ or http://{dockerHostIp}/. On my Mac OSX the dockerHostIp is 192.168.99.100, so the application is accessible at the URL http://192.168.99.100.

Written with Ratpack 1.1.1.