Deploying a Spring Boot application on Docker involves several steps, from creating a Docker image to running the containerized application. Here’s a step-by-step guide to help you through the process.
Prerequisites
- Docker Installed: Ensure Docker is installed on your system. You can download it from the Docker website.
- Spring Boot Application: A Spring Boot application ready for deployment.
- Maven or Gradle: Ensure you have either Maven or Gradle configured in your Spring Boot project.
Step 1: Create a Dockerfile
A Dockerfile
is a script that contains a series of instructions on how to build a Docker image for your application.
-
Navigate to your Spring Boot project directory.
-
Create a
Dockerfile
in the root directory of your project:1234567891011121314# Use an official Java runtime as a parent imageFROM openjdk:17-jdk-alpine# Set the working directory in the containerWORKDIR /app# Copy the project’s jar file into the container at /appCOPY target/myapp.jar /app/myapp.jar# Expose the port your Spring Boot app runs onEXPOSE 8080# Run the jar fileENTRYPOINT ["java", "-jar", "myapp.jar"]- Replace
target/myapp.jar
with the actual path to your JAR file. - If you’re using a different JDK version, replace
openjdk:17-jdk-alpine
with your desired base image.
- Replace
Step 2: Build Your Application
-
Build the application JAR using Maven or Gradle.
For Maven:
1mvn clean packageFor Gradle:
1./gradlew build -
Ensure the JAR file is generated in the
target
directory (for Maven) orbuild/libs
directory (for Gradle).
Step 3: Build the Docker Image
-
Open a terminal in your project directory where the
Dockerfile
is located. -
Build the Docker image using the following command:
1docker build -t my-spring-boot-app .-t my-spring-boot-app
tags your Docker image with the namemy-spring-boot-app
.- The
.
at the end indicates that the Dockerfile is in the current directory.
Step 4: Run the Docker Container
-
Run the Docker container with the following command:
1docker run -p 8080:8080 my-spring-boot-app-p 8080:8080
maps port 8080 on your local machine to port 8080 on the Docker container.my-spring-boot-app
is the name of the image you just built.
-
Access your application by navigating to
http://localhost:8080
in your web browser.
Step 5: Pushing to Docker Hub (Optional)
If you want to share your Docker image or deploy it to a cloud provider, you can push it to Docker Hub.
-
Log in to Docker Hub:
1docker login -
Tag your image for Docker Hub:
1docker tag my-spring-boot-app your-dockerhub-username/my-spring-boot-app -
Push the image to Docker Hub:
1docker push your-dockerhub-username/my-spring-boot-app -
Pull the image and run it on any machine with Docker:
12docker pull your-dockerhub-username/my-spring-boot-appdocker run -p 8080:8080 your-dockerhub-username/my-spring-boot-app
Step 6: Clean Up (Optional)
To stop the running container:
1 2 |
docker ps docker stop <container_id> |
To remove the container and image:
1 2 |
docker rm <container_id> docker rmi my-spring-boot-app |
Tips and Best Practices
-
Multi-Stage Builds: For smaller image sizes, you can use multi-stage builds in your Dockerfile.
123456789101112# Build stageFROM maven:3.8.5-openjdk-17-slim AS buildWORKDIR /appCOPY . .RUN mvn clean package# Run stageFROM openjdk:17-jdk-alpineWORKDIR /appCOPY --from=build /app/target/myapp.jar /app/myapp.jarEXPOSE 8080ENTRYPOINT ["java", "-jar", "myapp.jar"] -
Environment Variables: Use Docker environment variables for configuring your application dynamically.
-
Docker Compose: Use Docker Compose to manage multi-container applications (e.g., app container + database container).
-
CI/CD Pipelines: Integrate Docker build and deploy steps into your CI/CD pipeline using tools like Jenkins, GitHub Actions, or GitLab CI.
By following these steps, you can easily deploy your Spring Boot application using Docker, making it more portable and easier to manage in different environments.