Bobcares

How to Deploy a Spring Boot App with Docker to DigitalOcean App Platform

by | Feb 20, 2025

Learn how to deploy a Spring Boot App with Docker to the DigitalOcean app platform. Our DigitalOcean Support team is here to help you with your questions and concerns.

How to Deploy a Spring Boot App with Docker to DigitalOcean App Platform

DigitalOcean App Platform is a Platform-as-a-Service that enables developers to deploy applications directly to DigitalOcean servers without managing the underlying infrastructure.

Today, our Experts will walk you through deploying a Spring Boot application on the DigitalOcean App Platform via Docker.

Step-by-Step Guide

1. Create a Spring Boot Application

  1. Start by initializing a new Spring Boot project using the Spring Boot CLI:

    spring init --build=maven --java-version=17 --dependencies=web,data-jpa,postgresql spring-boot-docker

    This command creates a `spring-boot-docker` directory containing a Maven-based Spring Boot project.

  2. List the available dependencies with:

    spring init –list

  3. Go to the project directory:

    cd spring-boot-docker

  4. Next, add a Java class to manage data storage using Spring Data JPA.

2. Add Model Class

  1. Go to the package directory:

    cd src/main/java/com/example/springbootdocker

  2. Then, create and open the `Message.java` file:


    touch Message.java
    nano Message.java

  3. Define a `Message` class with fields for ID and message body. This class will map to the PostgreSQL database.

3. Add Repository Layer

  1. Create and open the `MessageRepository.java` file:


    touch MessageRepository.java
    nano MessageRepository.java

  2. Define the `MessageRepository` interface, extending `JpaRepository` to enable CRUD operations on the `Message` entity.

4. Add Controller Layer

  1. Create and open the `MessageController.java` file:


    touch MessageController.java
    nano MessageController.java

  2. Define the `MessageController` class to expose RESTful endpoints for saving and retrieving messages.

5. Configure PostgreSQL Database

  1. Go to the `resources` directory:

    cd src/main/resources

  2. Open the `application.properties` file:

    nano application.properties

  3. Add the following database configuration:

    spring.datasource.url=jdbc:postgresql://localhost:5432/dbcreds
    spring.datasource.username=dbcreds
    spring.datasource.password=dbcreds
    spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
    spring.jpa.hibernate.ddl-auto=update
    spring.jpa.show-sql=true

    Replace `dbcreds` with the actual database credentials.

6. Test Spring Boot App Locally

  1. Run the application:

    mvn spring-boot:run

  2. Test the API using `curl`:


    curl -X POST http://localhost:8080/message -H 'Content-Type: application/json' -d '{"body":"Hello world"}'
    curl -X GET http://localhost:8080/message/1

7. Add a Dockerfile

  1. Create and open a `Dockerfile` in the project root:


    touch Dockerfile
    nano Dockerfile

  2. Add the following content:


    FROM eclipse-temurin:11-jdk-jammy as builder
    WORKDIR /opt/app
    COPY .mvn/ .mvn
    COPY mvnw pom.xml ./
    RUN ./mvnw dependency:go-offline
    COPY ./src ./src
    RUN ./mvnw clean install -DskipTests
    FROM eclipse-temurin:11-jre-jammy
    WORKDIR /opt/app
    EXPOSE 8080
    COPY --from=builder /opt/app/target/*.jar /opt/app/*.jar
    ENTRYPOINT ["java","-Dspring.profiles.active=prod", "-jar", "/opt/app/*.jar"]

8. Build Docker Image

Ensure Docker is running and build the image:

docker build -t spring-boot-docker .

9. Push Code to GitHub

Initialize a Git repository and push the code:


git init
git remote add origin https://github.com/your_name/spring-boot-docker
git branch -M main
git add .
git commit -m "Initial commit"
git push -u origin main

10. Deploy to DigitalOcean App Platform

  1. Log in to the DigitalOcean account.
  2. Click Create and select Apps.
  3. Link the GitHub repository and select `spring-boot-docker`.
  4. DigitalOcean detects the `Dockerfile`; click Next.
  5. Configure environment variables if needed and deploy the application.

11. Attach a PostgreSQL Database

  1. Click Create > Database and select PostgreSQL.
  2. Add the database credentials as environment variables:


    SPRING_DATASOURCE_URL=jdbc:postgresql://:/?sslmode=require
    SPRING_DATASOURCE_USERNAME=
    SPRING_DATASOURCE_PASSWORD=

  3. Save changes to trigger a redeployment.

12. Test the Live Application

Retrieve the live app URL from DigitalOcean and test the API:


curl -X POST https://your-app-url/message -H 'Content-Type: application/json' -d '{"body":"Hello world"}'
curl -X GET https://your-app-url/message/1

The Spring Boot application is now successfully deployed on the DigitalOcean App Platform!

[Need assistance with a different issue? Our team is available 24/7.]

Conclusion

With the above best practices and troubleshooting methods, developers can easily resolve the “Failed: error occurred while running build command” issue in Cloudflare Pages.

In brief, our Support Experts demonstrated how to deploy a Spring Boot App with Docker to the DigitalOcean app platform.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF