Skip to main content

Dockerizing Vaadin

·1 min

The first step after creating the Vaadin-app in the last post [“Hello Vaadin”]({% post_url 2017-04-11-hello-vaadin %}) was to create a Docker image with it.

Since there is a nice Gradle-plugin for managing Docker available it was not much of work to create the image.

As a base image I have decided on the official Jetty-image and just adding the WAR-file of my application to it.

My build.gradle from the [last post]({% post_url 2017-04-11-hello-vaadin %}) grew a little to

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
      // https://github.com/bmuschko/gradle-docker-plugin
      classpath 'com.bmuschko:gradle-docker-plugin:3.0.6'
    }
}

plugins {
  id "fi.jasoft.plugin.vaadin" version "1.1.10"
}

apply plugin: 'com.bmuschko.docker-java-application'
apply plugin: 'com.bmuschko.docker-remote-api'

import com.bmuschko.gradle.docker.tasks.image.Dockerfile
import com.bmuschko.gradle.docker.tasks.image.DockerBuildImage
import com.bmuschko.gradle.docker.tasks.image.DockerPushImage

task createDockerfile(type: Dockerfile) {
  destFile = project.file('./build/Dockerfile')
  from 'jetty:9'
  maintainer 'The Dude "[email protected]'
  addFile "./libs/hello-vaadin.war", "/var/lib/jetty/webapps/"
}

task buildImage(type: DockerBuildImage) {
  inputDir = createDockerfile.destFile.parentFile
  tag = 'thedude/hello-vaadin:1.0'
}

task pushImage(type: DockerPushImage) {
  imageName = "thedude/hello-vaadin"
  tag = '1.0'
}

createDockerfile.dependsOn war
buildImage.dependsOn createDockerfile
pushImage.dependsOn buildImage

Two more steps left, building the image and running the corresponding container.

Now, building the image is simply gradle buildImage and running it with

docker run -ti -p 8888:8080 thedude/hello-vaadin:1.0

Let’s check in the browser what we get under http://localhost:8888/hello-vaadin/.

All cool? Then push the image with gradle pushImage.

Done for today!