Build Ktor Project with Docker and Add Nginx for Load Balancing & Reverse Proxy

Debanshu Datta
Backyard Programmers
6 min readMay 18, 2022
Photo by Yulia Matvienko on Unsplash

Kotlin is wide having a wide scale adoption both for Android and Backend. I primarily used Kotlin with SpringBoot but I feel for a simple backend and with a minimum requirement you should give Ktor a try it is worth it. Being a mobile developer I think it is a great way to enter backend development.

In this blog, we will try to deploy our Ktor app using Docker and later we will use Nginx as a load balancer. If you don’t know much about Docker or Ngnix you can get a good idea here.

The final project is here, you can check it out here:

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 visualize 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!!🎉

Adding Nginx for Load Balancing and Reverse Proxy With Docker

Nginx, pronounced like “engine-ex”, is an open-source web server that, since its initial success as a web server, is now also used as a reverse proxy, HTTP cache, and load balancer.

We will make a folder nginx outside src. You can see the folder structure below.

Create a file with the namenginx.conf and write it with the below code. In the server{} block, we are porting it to 9090 and we are basically using location{} block, we are routing it to all the list of servers with the Round Robin Algorithm we are assuming having two instances of our Ktor server running at port 8181 and 8282 . Hence working as load balancing and also hiding the actual server location since the user will hit the link with 9090 .

Let's add Docker to help us set up our Nginx server, we will create a new Dockerfile inside the nginx folder.

use these commands to run, as explained above in the Ktor section to build and run inside ./nginx . After running this you will find a new server running in your Docker Desktop on port 9090

docker build -t nginx-docker .docker run --name docker-nginx1 -p 9090:9090 nginx-docker

Yes, we were able to run our Nginx server with Docker, ie if you hi port `9090` you will get data from `8181` and `8282`🎉

Combine Server and Nginx together to docker-compose.yml

You can think of this as a script to run our servers in a proper way. Here we will try to run two servers running on ports 8181 & 8282 , Nginx running 9090 which will help in reverse proxy and load balancing.

We will have to add a file docker-compose.ymal at the base of the project. You can see the folder structure below.

We have added this docker-compose.yml , this file is easy to understand we are starting nginx-server:9090 which depends on 2 instances of our Ktor server service1 & service2 which is mapped to port 8181 & 8282 .

make a small change in nginx.conf we have to specify the service1 & service2 it is a small change.

We can finally get all of these syncs and start our servers together with two commands

docker-compose builddocker-compose up

Finally, we are able to start our servers and all synced if you open your Docker Desktop and your Browser you will see this.🎊

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 👏

Recent Post

--

--

Debanshu Datta
Backyard Programmers

Android(L2) @Gojek | Mobile Developer | Backend Developer (Java/Kotlin)