Deploy a Spring Boot Kotlin application with Docker
Tags: kotlin, docker, spring, deploy
Published: 2022-03-31 10:00:21

In this tutorial, we will see how to deploy a Spring Boot Kotlin application with Docker and the ScaleDynamics Platform.

## Introduction

[Spring Boot](https://spring.io/projects/spring-boot) is a Java-based web application framework that is designed to build simple, stand-alone, production-grade applications. It is built on top of the conventional Spring framework. In Spring Boot everything is auto-configured. It also support Kotlin, which in our case is the language used in the application we will deploy.

[Kotlin](https://kotlinlang.org/) is a statically typed programming language that is designed to be a modern, safe and efficient alternative to Java. It mainly target the JVM, but can also be compile in JavaScript or native code. Since 2019, Kotlin is the preferred language for Android app developpers.

[Docker](https://www.docker.com/) is a containerization platform that allows you to run applications in a container. It is used to build and run applications on the cloud.

## Goals

Our goal will be to clone the Spring Petclinic sample application, use the Docker app to run it locally and then deploy it on the ScaleDynamics Platform.

## Prerequisites

To follow along, you will need:

- [Docker](https://www.docker.com/get-started) on your system to run the application locally.
- Node.js and yarn / npm installed. You can go [here](https://nodejs.org/en/download/) to download the latest version of Node.js and npm.
- [Git](https://git-scm.com/) installed.

## Setup the project

The project we will clone is the Spring Petclinic sample appication in Kotlin. You can find the repository [here](https://github.com/spring-petclinic/spring-petclinic-kotlin).

Now let's get the project by running the following command:

```sh
git clone https://github.com/spring-petclinic/spring-petclinic-kotlin.git
```

The project is now ready, let's run it locally with Docker !

## Run the application locally

First of all, you need to launch the Docker app on your system.

### Build the image

Before being able to run the application, we need to build the image. The project has already a ready to use Dockerfile, so we just need to run the following command inside the project directory:

```sh
# the `--tag` option is used to name the image.
docker build . --tag spring-petclinic-kotlin
```

I got some error on a Mac M1 chip:

![Error docker build](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/an8p0izjca0o7x825s4c.png)

If you encountered the same issue, you can modify the Dockerfile like this:

```dockerfile
FROM gradle:7.4.1 AS build
COPY --chown=gradle:gradle . /home/gradle/src
WORKDIR /home/gradle/src
RUN gradle build

FROM openjdk:8-jre-slim
EXPOSE 8080
COPY --from=build /home/gradle/src/build/libs/spring-petclinic-kotlin-2.6.2.jar /app/
RUN bash -c 'touch /app/spring-petclinic-kotlin-2.6.2.jar'
ENTRYPOINT ["java", "-XX:+UnlockExperimentalVMOptions", "-XX:+UseCGroupMemoryLimitForHeap", "-Djava.security.egd=file:/dev/./urandom","-jar","/app/spring-petclinic-kotlin-2.6.2.jar"]
```

Those modifications include:

- changing the gradle image
- changing the name of the jar built by gradle

Now the image is ready to be run.

### Run the image

You can run this docker image using the docker application directly or with the command:

```sh
docker run -p 8080:8080 spring-petclinic-kotlin
```

The application is now running on [localhost:8080](http://localhost:8080), you can now access it in your browser. It should look like:

![Petclinic app](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rjsq1c2p5sul0b1im5z4.png)

Let's deploy it !

## Account creation and resource selection

To deploy this server on ScaleDynamics's cloud, you need an account and create an environment with a resource for it. The cloud resource provides virtual CPU and storage capacities used to execute the app. In this tutorial we will use a free shared resource available on ScaleDynamics’s cloud. These are the best one for testing. In this tutorial we will see later how you can upgrade to a dedicated production resource on the public cloud provider and region of your choice.

If you don't have an account, feel free to create one [here](https://console.scaledynamics.com/auth/signup/) (it's free and no credit card are required). Once your account is created, sign in.

Let's create a project and an environment on the ScaleDynamics's [console](https://scaledynamics.com/console). Select your organization, create a new project, then create a new environment. Now we need to choose what kind of service we need for our deployment. There are four types:

- managed HTTP docker
- managed Node.js server
- managed Node.js module
- static assets hosting

For our application, we use docker to run it. Let's pick the managed HTTP docker. You can learn more on the other types in the [ScaleDynamics documentation](https://docs.scaledynamics.com/).

![Environment creation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qtdflv117unipzd7tpgl.gif)

## Deploy the application

The environment has the resource needed, let's deploy on it.

### Login

Log into your account and select your organization via the prompt:

```shell
npx warp login
```

### Deploy

Finally, we can run the deployment command:

```shell
npx warp deploy
```

You can see that the Dockerfile was detected by the SDK. You can just follow the prompt, select the project you created, then the environment. The prompt will also ask you for a hostname, you can leave it blank for a random name or use the one you want. Finally, you can select a host. and then it will do it's magic.

This command will dump something like this:

![Deployment output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wac2alvo7w1ebb7jurx5.png)

You can now open your browser and go to the URL and TADA ! Your application is live !

## Go further

This was a simple example of deploying a simple application with docker on the ScaleDynamics Platform. You can pretty much go with any other language, framework or library you want to use.

If you are interested in CI/CD, there is a tutorial on how to implement it [here](https://docs.scaledynamics.com/blog/nest-github-actions).

Enjoy !