In this detailed guide, we’ll explore how to package a Spring Boot application into a Docker container and run it locally. Each step will be explained thoroughly to ensure clarity and understanding.
Prerequisites
- Java Development Kit (JDK) installed
- Docker installed
- Maven installed
- Basic understanding of Spring Boot
Step-by-Step Guide
Step 1: Create a Spring Boot Application
First, let’s create a simple Spring Boot application. You can use Spring Initializr to generate a basic project structure.
Example pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.2</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
artifactId
: This is the name of our project. It will be used to name the JAR file.version
: This indicates the version of our application.0.0.1-SNAPSHOT
is a common version format indicating a development version.
Step 2: Create a Dockerfile
Create a Dockerfile
in the root directory of your Spring Boot project. This file defines how our Docker image will be built.
# Dockerfile
FROM openjdk:17-jdk-slim
VOLUME /tmp
COPY target/demo-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
FROM openjdk:17-jdk-slim
: This specifies the base image. We are using a slim version of OpenJDK 17 to keep the image lightweight.VOLUME /tmp
: This creates a temporary volume to store files during the application runtime.COPY target/demo-0.0.1-SNAPSHOT.jar app.jar
: This copies the JAR file generated by Maven into the Docker image. The namedemo-0.0.1-SNAPSHOT.jar
comes from theartifactId
andversion
specified in thepom.xml
.ENTRYPOINT ["java","-jar","/app.jar"]
: This defines the command to run the application when the container starts.
Step 3: Build the Spring Boot Application
Use Maven to compile and package your Spring Boot application. This will generate a JAR file in the target
directory.
mvn clean package
mvn clean package
: This command does two things:clean
: Removes thetarget
directory to ensure a fresh build.package
: Compiles the code and packages it into a JAR file.
Step 4: Build the Docker Image
Now, build the Docker image using the Dockerfile you created.
docker build -t spring-boot-docker-demo .
docker build
: This command builds the Docker image.-t spring-boot-docker-demo
: Tags the image with the namespring-boot-docker-demo
..
: Specifies the current directory as the build context.
Step 5: Run the Docker Container
Run the Docker container using the image you just built.
docker run -p 8080:8080 spring-boot-docker-demo
docker run
: Starts a new container from the specified image.-p 8080:8080
: Maps port 8080 of the Docker container to port 8080 on your local machine, allowing you to access the application viahttp://localhost:8080
.
Step 6: Test the Application
Open a web browser and navigate to http://localhost:8080
. You should see your Spring Boot application running.
Conclusion
You’ve successfully packaged a Spring Boot application in a Docker container and run it locally. Docker simplifies the deployment process by ensuring that your application runs consistently across different environments.