Deploying applications efficiently is critical for developers, and Docker has emerged as a standard for creating, shipping, and running applications in containers. This guide walks you through the process of containerizing a Spring Boot application, from creating a Dockerfile to running the application in a containerized environment.
By the end of this article, you’ll learn how to use Docker with Spring Boot, understand the purpose of .dockerignore
, build and run containers, and expose your application’s ports for external access.
Table of Contents
- Dockerfile for Spring Boot
- .dockerignore Usage
- Building and Running the Container
- Exposing Ports
- Running the Spring Boot App
- External Resources for Further Learning
- Final Thoughts
Dockerfile for Spring Boot
A Dockerfile is the heart of containerizing an application. It contains the instructions that Docker uses to create an image for your application. Below is a step-by-step guide to creating a Dockerfile for a Spring Boot app.
Sample Dockerfile
Here’s an example Dockerfile for a Spring Boot application:
# Use an official Java runtime as the base image
FROM openjdk:17-jdk-slim
# Set the working directory inside the container
WORKDIR /app
# Copy the application's JAR file into the container
COPY target/demo-app.jar app.jar
# Expose the application on port 8080
EXPOSE 8080
# Command to run the Spring Boot application
ENTRYPOINT ["java", "-jar", "app.jar"]
Explanation of Dockerfile Instructions
FROM
Specifies the base image. Here, we useopenjdk
, which contains the Java Development Kit (JDK) required to run our Spring Boot app.WORKDIR
Sets the working directory inside the container to/app
.COPY
Copies the Spring Boot JAR file from your local machine into the container.EXPOSE
Declares the port on which the application will run. Note that this does not publish the port but informs Docker of the intent.ENTRYPOINT
Defines the command to be executed when the container starts. Here, it tells Docker to run the Spring Boot app usingjava -jar
.
Building the Spring Boot JAR
Before building the Docker image, ensure that your Spring Boot application is compiled into a JAR file. Use Maven or Gradle to package your app:
- Maven:
./mvnw clean package
- Gradle:
./gradlew build
The output JAR file will usually be located in the target/
directory (for Maven) or build/libs/
directory (for Gradle). Update the COPY
instruction in your Dockerfile accordingly.
.dockerignore Usage
Similar to .gitignore
in Git, a .dockerignore
file prevents unnecessary files/directories from being copied into the Docker image, reducing image size and build time.
Example .dockerignore File
A typical .dockerignore
file for a Spring Boot app might contain:
target/
.git
*.log
*.class
.idea/
Key Benefits of .dockerignore
- Reduces the size of the Docker image by excluding unnecessary files (e.g., logs, version control metadata, and IDE settings).
- Minimizes security risks by preventing sensitive files (e.g.,
.env
) from being included in the image. - Improves build performance by skipping files during the image build process.
Building and Running the Container
Step 1 – Build the Docker Image
To create a Docker image from the Dockerfile, use the docker build
command:
docker build -t spring-boot-app .
Explanation:
-t spring-boot-app
assigns a name/tag (spring-boot-app
) to the image..
specifies the current directory as the build context (where the Dockerfile is located).
Step 2 – Verify the Image
To list all available Docker images on your system, run:
docker images
You should see the spring-boot-app
image listed.
Step 3 – Run the Container
Use the docker run
command to start a container based on the built image:
docker run -d --name my-spring-app -p 8080:8080 spring-boot-app
Explanation:
-d
runs the container in detached mode (in the background).--name my-spring-app
assigns a custom name to the container.-p 8080:8080
maps the container’s port 8080 to your host machine’s port 8080.
After running the command, your Spring Boot application should be accessible via http://localhost:8080
.
Exposing Ports
Why Expose Ports?
By default, the container runs in an isolated network. To allow external traffic, you need to map the container’s internal port (e.g., 8080) to a port on the host machine where the container runs.
Methods of Port Mapping
- Single Port Mapping:
docker run -p 8080:8080 spring-boot-app
Maps container port 8080 to host port 8080. - Dynamic Port Mapping:
docker run -P spring-boot-app
Randomly maps container ports to available host ports. Rundocker ps
to see the assigned ports.
Verify Port Binding
Check which ports are mapped by using the docker ps
command:
CONTAINER ID IMAGE PORTS
abcdef123456 spring-boot-app 0.0.0.0:8080->8080/tcp
Running the Spring Boot App
Once the container is running, the Spring Boot app is accessible just like any web service. To stop or remove the container, follow these steps:
Stop the Running Container
Use the docker stop
command:
docker stop my-spring-app
Start the Stopped Container
To restart a stopped container:
docker start my-spring-app
Remove the Container
After the container is stopped, remove it using:
docker rm my-spring-app
Remove the Docker Image
If you no longer need the image, remove it with:
docker rmi spring-boot-app
External Resources for Further Learning
- Docker Official Documentation
- Spring Boot Docker Guide
- Wikipedia on Containers
- Best Practices for Dockerfiles
Final Thoughts
Containerizing a Spring Boot application using Docker simplifies deployment, ensures environment consistency, and enables scalability. By following this guide, you’ve learned how to create a Dockerfile, use .dockerignore
, build and run a container, and expose ports for your app.
Docker’s portability and resource efficiency make it a perfect fit for modern application development. Start containerizing your Spring Boot projects today and enjoy the benefits of streamlined workflows and effortless deployments! Bookmark this guide and refer back anytime you need help with Docker and Spring Boot integration.