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
- 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
Copy CodeThis command creates a `spring-boot-docker` directory containing a Maven-based Spring Boot project.
- List the available dependencies with:
spring init –list
Copy Code - Go to the project directory:
cd spring-boot-docker
Copy Code - Next, add a Java class to manage data storage using Spring Data JPA.
2. Add Model Class
- Go to the package directory:
cd src/main/java/com/example/springbootdocker
Copy Code - Then, create and open the `Message.java` file:
touch Message.java nano Message.java
Copy Code - Define a `Message` class with fields for ID and message body. This class will map to the PostgreSQL database.
3. Add Repository Layer
- Create and open the `MessageRepository.java` file:
touch MessageRepository.java nano MessageRepository.java
Copy Code - Define the `MessageRepository` interface, extending `JpaRepository` to enable CRUD operations on the `Message` entity.
4. Add Controller Layer
- Create and open the `MessageController.java` file:
touch MessageController.java nano MessageController.java
Copy Code - Define the `MessageController` class to expose RESTful endpoints for saving and retrieving messages.
5. Configure PostgreSQL Database
- Go to the `resources` directory:
cd src/main/resources
Copy Code - Open the `application.properties` file:
nano application.properties
Copy Code - 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
Copy CodeReplace `
` with the actual database credentials.dbcreds
Copy Code
6. Test Spring Boot App Locally
- Run the application:
mvn spring-boot:run
Copy Code - 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
Copy Code
7. Add a Dockerfile
- Create and open a `Dockerfile` in the project root:
touch Dockerfile nano Dockerfile
Copy Code - 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"]
Copy Code
8. Build Docker Image
Ensure Docker is running and build the image:
docker build -t spring-boot-docker .
Copy Code
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
Copy Code
10. Deploy to DigitalOcean App Platform
- Log in to the DigitalOcean account.
- Click Create and select Apps.
- Link the GitHub repository and select `spring-boot-docker`.
- DigitalOcean detects the `Dockerfile`; click Next.
- Configure environment variables if needed and deploy the application.
11. Attach a PostgreSQL Database
- Click Create > Database and select PostgreSQL.
- Add the database credentials as environment variables:
SPRING_DATASOURCE_URL=jdbc:postgresql://:/?sslmode=require SPRING_DATASOURCE_USERNAME= SPRING_DATASOURCE_PASSWORD=
Copy Code - 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
Copy Code
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