Unlock Your Python Backend Career: Build 30 Projects in 30 Days. Join now for just $54

Spring Boot + Docker: Dockerizing Java Spring Boot Apps

by Ayush Shrivastava

.

Updated Fri May 09 2025

.
Spring Boot + Docker: Dockerizing Java Spring Boot Apps

Docker has changed the way we build, run, and scale applications.

It lets you package your app with everything it needs, so it runs the same everywhere, on your computer, on someone else’s, or on a server.

Docker is one of the most popular tools for this. It helps keep things consistent and avoids the “it works on my machine” problem.

In this blog, we will see how to containerize a Spring Boot application using Docker, step by step. By the end, you’ll be able to run your Spring Boot app inside a Docker container and deploy it seamlessly.

What is Docker?

  • Docker gets its name from the term "dock worker," someone who loads and unloads shipping containers. Similarly, Docker "loads" and "runs" software containers.

  • Using docker we can implement the containerization technologies.

  • Containerization is a concept that allows us to package software applications along with the necessary libraries and binaries required to run them. This package is called an image.

  • Docker is tool/platform that makes it easy to create, manage or run the containers.

Click here to read the definitive guide to Docker.

Spring Boot Application with Docker.pngWhy Docker?

Suppose we're building a Spring Boot application on our local machine. It runs perfectly because our system has the correct versions of Java, Maven, and other necessary tools installed. However, when we want to deploy this application to another machine, we have to manually set up all the required software.

In real-world projects, applications are typically deployed to multiple environments for testing and production, such as DEV, UAT, SIT, and PROD.

Ensuring that each of these environments has the exact setup can be a time-consuming and error-prone process. One machine might have a different Java version or might be missing certain dependencies, leading to unexpected errors.

Now, if we use Docker, we can create a Dockerfile that includes our application, the exact Java version, and all required settings. Once packaged into a Docker image, the application can run on any system with Docker installed, without worrying about environment-specific configurations. We can run the same container across all environments just as we do locally.

This consistency not only saves time but also greatly simplifies both development and deployment.

Prerequisites

To follow along, ensure you have:

Simple Spring Boot Application

We’ll use a simple Spring Boot project with just one REST API that returns "Hello, World!" when the /hello endpoint is accessed. We’ve pushed the code to GitHub, so you can simply clone the repository and switch to the master branch.

You can check out the full code here:

https://github.com/ayushstwt/docker-spring-boot

Test Spring Boot Application on localhost

http://localhost:8080/hello

image (17).png

Docker Terminology

  • Docker file- It contains set of instructions to build docker image.

  • Docker Image- It is a package which contains application code + dependencies.

  • Docker Hub- It is a registry to store docker images. (AWS ECR)

  • Docker Container- It is a runtime instance of our application Run time instance of an image. If you run docker image container will be created that's where our application is running.

To Generate docker image from our spring-boot application there are three different commonly used approaches. we can choose one of theme.

  • Dockerfile

  • Buildpacks

  • Google Jib

Approach 1

Running a Spring Boot application as a container using Dockerfile

Step 1 - Run the maven command, “mvn clean install”. to generate a fat jar inside the target folder.

Step 2- Create the file name as Dockerfile inside the root directory to generate a Docker image.

FROM openjdk:17-jdk-slim
COPY target/docker-spring-boot-0.0.1-SNAPSHOT.jar docker-spring-boot-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","/docker-spring-boot-0.0.1-SNAPSHOT.jar"]

Here in the above file

FROM openjdk:17-jdk-slim – Uses a lightweight OpenJDK 17 base image to run the Spring Boot application. COPY target/docker-spring-boot-0.0.1-SNAPSHOT.jar docker-spring-boot-0.0.1-SNAPSHOT.jar – Copies the built JAR file from the target directory into the Docker image. ENTRYPOINT ["java","-jar","/docker-spring-boot-0.0.1-SNAPSHOT.jar"] – Defines the command to run the Spring Boot JAR when the container starts.

  • To use Docker, first download Docker Desktop from docker.com/products/docker-desktop. Pick the version for your OS (Windows or macOS). If you're on Linux, follow the guide at docs.docker.com/engine/install. Install it like any app and launch Docker Desktop.

  • Next, open your terminal and run docker login. Enter your Docker Hub username and password. If you don’t have an account, create one at hub.docker.com/signup.

  • Once logged in, you’re ready to build images, run containers, and manage apps easily.

Step 3 - Execute the docker command to generate the docker image based on the tag name. make sure that your docker desktop is running.


// syntax
docker build -t your-image-name:tag .

// command
docker build -t ayshriv/docker-spring-boot:latest .

After executing this command, you’ll see an output indicating that the Docker image was successfully generated. The docker build command is used to create a Docker image from a Dockerfile. The -t flag allows you to specify a name for your image and optionally assign a version tag.

For example, in docker-spring-boot:latest, docker-spring-boot is the image name and latest is the tag. The . (dot) at the end tells Docker to use the current directory as the build context, where it looks for the Dockerfile and all necessary files.

image (18).pngTo verify that the image was successfully created, use the docker images command. This will list all the Docker images available on your system.

image (19).pngStep 4 - Next, execute the following command to create and run a Docker container based on the image you just built.

docker run -p 8080:8080 ayshriv/docker-spring-boot:latest

The command docker run -p 8080:8080 ayshriv/docker-spring-boot:latest is used to start a Docker container from the image we built. The -p 8080:8080 option maps port 8080 on your host machine to port 8080 inside the container, allowing you to access the application via http://localhost:8080.

Here, ayshriv/docker-spring-boot:latest is the name and tag of the Docker image. This command runs your Spring Boot application inside a container, exactly as it would run in production.

In the 8080:8080 format:

  • The first value (host port) is the external port on your machine.

  • The second value (container port) is the internal port your app listens on inside the container.

After executing this command, you should see output indicating that the container is running successfully.

image (20).pngTo check if your container is running, use the docker ps command. This will display a list of all currently running containers.

The Docker image name ayshriv/docker-spring-boot:latest, ayshriv is your Docker Hub username, which helps identify you as the owner of the image. docker-spring-boot is the name of your application, and it's good practice to keep this name short, clear, and related to what your app does.

The part after the colon, latest, is the tag, which usually refers to the current or most recent version of the image. You can also use tags like v1.0, prod, or dev to manage different versions.

While creating a Docker image using a Dockerfile is effective and widely adopted, it does have some drawbacks. One major issue is the size of the resulting image, which can be quite large—especially when using base images like OpenJDK. Larger images take more time to build, transfer, and start, which can negatively impact development speed and deployment efficiency.

Additionally, maintaining a Dockerfile requires ongoing effort, as you'll need to update it whenever your application's structure or dependencies change. In our case, the generated image size is around 428MB, which is quite substantial.

The next approach we'll explore helps reduce image size and requires minimal manual effort.

Approach 2

Running a Spring Boot application as a container using Buildpacks

Cloud Native Buildpacks transform your application source code into images that can run on any cloud. It is an alternative approach to dockerfiles. With the help of Buildpacks we can automatically generate production ready images form our application source code without writing the Dockerfile.

https://buildpacks.io/

Step 1- We need to add the given configuration inside the pom.xml for the image name.

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <image>
                            <name>ayshriv/${project.artifactId}:latest</name>
                    </image>
                </configuration>
            </plugin>
        </plugins>
  </build>

Here, also we follows the same naming convention for image name.

Step 2- Now, we run the given maven command to generate the docker image with out the need of Dockerfile (make sure that docker desktop is running).

mvn spring-boot:build-image

Step 3 – Next, execute the following command to create a Docker container from the image, along with the required port mapping.

docker run -p 8080:8080 ayshriv/docker-spring-boot:latest

With this approach, there’s no need to create a Dockerfile manually, and the resulting image size is also reduced. However, one limitation is that Docker Desktop still needs to be installed on your local machine to build and run the image.

To overcome this dependency, we can use Google JIB, a tool specifically designed for Java applications. JIB allows you to generate a Docker image for your Spring Boot application without needing a Dockerfile or Docker installed locally, while also producing smaller and more optimized images.

Approach 3

Running a Spring Boot application as a container using Google JIB

https://github.com/GoogleContainerTools/jib

Google JIB is used to Build container images for your Java applications, without need of Dockerfile and Docker Desktop. it is an alternative approach to dockerfiles. With the help of Google JIB we can automatically generate production ready images form our application source code without writing the Dockerfile.

Step 1- we need to add the given configuration inside the pom.xml for the image name.

<build>
  <plugins>
    <plugin>
      <groupId>com.google.cloud.tools</groupId>
      <artifactId>jib-maven-plugin</artifactId>
      <version>3.4.0</version> 
      <configuration>
        <to>
          <image>ayshriv/${project.artifactId}:latest</image>
        </to>
      </configuration>
    </plugin>
  </plugins>
</build>

Step 2- Run the given maven command to generate the docker image (for Google JIB there is no need of Docker Desktop to be installed on your local system).

mvn compile jib:dockerBuild

Step 3 – After this, execute the following command to create and run a Docker container based on the image, with the appropriate port mapping.

docker run -p 8080:8080 ayshriv/docker-spring-boot:latest

In this approach, we eliminate the need for a Dockerfile, and the resulting image size is significantly reduced. However, one limitation is that Docker Desktop must be installed on the local system to build and run Docker images. To overcome this dependency, we can use Google JIB, a powerful tool specifically designed for Java applications.

Google JIB allows us to containerize our Spring Boot application without writing a Dockerfile or having Docker installed locally. It integrates directly with Maven or Gradle, enabling seamless image creation and pushing to container registries. Additionally, JIB optimizes image layering, which helps in reducing image size and speeding up rebuild times during development.

Some Commonly Used Docker Commands for Java Developers

# 1. Build Docker image from Dockerfile (usually for Spring Boot app)
docker build -t my-java-app .

# 2. List all images
docker images

# 3. Run the Docker container from the built image
docker run -d -p 8080:8080 --name java-app-container my-java-app

# 4. View running containers
docker ps

# 5. View all containers (including stopped ones)
docker ps -a

# 6. Stop a running container
docker stop java-app-container

# 7. Start a stopped container
docker start java-app-container

# 8. Restart a container
docker restart java-app-container

# 9. Remove a container
docker rm java-app-container

# 10. Remove an image
docker rmi my-java-app

# 11. View logs of a container (helpful for debugging)
docker logs java-app-container

# 12. Execute a command inside a running container (like inspecting files, checking logs)
docker exec -it java-app-container bash

# 13. Tag an image (e.g., for pushing to Docker Hub)
docker tag my-java-app ayushshrivastaav/my-java-app:latest

# 14. Login to Docker Hub
docker login

# 15. Push image to Docker Hub
docker push ayushshrivastaav/my-java-app:latest

# 16. Pull image from Docker Hub
docker pull ayushshrivastaav/my-java-app:latest

# 17. Remove all stopped containers
docker container prune -f

# 18. Remove all unused images
docker image prune -a -f

# 19. Check Docker version
docker version

# 20. View system-wide Docker resource usage
docker system df

# 21. Compose up (if using docker-compose.yml for multi-container apps)
docker-compose up -d

# 22. Compose down
docker-compose down

Course image
Become a Java Backend Engineeer today

All-in-one Java course for learning backend engineering with Java. This comprehensive course is designed for Java developers seeking proficiency in Java.

Start Learning Now

Whenever you're ready

There are 4 ways we can help you become a great backend engineer:

The MB Platform

Join 1000+ backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learnings and set schedules, and solve backend engineering tasks, exercises, and challenges.

The MB Academy

The “MB Academy” is a 6-month intensive Advanced Backend Engineering BootCamp to produce great backend engineers.

Join Backend Weekly

If you like post like this, you will absolutely enjoy our exclusive weekly newsletter, Sharing exclusive backend engineering resources to help you become a great Backend Engineer.

Get Backend Jobs

Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board

Backend Tips, Every week

Backend Tips, Every week