Deploy Your Ktor Project Anywhere!
Ktor backend is becoming more and more common since it is easy to build and lighter to run. But the main problem you may face is deploying it on your Linux server, or anywhere in general, you can do it in two ways:
- Getting everything building a jar out of the Ktor project, pushing it to the server, and running it on a JVM environment.
- Using Docker to do all the heavy lifting
We will do the Docker way here. No need to worry it is really simple and you can reuse it every time it is straightforward.
Project Setup
We will not do much on the Ktor side, we will use the Ktor Project generator to build a simple minimal project.
This is the basic structure of the Ktor Project, we will add a simple String here, in return. If you start the application it will run and return the string when we hit this endpoint http://localhost:8080/api
Build a Jar from Ktor Project
We will app a plugin in build.gradle.kt
the plugins{}
block
id("com.github.johnrengelman.shadow") version "7.1.2"
when we make the Gradle sync, will get a new task added to our list of Gradle tasks. We will run this task and this will create a jar for us.
gradlew shadowJar
this will create a jar inside builds/libs
and you can simply run this particular jar directly it has all the requirements inside so it can run anywhere. You can see the folder structure below. You can run it will the command after you navigate inside builds/libs
java -jar com.example.docker-ktor-sample-0.0.1-all.jar
This will run on our localhost on port 8080
Deploy Ktor with Docker
Docker is really simple to use and install, install Docker Desktop will really help us to visualise the images and containers. You can see the folder structure below.
You can directly copy this, and make a file with the name Dockerfile
at the base of the project. These consist of really simple setting Java 11, moving into a directory, downloading and setting up gradlew
, run the gradlw shadowJar
to build the jar, we the expose like open8080
, we make a run folder and then copy from the build/libs
to the run
folder rename to server.jar
, finally, we will run the java -jar /run/server.jar
.
Now how to run this file? you can simply do to run this to run the servers
/*
This command at the base of project where Dockerfile is present,which build from the above Dockerfile with the name ktor-docker. '.' signifies find the Dockerfile here
*/
docker build -t ktor-docker .
/*
This command will have to run the above 'ktor-docker' with the name docker-ktor1 on port 8080
*/
docker run --name docker-ktor1 -p 8080:8080 ktor-docker
The server will start at port 8080 and it will be visible on the Docker Desktop
Wow, we were able to run our Ktor app with Docker!!🎉
Since docker gives you the flexibility to run the backend irrespective of the system preferences you can now deploy to any popular Infrastructure as a Service like GCP, AWS, Azure etc.
For any doubts and suggestions, you can reach out on my Instagram, or LinkedIn. Follow me for Kotlin content and more. Happy Coding!
I will well appreciate one of these 👏