1 minute read

Case study - Wrap kotlin app in Docker container

What is needed to do to dockerise the app?

We need to find out how to perform some steps without usage of IDE - don’t worry, with Gradle its piece of cake.

To make things easier, navigate to

` cd application-directory`

Building

and

## Powershell
./gradlew assemble
## Bash
gradle assembly

In case of success We should see screen which looks like Figure 1.

Figure 1. Expected output of command

Running

and

## Powershell
./gradlew run
## Bash
gradle run

In case of success We should see screen which looks like Figure 2.

Figure 2. Expected output of command

Gradle properties

As we’d try to host our app in free environment let’s reduce resources usage

gradle.properties

org.gradle.jvmargs=-Xmx512m -XX:MaxPermSize=256m
org.gradle.caching=true

Dockerizin’

Lets prepare dockerfile

dockerfile

# Let's use JDK11 with gradle image - from https://hub.docker.com/_/gradle
FROM gradle:6.8.3-jdk11 AS build

# Set current working directory to App
WORKDIR /app

# Change owner of file to avoid errors and copy 
COPY --chown=gradle:gradle . /app


# Let's build app for production
RUN gradle installDist

# Let's have another build stage to optimize run
FROM gradle:6.8.3-jdk11

# `train-bsc` is name of kt application 
COPY --from=build /app/build/install/train-bsc /app/

# Set current working directory to App
WORKDIR /app/bin

# Application network port there
EXPOSE 8080

# Let's start app
CMD ["./train-bsc"]

Beware I did a try with gradle build --cache-local following with gradle run - locally it went fine, but during Heroku warmup memory limit was exceeded

To create image from local filesystem we need to build it using:

` docker build . -t my-app`

Output should looks like Figure 3.

Figure 3. Expected output of command

Try to run locally

                  # switch -p (publish) create a firewall rule which maps port inside container to host
docker run my-app -p 8080:8080
Figure 4. Successful application run

Here we can deploy to heroku