← Back to Guides
Profile image for Amr Essam
Published 4th April 2025

Deploy Spring Boot with PostgreSQL on Northflank

Spring Boot is a framework for building stand-alone Spring-based applications. It makes the development process easier by offering an opinionated design that helps developers quickly get started with common components in their project, while also providing the flexibility to manually change the project's defaults as requirements change and grow over time.

Although it relies on Spring as its core, Spring Boot removes a lot of the complexities associated with the configuration-heavy nature of Spring. It provides auto-configuration features that set sensible defaults and allow for quick component initialization.

Additionally, Spring Boot includes an embedded server which removes the need for deploying a separate application server (e.g., Tomcat). This results in a single standalone JAR file which can run on its own, making Spring Boot applications a perfect fit for a microservice architecture.

By deploying your Spring Boot applications on Northflank you can gain all the advantages of Kubernetes with none of the complexity of configuring and managing the infrastructure yourself. You can build and deploy from development to production, automate your workflows, and access a host of useful and powerful features to manage CI/CD, deploy databases, configure networking, and much more.

In this guide, we will explain how to deploy a Spring Boot application with PostgreSQL database on Northflank. We’ll use a combined service with a managed add-on for an easy and quick deployment. Then we’ll show how to use a Northflank pipeline with a release flow for a more customized workflow and multi-environment deployment. Finally, we’ll explain how to use a Northflank template to create a reusable deployment blueprint.

Prerequisites

Before going through the steps in this guide, you need to have already completed the following:

  1. Login to your existing Northflank account, or create a new account if you don’t have one already.
  2. Create a new project under a new or existing team in your account.
  3. Link your Git account to Northflank. This should be the Git account that includes the repository of your Spring Boot source code.

Dockerize your Spring Boot application

The first step is to Dockerize our application. If you already have a Dockerfile in your code repository you can skip this step. If you don’t have a Dockerfile you can create one with the following contents as an example:

# Base Image with OpenJDK17 and Maven
FROM maven:3.8.5-openjdk-17

# copying the required application files
COPY . .

# building the spring boot app
RUN mvn clean install

# Expose the application port
EXPOSE 8080

# running the spring boot app
CMD ["java", "-jar", "./target/Spring_Boot_JPA-0.0.1-SNAPSHOT.jar"]

In this Dockerfile we use a base image that contains Maven and JDK to build and run our application. We can also use separate base images for the build and run in a multi-stage configuration to optimize the generated image size.

Next we copy the contents of our repository into the image and execute the mvn clean install command to build our application. Finally, we run our application when a container is started from this image using a simple java -jar command.

Northflank also supports setting a specific path for the location of your Dockerfile inside the repository. This is beneficial in a monorepo scenario where multiple projects are included in the same code repository and each project has its own Dockerfile in a subdirectory.

Deploy Spring Boot application using a combined service

Combined services on Northflank provide a quick and easy way to build and deploy your applications. Instead of creating and customizing a more complex pipeline, combined services allow the configuration of a simple workflow that builds the application directly from a Git repository and deploys it to a destination environment. It also offers the capability to enable CI/CD where each commit to the source repository automatically triggers a new build and deployment.

Let’s create our combined service with the following steps:

  1. From the project dashboard select create new > service. Choose combined service and provide a name for your service.
  2. In the repository section, select the Spring Boot repository and the branch from which you want to build your code.
  3. Choose the build type as Dockerfile, you can set the location of the Dockerfile if it’s not in the repository root, or change the build context if your Spring Boot application is in a subdirectory. You’ll see the contents of your Dockerfile showing under the configuration.
  4. In the networking part, you can add ports to your service to allow network access. By default, Northflank detects the ports specified in your Dockerfile from the EXPOSE command.
  5. In the resources section, choose a compute plan which specifies the amount of CPU and memory available to your application.
  6. Click on create service and Northflank will automatically build your application from the specified repository and deploy it.

You can also notice that CI/CD is enabled by default, meaning that any new push to the specified repository and branch will trigger a new build and deployment for the service.

Create a PostgreSQL database using Northflank addons

Our Spring Boot application here uses JPA for data persistence to a relational database, so we need to provision a database instance and allow our deployed application to connect to it.

Northflank offers addons that integrate common infrastructure components like databases, caches, message queues with your application. The addons are provided as managed services, meaning that the Northflank platform takes care of the heavy lifting for provisioning and setting up these components, and managing backups, restores, and upgrades, allowing you to focus on your application.

In our scenario, we’re going to create a PostgreSQL database with the following steps:

  1. Click on Create new > addon under your project. Then choose PostgreSQL and provide a name for the addon.
  2. Specify the compute plan for your database instance.
  3. Click on create addon and Northflank will start provisioning your database.

You’ll be provided with the database connection details under the overview section.

Create a secret group with the database connection details

To pass the database connection details to our Spring Boot application, we populated our application.properties file to retrieve this information from environment variables as follows:

Now we need to pass these environment variables for DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASSWORD to the Northflank service to enable our application to connect to the database. We can achieve this by the following steps:

  1. Click on Create new > secret group under your project and provide a name (postgres connection for example).
  2. Expand show addons in the linked addons section and click configure on your Postgres addon.
  3. Click HOST, PORT, DATABASE, USERNAME and PASSWORD to link them to the secret group.
  4. Add + an alias to each with the respective environment variable names:
Linked valueAlias
HOSTDB_HOST
PORTDB_PORT
DATABASEDB_NAME
USERNAMEDB_USER
PASSWORDDB_PASSWORD

Finally, create the secret group, and then navigate to your service and restart it. It will be redeployed with the new values.

Now let’s try accessing our application with the following steps:

  1. In the service overview header, get the provided DNS name for our application.
  2. Test the access to the application using this DNS name.

Deploy Spring Boot application using Northflank pipelines

Northflank pipelines allow creating more complex CI/CD workflows that support multi-environment deployments. Under each stage in a pipeline, we can create a release flow that includes different nodes where each node represents a specific action that we want to execute like deploying our application, promoting an image from one stage to another, backing up data, or running migrations.

The following diagram demonstrates the relationship between projects, pipelines, stages, release flows, and nodes.

Before creating a pipeline and a release flow, we first need to create separate build and deployment services so that we can use them in our pipeline release flow.

Let’s start by creating a build service with the following steps:

  1. Create a new service. This time we need to choose a build service.
  2. Choose the repository, branches to build, Dockerfile location, and the build context.
  3. Select the compute plan and click on create service.

Once the build service is created, we can move on to creating the deployment service with the following steps:

  1. Create a new service and choose the type as deployment service.
  2. Select the deployment source as Northflank since we’ll be building the image inside Northflank. Don’t link the build service.
  3. Choose a compute plan and click on create service.

The deployment service will also inherit the connection details from the secret group to use your Postgres addon. Follow the steps above to create them if you haven't already.

Now our build and deployment services are ready. The next step is to create a pipeline and a release flow with the following steps:

  1. Create new pipeline in your project

  2. Add the deployment service and the database addon to one of the pipeline stages. Here I’ll select the development stage

  3. Create a new release flow in the stage and add a start build node. Use the build service we created before for this node and enable wait for completion.

    The wait for completion option specifies that the next node in the release flow will not run until this node finishes execution. So here the next deploy node will not start until this build node completes successfully.

  4. Add a deploy build node that uses the previous build service as the origin.

You can now save and run your release flow, and it will execute each node in turn to build and deploy your repository.

You can configure Git or webhook triggers to automatically start the release flow, and expand your release flow to include more steps to automate your release process, for example to back up a database, run a migration job, or deploy other microservices.

Create reusable configuration with Northflank templates

Templates provide a way to group multiple resources and reuse them as a single deployable object. If you want to replicate a similar infrastructure that you already have, you don’t need to create and configure every resource again from scratch. You can include your resources and configurations in a template and create new resources from this template.

To create a template for the project we've just created you can either create a new template in your team and drag and drop the same resource nodes that we created manually in the steps above, or click project template in your Spring Boot project's header and select new template from spec. Click create template to create a template with your existing project and resource configuration.

You can now reuse this template for a consistent and repeatable deployment of your resources across different projects and regions, and modify and expand it as required.

You can version control your template by enabling GitOps to check them into a source control like Git. Northflank bidirectional GitOps means that any change in your code will be automatically synced and applied to the template on Northflank, and any change in the template will be pushed back to the linked repository.

Learn more about defining and managing infrastructure as code on Northflank.

Conclusion

By abstracting away the underlying infrastructure details, Northflank allows developers to focus more on their workloads rather than the low-level infrastructure configuration. In this article we covered the different features offered by Northflank that enable a seamless and efficient deployment process for Spring Boot applications.

We can use a combined service for less complex scenarios as it provides a simple way to quickly configure a build and deployment CI/CD workflow. We can also create managed addons to integrate common infrastructure components like databases, queues, or caches into our application deployment.

If a more complex workflow is required, like executing database backup/migration jobs or promoting image builds across different environments, we can use Northflank pipelines and release flows. Additionally, we can create Northflank templates to provide reusable and repeatable deployment workflows and resources.

Northflank allows you to deploy your code and databases within minutes. Sign up for a Northflank account and create a free project to get started.

  • Deployment of Docker containers
  • On-demand instances
  • Infrastructure as code
  • Observe & monitor with real-time metrics & logs
  • Low latency and high performance
  • Managed databases, storage, and persistent volumes
  • Bring your own cloud
  • Run GPU workloads
Share this article with your network
X