Deploy a Spring Boot App
This guide explains how to deploy a Spring Boot (opens in a new tab) application written in Java to Koyeb using:
- Git-driven deployment to automatically build and deploy a new version of your application each time a change is detected on your branch.
- Pre-built containers you can deploy from any public or private registry.
To successfully follow this documentation, you will need to have a Koyeb account (opens in a new tab). You can optionally install the Koyeb CLI if you prefer to follow this guide without leaving the terminal.
You can deploy and preview the Express application from this guide by clicking the Deploy to Koyeb button below.
You can consult the repository on GitHub (opens in a new tab) to find out more about the example application that this guide uses.
Create the Spring Boot app
Get started by creating a minimalistic Spring Boot application. You will need a recent version of Java installed on your local machine. Additionally, you may wish to install Docker (opens in a new tab) if you are planning on building and testing a container image for the application locally.
Download and extract a project template
When you are ready, visit this spring initializr page (opens in a new tab), which has the configuration for the demo project we'll be generating already filled out. While there are a fairly large number of options, the most important details are the following:
- Project management tool: Maven
- Language: Java
- Group:
com.koyeb
- Artifact:
example-spring-boot
- Packaging: Jar
- Dependencies: Search for and add Spring Web
After taking a look at the project details, click GENERATE and then save the provided .zip
file to your local computer.
Unzip the .zip
file to access the project files, remove the .zip
archive, and enter the new project directory. The link above generates a file called example-spring-boot.zip
, so we will run:
unzip example-spring-boot.zip
rm example-spring-boot.zip
cd example-spring-boot
The most important project files and directories are:
./mvnw
: A wrapper for the Maven command for Linux and macOS. This calls a vendored version of Maven stored in.mvn/wrapper/maven-wrapper.jar
instead of the system version../mvnw.cmd
: This is an equivalent wrapper script for Windows../pom.xml
: The project management file for defining dependencies and metadata../src/main/java/com/koyeb/examplespringboot/ExampleSpringBootApplication.java
: The main application file for the project../src/main/resources/application.properties
: Describes environment-specific configuration details for the project.
Modify the application code
The application code generated by the project template doesn't really do much currently. Let's replace it with a basic "hello world" application.
In your editor, open the ExampleSpringBootApplication.java
file in the ./src/main/java/com/koyeb/examplespringboot
directory in your editor. Replace the current contents with the following code:
package com.koyeb.examplespringboot;
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
@RestController
public class ExampleSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(ExampleSpringBootApplication.class, args);
}
@GetMapping("/")
public String hello() {
return String.format("Hello world!");
}
}
This code imports the necessary classes and interfaces to make a web application from the Spring Boot and Spring Web packages. It defines one class with annotations to mark it as a Spring Boot application and REST controller. This class has main
method to bootstrap the Spring Boot capabilities as well as a hello
method that is mapped to respond to requests to the /
endpoint with "Hello world!".
You can run the new application by typing the following from the root project directory:
./mvnw spring-boot:run
Maven will build the application and then run it according to the project configuration. Once the application is running, you can visit http://127.0.0.1:8080
in your browser or with curl
. You should get a "Hello world!" message as the response.
Press CTRL-C when you are finished to stop the application.
Modify the configuration to make the port configurable
If the previous command completed successfully, then you've validated that the application builds and runs correctly. Next, instead of always running on port 8080, let's configure it to run on the port specified by the PORT
environment variable. We will keep port 8080 as the default fallback value to avoid confusion if the PORT
variable is unset.
To begin, open the application.properties
file in the ./src/main/resources
directory in your text editor. It will likely be blank currently. Inside, set the following configuration for the project's port:
server.port=${PORT:8080}
This configures Tomcat, the application server, to check the PORT
environment variable for the port to run on.
Now, confirm that port 8080 is still being used when the PORT
environment variable is unset:
./mvnw spring-boot:run
Your application should be served on http://127.0.0.1:8080
as it was earlier.
Now, test that you can modify the port by passing the PORT
variable:
PORT=5555 ./mvnw spring-boot:run
The application should now be available on port 5555 instead of 8080, which confirms that we can dynamically configure the application port.
Create a Dockerfile for the project (Optional)
We can build and run our Spring Boot project on Koyeb using the native Java buildpack, but we can also optionally build from a Dockerfile for more control. To make it possible to build a container image for our application, we just need to create the Dockerfile. We'll also define a .dockerignore
file to tell the builder what files to skip when creating the image.
First, define a .dockerignore
file in your main project directory. Inside, paste the following contents:
.git
.gitignore
Dockerfile
.dockerignore
target
This file tells Docker to not include Git files, the Docker files themselves, and any build artifacts placed in the target
directory. This helps ensure that the image we build is not bloated and that the build completes faster.
Next, create a new file called Dockerfile
within the main project directory. Inside, paste the following contents:
# Build stage
FROM eclipse-temurin:17-jdk-alpine AS builder
WORKDIR /app
COPY . .
RUN ./mvnw package
# Run stage
FROM eclipse-temurin:17-jdk-alpine AS runner
WORKDIR /app
COPY --from=builder /app/target/*.jar app.jar
CMD ["java", "-jar", "app.jar"]
This Dockerfile uses a multistage build (opens in a new tab) to separate the build steps from the final image environment. This creates a more streamlined image and allows us to tightly control what files are included in the final image.
Both stages are based on the Alpine version of the eclipse-temurin
image (opens in a new tab). The build stage copies all of the files over to the image and builds the package. The compiled artifact is then copied to the runtime image where it is executed directly with the java
executable.
If you have Docker installed locally, you can build and test the image on your computer and optionally upload it to a registry. You can deploy container images from any container registry to Koyeb.
We can also build the Dockerfile directly from the repository when we deploy, which is useful as a way of automatically deploying when changes occur. We will demonstrate this method as one of the options in this guide.
Push the project to GitHub
In the project directory, initialize a new git repository by running the following command:
git init
Next, download a basic .gitignore
file designed for Maven projects from GitHub:
curl -L https://raw.githubusercontent.com/github/gitignore/main/Maven.gitignore -o .gitignore
Finally, specify the Java runtime version to use so that the Koyeb Java buildpack executes the project with the correct version:
echo "java.runtime.version=17" > system.properties
Next, add the project files to the staging area and commit them. If you don't have an existing GitHub repository to push the code to, you can create a new one and run the following commands to commit and push changes to your GitHub repository:
git add :/
git commit -m "Initial commit"
git remote add origin git@github.com:<YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME>.git
git push -u origin main
Make sure to replace <YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME>
with your GitHub username and repository name.
Deploy to Koyeb using git-driven deployment
Once the repository is pushed to GitHub, you can deploy the Spring Boot application to Koyeb. Any changes in the deployed branch of your codebase will automatically trigger a redeploy on Koyeb, ensuring that your application is always up-to-date.
To deploy the Spring Boot app on Koyeb using the control panel (opens in a new tab), follow the steps below:
- Click Create Web Service on the Overview tab of the Koyeb control panel.
- Select GitHub as the deployment option.
- Choose the GitHub repository and branch containing your application code. Alternatively, you can enter our public Spring Boot example repository (opens in a new tab) into the Public GitHub repository at the bottom of the page:
https://github.com/koyeb/example-spring-boot
. - Choose the Builder for your project. We can use either a Dockerfile or buildpack for this repository.
- Name the App and Service, for example
example-spring-boot
. - Click the Deploy button.
A Koyeb App and Service will be created. Your application will be built and deployed to Koyeb. Once the build has finished, you will be able to access your application running on Koyeb by clicking the URL ending with .koyeb.app
.
Deploy to Koyeb from a container registry
If you chose to build a container image for the Spring Boot application, you can optionally deploy the application from a container registry instead of from GitHub.
To deploy a pre-built Spring Boot container image on Koyeb using the control panel (opens in a new tab), follow the steps below:
- Click Create Web Service on the Overview tab of the Koyeb control panel.
- Select Docker as the deployment option.
- Choose the container image and tag from your registry and click Next to continue.
- Name the App and Service, for example
example-spring-boot
. - Click the Deploy button.
A Koyeb App and Service will be created. The container image will be pulled from the container registry and a container will be deployed from it. Once the initialization is complete, you will be able to access your application running on Koyeb by clicking the URL ending with .koyeb.app
.