Dockerize a Spring Boot project
- Create a Spring Boot Project
If you don’t have a Spring Boot project yet, you can generate one using Spring Initializr:
- Go to Spring Initializr
- Choose your project metadata and dependencies
- Click “Generate” to download a ZIP file
- Extract the ZIP file and import it into your IDE
2. Add a Dockerfile
Create a Dockerfile
in the root directory of your Spring Boot project. Here’s a basic example:
- maven project -
# Use the official Maven image to build the app
FROM maven:3.8.6-jdk-11 AS build
# Set the working directory
WORKDIR /app
# Copy the pom.xml and download dependencies
COPY pom.xml .
RUN mvn dependency:go-offline
# Copy the source code
COPY src ./src
# Package the application
RUN mvn package -DskipTests
# Use the official OpenJDK image to run the app
FROM openjdk:11-jre-slim
# Set the working directory
WORKDIR /app
# Copy the jar file from the build stage
COPY --from=build /app/target/*.jar app.jar
# Expose the port that the app runs on
EXPOSE 8080
# Run the jar file
ENTRYPOINT ["java", "-jar", "app.jar"]
- Gradle project-
# Use the official Gradle image to build the app
FROM gradle:7.6-jdk17 AS build
# Set the working directory
WORKDIR /app
# Copy the build.gradle and settings.gradle files
COPY build.gradle settings.gradle ./
COPY gradle gradle
# Download dependencies
RUN gradle dependencies --no-daemon
# Copy the source code
COPY src src
# Build the application
RUN gradle build --no-daemon
# Use the official OpenJDK image to run the app
FROM openjdk:17-jre-slim
# Set the working directory
WORKDIR /app
# Copy the jar file from the build stage
COPY --from=build /app/build/libs/*.jar app.jar
# Expose the port that the app runs on
EXPOSE 8080
# Run the jar file
ENTRYPOINT ["java", "-jar", "app.jar"]
3. Build and Run the Docker Image
- Build the Docker Image:
Definition: A Docker image is a lightweight, standalone, and executable package that includes everything needed to run a piece of software: the code, runtime, libraries, and dependencies.
docker build -t my-spring-boot-app .
When you build a Docker image, the terminal output provides detailed information about the build process. This output includes steps like copying files, running commands, and any errors or warnings encountered. Here’s an example of what the terminal output might look like when you build a Docker image for a Spring Boot application:
To see all Docker images on your system:
docker images
Or, using the shorthand:
docker image ls
- Run the Docker Container:
Definition: A Docker container is a runtime instance of a Docker image. It is an isolated environment where the software defined in the image runs. Containers share the host OS kernel but operate in isolation from one another.
After building the image, you can run a container:
docker run -p 8080:8080 myspring-boot-app
This command maps port 8080 inside the container to port 8080 on your host system. So, if you access http://localhost:8080
on your host machine, it will forward the request to port 8080 inside the container where your application is running.
Summary
- Docker Image: A static blueprint containing the application and its dependencies. It’s like a snapshot of a system.
- Docker Container: A running instance of an image that provides an isolated environment for executing applications.
4. Push to Docker Hub (Optional)
If you want to share your Docker image, you can push it to Docker Hub:
- Tag the Image
docker tag my-spring-boot-app docker-hub-username/my-spring-boot-app
This tags the local image my-spring-boot-app
with the name docker-hub-username/my-spring-boot-app
, which prepares it to be pushed to your Docker Hub repository.
- Push the Image:
docker push your-docker-hub-username/my-spring-boot-app
That’s it! Your Spring Boot application with Gradle or maven is now dockerized and ready for deployment. 🚀
Thank You 🙌!