How to Deploy Spring Boot Apps on Fly.io

java dev.to

Fly.io is an incredible platform for deploying applications close to your users globally. However, if you are a Java developer looking to launch a Spring Boot application, you might hit a brief moment of hesitation.

As of the time of writing, Fly.io does not offer native, out-of-the-box support for Spring in the same way it does for the options listed on their supported languages and frameworks page.

Don't worry—getting your Spring Boot app running on Fly.io is still incredibly straightforward. Here is how to do it.


The Standard Route: Using a Dockerfile

Even though Spring isn't formally supported with an automatic builder, Fly.io handles standard Docker containers seamlessly. If you already have a Dockerfile for your project, you are basically good to go. You can follow Fly.io's official Dockerfile deployment instructions, and your app will be up and running in no time.

But what if you don't have a Dockerfile and don't particularly want to write or maintain one?


The Easier Route: Cloud Native Buildpacks

When working with modern Spring Boot, there is a highly convenient way to create optimized Docker images without ever touching a Dockerfile. You can accomplish this using Cloud Native Buildpacks, which are integrated directly into both Maven and Gradle.

With a single command like ./mvnw spring-boot:build-image or ./gradlew bootBuildImage, Spring Boot will package your application into a production-ready Docker image.

Taking this route is, in my opinion, the absolute easiest way to get your app onto Fly.io. Here is the step-by-step process.


4 Steps to Deploy Spring Boot via Buildpacks

1. Initialize your Fly.io App

First, you need to tell Fly.io about your project. Open your terminal at the root of your Spring Boot project and run the launch command. Follow the interactive prompts to set up your app name and region.

fly launch
Enter fullscreen mode Exit fullscreen mode

This command provisions your app on Fly.io and generates a fly.toml configuration file in your project directory. Note: Tell the CLI not to deploy just yet when it asks, as we still need to build our image.

2. Build and Tag the Docker Image

Next, we will use Spring Boot's buildpack integration to create our image and immediately tag it for the Fly.io Docker registry. Make sure your Docker daemon is running locally before executing this.

If you are using Gradle:

./gradlew bootBuildImage --imageName=registry.fly.io/{YOUR_APP_NAME}:latest
Enter fullscreen mode Exit fullscreen mode

If you are using Maven:

./mvnw spring-boot:build-image -Dspring-boot.build-image.imageName=registry.fly.io/{YOUR_APP_NAME}:latest
Enter fullscreen mode Exit fullscreen mode

Replace {YOUR_APP_NAME} with the name you chose during the fly launch step.

3. Push to the Fly Registry

With the image successfully built and tagged, we need to push it to Fly.io's internal registry. First, authenticate your local Docker client with Fly, and then push the image.

flyctl auth docker
docker push registry.fly.io/{YOUR_APP_NAME}:latest
Enter fullscreen mode Exit fullscreen mode

4. Deploy the Application

Your image is now sitting in the registry, waiting to be used. The final step is to tell Fly.io to deploy a new release using that specific image.

flyctl deploy --image registry.fly.io/{YOUR_APP_NAME}:latest
Enter fullscreen mode Exit fullscreen mode

Conclusion

There you have it! You have successfully deployed your Spring Boot app to Fly.io. By utilizing Spring Boot's built-in buildpack support and the Fly.io private registry, you can easily bypass the lack of native framework support and completely avoid the hassle of manually maintaining a Dockerfile.

Source: dev.to

arrow_back Back to Tutorials