Dockerizing Spring Boot REST API Gradle
Dockerize a Spring Boot REST API built with Gradle.
Gradle-based Spring Boot applications are dockerized in almost the same way as Maven projects.
The main difference is the build command and output location. Gradle usually creates the JAR inside build/libs.
Build the project
Run:
./gradlew clean buildOn Windows, you may use:
gradlew.bat clean buildAfter the build, check:
build/libs/You should see the application JAR.
Dockerfile
Create a Dockerfile:
FROM eclipse-temurin:17-jre
WORKDIR /app
COPY build/libs/*.jar app.jar
EXPOSE 8080
CMD ["java", "-jar", "app.jar"]Using *.jar is convenient for beginner projects, but in production you may prefer the exact JAR name.
Build the Docker image
docker build -t spring-gradle-api:1.0 .Run the API
docker run -d -p 8080:8080 --name gradle-api spring-gradle-api:1.0Test an endpoint:
http://localhost:8080/apiUse the actual endpoint from your application.
Debug if needed
docker logs gradle-api
docker ps -aIf the container exits immediately, the Spring Boot app probably failed during startup.
For Gradle projects, remember the output path: Maven usually uses target, while Gradle usually uses build/libs.
Written By: Shiva Srivastava
How is this guide?
Last updated on
