Deploying a Spring Boot App
Spring Boot is a popular Java framework that simplifies the development of stand-alone, production-grade Spring-based applications. It provides embedded servers, auto-configuration, and a wide ecosystem, making it ideal for building robust web services and APIs with minimal setup.
This guide explains how to deploy a Spring Boot application to Klutch.sh, both with and without a Dockerfile. It also covers installation and provides sample code to get started.
Prerequisites
- Java 17+ (or your target version)
- Maven or Gradle installed
- Git and GitHub account
- Klutch.sh account
Getting Started: Install Spring Boot
- Create a new Spring Boot app using Spring Initializr:
- Choose Maven or Gradle, Java version, and dependencies (e.g., Spring Web)
- Download and unzip your project
- Open the project in your IDE
- Build and run the app:
Your app should be running at http://localhost:8080.
Terminal window ./mvnw spring-boot:run# or./gradlew bootRun
Sample Code (src/main/java/com/example/demo/DemoApplication.java)
package com.example.demo;
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication@RestControllerpublic class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }
@GetMapping("/") public String home() { return "Hello from Spring Boot on Klutch.sh!"; }}
Deploying Without a Dockerfile
- Push your Spring Boot app to a GitHub repository.
- Log in to Klutch.sh.
- Create a new project and give it a name.
- Create a new app:
- Select your Spring Boot GitHub repository and branch
- Set the port to route traffic (usually 8080 for Spring Boot)
- Choose region, compute, number of instances, and add any environment variables
- Add a start command in your app settings:
Terminal window ./mvnw spring-boot:run# or./gradlew bootRun - Make sure your app listens on the port provided by Klutch.sh via the
PORT
environment variable. Inapplication.properties
:server.port=${PORT:8080} - Click “Create” to deploy. Klutch.sh will build and deploy your app automatically.
Deploying With a Dockerfile
- Add a
Dockerfile
to your project root. Example:# Use official OpenJDK imageFROM eclipse-temurin:17-jre-alpine# Set working directoryWORKDIR /app# Copy and build appCOPY . .RUN ./mvnw package -DskipTests# Expose port (match your Spring Boot app)EXPOSE 8080# Start the appCMD ["java", "-jar", "target/demo-0.0.1-SNAPSHOT.jar", "--server.port=${PORT}"] - Push your code (with Dockerfile) to GitHub.
- In Klutch.sh, follow the same steps to create a project and app, but select the Dockerfile option when prompted.
- Set the service details and environment variables as needed.
- Click “Create” to deploy. Klutch.sh will build your Docker image and deploy your app.
Note: Your Spring Boot app should always listen on the PORT
environment variable as shown above.
Resources
Deploying to Klutch.sh is simple and flexible. Choose the method that best fits your workflow and project requirements.