Back to Garden
Guides|June 6, 2025

Dockerizing a Spring Boot Application: A Step-by-Step Guide

#Java#SpringBoot#Docker

Docker is a popular tool for packaging and deploying applications in containers, which provide a lightweight and portable runtime…

Docker is a popular tool for packaging and deploying applications in containers, which provide a lightweight and portable runtime environment. In this tutorial, I will show you how to dockerize a Spring Boot application, allowing you to easily build, deploy, and run your application as a Docker container. I will cover the basics of Docker and Spring Boot, and then walk through the steps of building and deploying a simple Spring Boot application as a Docker container. Let’s get started!

To dockerize a Spring Boot application, you will need to have Docker installed on your system. If you don’t already have Docker installed, you can follow the instructions on the Docker website to download and install Docker for your operating system. Once Docker is installed, you can verify that it is working by running the following command:

docker --version

This command should print the version of Docker that is installed on your system. If you see an error message instead, make sure that Docker is properly installed and configured on your system.

Once you have Docker installed and running, the next step is to create a Dockerfile for your Spring Boot application. A Dockerfile is a text file that contains a set of instructions for building a Docker image. The Docker image will contain your Spring Boot application and all of its dependencies, allowing you to run it as a Docker container.

To create a Dockerfile for your Spring Boot application, create a new text file in the root directory of your project and name it Dockerfile. Then, add the following lines to the file:

FROM maven:3-openjdk-17 AS builder  
COPY pom.xml .  
RUN mvn de.qaware.maven:go-offline-maven-plugin:resolve-dependencies  
WORKDIR /app  
COPY . .  
RUN mvn clean package  
FROM openjdk:17-slim  
COPY --from=builder /app/target/app.jar ./app.jar  

ENTRYPOINT ["java","-jar", "/app.jar"]

This Dockerfile uses a multi-stage build to create a minimal production-ready image. The first stage uses the maven:3-openjdk-17 image to build the Maven project and package it into an app.jar file. The second stage uses the openjdk:17-slim image to copy the app.jar file and specify the command to run it. This allows for a smaller final image that only includes the necessary runtime components.

Once you have created the Dockerfile for your Spring Boot application, you can build a Docker image for your application by running the following command:

docker build -t my-app .

This command will build a Docker image for your Spring Boot application and tag it with the name my-app. You can replace my-app with any name you like, but it’s best to use a descriptive name that reflects the purpose of your application.

Once you have built the Docker image for your Spring Boot application, you can run it as a Docker container by using the docker run command. For example, to run the my-app image as a Docker container, you can use the following command:

docker run -p 8080:8080 my-app

This command will run the my-app image as a Docker container, binding the container’s 8080 port to the host’s 8080 port. This means that you will be able to access your Spring Boot application on the host’s 8080 port by using a web browser or other HTTP client.

In this tutorial, I have shown you the basics of dockerizing a Spring Boot application. I hope that you have found this tutorial helpful and that you are now ready to start using Docker and Spring Boot to build and deploy your own applications.