DEV Community

Cover image for How to deploy a Ktor app on Railway
ColtonIdle
ColtonIdle

Posted on • Updated on

How to deploy a Ktor app on Railway

Deploying Ktor onto Railway isn't too popular, but I have found two different ways to do it.

Option 1

  • use https://start.ktor.io/ and just download the baseline project there
  • On railway set build command to ./gradlew buildFatJar
  • On railway set deploy command to ./gradlew runFatJar
  • On railway Set a variable of PORT to 8080

Note: In railway free tier, ./gradlew runFatJar seems to sometimes time-out so you can instead use java -jar thejarname-all.jar seems to work fine without causing issues

Option 2

  • use https://start.ktor.io/ and just download the baseline project there
  • On railway set PORT variable to 8080
  • Add a nixpacks.toml to the root of your project containing...
[start]
cmd = 'java $JAVA_OPTS -jar build/libs/*-all.jar'
Enter fullscreen mode Exit fullscreen mode

(Basically the same as setting the start command in option 1)

Option 3

Docker or Docker Compose
https://ktor.io/docs/docker.html
https://ktor.io/docs/docker-compose.html

Bonus tips:

Downtime/503's should be non-existent if you use railway's health-check feature + set the overlap time. This will make sure that your old service is still running until the new service is actually up and running.

  1. Set a healthcheck path at railway > [Your Project] > Settings > Healthcheck https://docs.railway.app/reference/healthchecks

  2. Set the RAILWAY_DEPLOYMENT_OVERLAP_SECONDS more info

  3. You can skip setting a PORT variable in Option 1, 2 and railway will randomly assign one. Then in your ktor code you can use System.getenv("PORT") to get the port from railway.

  4. Instead of using nixpacks.toml you can use railway.json since it will do schema validation for you.

{
    "$schema": "https://schema.up.railway.app/railway.schema.json",
    "build": {
        "builder": "NIXPACKS",
        "buildCommand": "./gradlew -p server clean build -x check -x test"
    },
    "deploy": {
        "startCommand": "java $JAVA_OPTS -jar server/build/libs/*-all.jar"
    }
}
Enter fullscreen mode Exit fullscreen mode

Thanks @Brody and @Aleks for all of your help!

Top comments (0)