Creating Docker Container

Docker is a container platform:it allows packaging software in a format that can then be run in isolation in any supported operating system.

Publishing a Ktor application to docker is very easy and only takes a few steps:

  • Install Docker
  • A JAR packaging tool

In this page we will guide you through creating a docker image and publishing an application to it.

Table of contents:

Package an application using Gradle

In this tutorial, we will use the Gradle shadow plugin.It packages all the output classes, resources, and all the required dependencies into a single JAR file,and appends a manifest file to tell Java which is the entry-point main class containing the main method.

First, you need to add the shadow plugin dependency in your build.gradle file:

  1. buildscript {
  2. ...
  3. repositories {
  4. ...
  5. maven { url "https://plugins.gradle.org/m2/" }
  6. }
  7. dependencies {
  8. ...
  9. classpath "com.github.jengelman.gradle.plugins:shadow:2.0.1"
  10. }
  11. }

After that, you have to apply it, along with the application plugin:

  1. apply plugin: "com.github.johnrengelman.shadow"
  2. apply plugin: 'application'

Then specify the main class, so it knows what to run when running the java’s JAR inside Docker:

  1. mainClassName = 'org.sample.ApplicationKt'

The string is the fully qualified name of the class containing your main function. When main function is a top-levelfunction in a file, the class name is the file name with the Kt suffix. In the example above, main function is in thefile Application.kt in package org.sample.

Finally, you have to configure the shadow plugin:

  1. shadowJar {
  2. baseName = 'my-application'
  3. classifier = null
  4. version = null
  5. }

Now you can run ./gradlew build to build and package your application.You should get my-application.jar in build/libs folder.

For more information about configuring this plugin see documentation for the plugin

So a full build.gradle file would look like this:

build.gradle

  1. buildscript {
  2. ext.kotlin_version = '1.3.61'
  3. ext.ktor_version = '1.3.0'
  4. ext.logback_version = '1.2.3'
  5. ext.slf4j_version = '1.7.25'
  6. repositories {
  7. jcenter()
  8. maven { url "https://plugins.gradle.org/m2/" }
  9. }
  10. dependencies {
  11. classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
  12. classpath "com.github.jengelman.gradle.plugins:shadow:2.0.1"
  13. }
  14. }
  15. apply plugin: 'kotlin'
  16. apply plugin: "com.github.johnrengelman.shadow"
  17. apply plugin: 'application'
  18. mainClassName = "io.ktor.server.netty.EngineMain"
  19. sourceSets {
  20. main.kotlin.srcDirs = [ 'src' ]
  21. main.resources.srcDirs = [ 'resources' ]
  22. }
  23. repositories {
  24. jcenter()
  25. }
  26. dependencies {
  27. compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
  28. compile "io.ktor:ktor-server-netty:$ktor_version"
  29. compile "io.ktor:ktor-html-builder:$ktor_version"
  30. compile "ch.qos.logback:logback-classic:$logback_version"
  31. }
  32. kotlin.experimental.coroutines = 'enable'
  33. shadowJar {
  34. baseName = 'my-application'
  35. classifier = null
  36. version = null
  37. }

resources/application.conf

  1. ktor {
  2. deployment {
  3. port = 8080
  4. }
  5. application {
  6. modules = [ io.ktor.samples.hello.HelloApplicationKt.main ]
  7. }
  8. }

src/HelloApplication.kt

  1. package io.ktor.samples.hello
  2. import io.ktor.application.*
  3. import io.ktor.features.*
  4. import io.ktor.html.*
  5. import io.ktor.routing.*
  6. import kotlinx.html.*
  7. fun Application.main() {
  8. install(DefaultHeaders)
  9. install(CallLogging)
  10. routing {
  11. get("/") {
  12. call.respondHtml {
  13. head {
  14. title { +"Ktor: jetty" }
  15. }
  16. body {
  17. p {
  18. +"Hello from Ktor Jetty engine sample application"
  19. }
  20. }
  21. }
  22. }
  23. }
  24. }

You can check this full example at the ktor-samples repository.

Prepare Docker image

In the root folder of your project create a file named Dockerfile with the following contents:

Dockerfile

  1. FROM openjdk:8-jre-alpine
  2. ENV APPLICATION_USER ktor
  3. RUN adduser -D -g '' $APPLICATION_USER
  4. RUN mkdir /app
  5. RUN chown -R $APPLICATION_USER /app
  6. USER $APPLICATION_USER
  7. COPY ./build/libs/my-application.jar /app/my-application.jar
  8. WORKDIR /app
  9. CMD ["java", "-server", "-XX:+UnlockExperimentalVMOptions", "-XX:+UseCGroupMemoryLimitForHeap", "-XX:InitialRAMFraction=2", "-XX:MinRAMFraction=2", "-XX:MaxRAMFraction=2", "-XX:+UseG1GC", "-XX:MaxGCPauseMillis=100", "-XX:+UseStringDeduplication", "-jar", "my-application.jar"]

Let’s see what is what:

  1. FROM openjdk:8-jre-alpine

This line tells Docker to base an image on a pre-built image with Alpine Linux. You can use other images from OpenJDK registry. Alpine Linux benefit is that the image is pretty small. We also select JRE-only image since we don’t need to compile code on the image, only run precompiled classes.

  1. RUN mkdir /app
  2. COPY ./build/libs/my-application.jar /app/my-application.jar
  3. WORKDIR /app

These lines copy your packaged application into the Docker image and sets the working directory to where we copied it.

  1. CMD ["java", "-server", "-XX:+UnlockExperimentalVMOptions", "-XX:+UseCGroupMemoryLimitForHeap", "-XX:InitialRAMFraction=2", "-XX:MinRAMFraction=2", "-XX:MaxRAMFraction=2", "-XX:+UseG1GC", "-XX:MaxGCPauseMillis=100", "-XX:+UseStringDeduplication", "-jar", "my-application.jar"]

The last line instructs Docker to run java with G10s GC, 4G of memory and your packaged application.

Building and running the Docker image

Build an application package:

  1. ./gradlew build

Build and tag an image:

  1. docker build -t my-application .

Start an image:

  1. docker run -m512M --cpus 2 -it -p 8080:8080 --rm my-application

With this command, we start Docker in a foreground mode. It will wait for the server to exit, itwill also respond to Ctrl+C to stop it. -it instructs Docker to allocate a terminal (tty) to pipe the stdoutand to respond to the interrupt key sequence.

Since our server is running in an isolated container now, we should tell Docker to expose a port so we canactually access the server port. Parameter -p 8080:8080 tells Docker to publish port 8080 from inside a container as a port 8080 on a localmachine. Thus, when you tell your browser to visit localhost:8080 it will first reach out to Docker, and it will bridgeit into internal port 8080 for your application.

You can adjust memory with -m512M and number of exposed cpus with —cpus 2.

By default a container’s file system persists even after the container exits, so we supply —rm option to preventgarbage piling up.

For more information about running a docker image please consult docker run documentation.

Pushing docker image

Once your application is running locally successfully, it might be a time to deploy it:

  1. docker tag my-application hub.example.com/docker/registry/tag
  2. docker push hub.example.com/docker/registry/tag

These commands will tag your application for a registry and push an image. Of course, you need to replace hub.example.com/docker/registry/tag with an actual URL for your registry.

We won’t go into details here since your configuration might require authentication, specific configuration options and even special tools. Please consult your organization or cloud platform, or check docker push documentation.

Sample

You can check a full sample at the ktor-samples repository.