DEV Community

Clément POISSON for ScaleDynamics

Posted on • Updated on • Originally published at docs.scaledynamics.com

Deploy a Spring Boot Kotlin application with Docker

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

Introduction

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 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 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 on your system to run the application locally.
  • Node.js and yarn / npm installed. You can go here to download the latest version of Node.js and npm.
  • Git installed.

Setup the project

The project we will clone is the Spring Petclinic sample appication in Kotlin. You can find the repository here.

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

git clone https://github.com/spring-petclinic/spring-petclinic-kotlin.git
Enter fullscreen mode Exit fullscreen mode

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:

# the `--tag` option is used to name the image.
docker build . --tag spring-petclinic-kotlin
Enter fullscreen mode Exit fullscreen mode

I got some error on a Mac M1 chip:

Error docker build

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

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"]
Enter fullscreen mode Exit fullscreen mode

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:

docker run -p 8080:8080 spring-petclinic-kotlin
Enter fullscreen mode Exit fullscreen mode

The application is now running on localhost:8080, you can now access it in your browser. It should look like:

Petclinic app

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 (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. 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.

Environment creation

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:

npx warp login
Enter fullscreen mode Exit fullscreen mode

Deploy

Finally, we can run the deployment command:

npx warp deploy
Enter fullscreen mode Exit fullscreen mode

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

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.

Enjoy !

Top comments (1)

Collapse
 
generalman025 profile image
generalman025

Thanks for a great tutorial !!!

Anyway, I've found some issues during building a docker image because a project version in Github repository was changed to 2.7.0, therefore I fixed a Dockerfile as below.

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.7.0.jar /app/
RUN bash -c 'touch /app/spring-petclinic-kotlin-2.7.0.jar'
ENTRYPOINT ["java", "-XX:+UnlockExperimentalVMOptions", "-XX:+UseCGroupMemoryLimitForHeap", "-Djava.security.egd=file:/dev/./urandom","-jar","/app/spring-petclinic-kotlin-2.7.0.jar"]
Enter fullscreen mode Exit fullscreen mode