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.