DEV Community

Cover image for Containerizing Spring Boot Applications with Docker: A Complete Guide
Ayush Shrivastava for Mastering Backend

Posted on • Edited on • Originally published at masteringbackend.com

9 1

Containerizing Spring Boot Applications with Docker: A Complete Guide

Containerizing Spring Boot Applications with Docker

In today's world of cloud computing and microservices, deploying applications efficiently is a big challenge. This is where Docker comes in. Docker helps in packaging applications along with all their dependencies, making them easy to deploy anywhere.

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.

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 is a word that is driven from dock-worker, dock worker is a job of person who loads and un loads the containers in a ship from where it has been names as ‘Docker’.
  • Using docker we can implement the containerization technologies.
  • Containerization is concept through which we can package software applications and the corresponding libraries/binaries which are required for running the application. and that package is called image.
  • Docker is tool/platform that makes it easy to create, manage or run the containers.

Spring Boot Application with Docker.png

Why Docker ?

suppose that we are building a spring boot application on our local machine. it runs perfectly because our system has the right version of java, maven and other tools installed. In order to deploy our application in a another machine we need to setup all the Software which are required to our application. In a real time project should be deployed into multiple environments for testing purpose such as DEV, UAT, PROD, SIT etc. To deploy application to these many environments we need to take of all the software required to run our application in all environments. It is very difficult task. Maybe another machine has different java version, or it is missing some dependencies. then it throws errors.

Now, let’s we use Docker, we create a Dockerfile that includes our application, the exact java version, and all the requires settings. once it is packaged into a docker image, it can run on any system with Docker installed- without worrying about what’s installed on that machine. now we can run the same container in every environments as we run in our local system. so we can say that docker help us to save the time and simplifies both development and deployment.

Prerequisites

  • Basic knowledge of Java, Spring Boot and Maven.
  • Installed Java, Maven, Docker and Docker CLI and IDE (IntelliJ, VS Code).

Simple Spring Boot Application

I created a basic Spring Boot project with just one REST API that returns "Hello, World!" when we hit the /hello endpoint. and I push the code of spring boot application on GitHub. so you just clone it and switch to master branch.

You can check out the full code here :

ayushstwt/docker-spring-boot

Test Spring Boot Application on localhost

http://localhost:8080/hello

image.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"]
Enter fullscreen mode Exit fullscreen mode

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 .**
Enter fullscreen mode Exit fullscreen mode

after executing this command you get this output that means our image is successfully generated. here the docker build command is used to create a Docker image from a Dockerfile. The -t flag allows you to give your image a name and optionally add a version tag. For example, docker-spring-boot is the name and : latest is version of the image you're creating. The . (dot) at the end tells Docker to look in the current directory for the Dockerfile and all the necessary files to build the image.

image.png

now check that image is generated or not we use the command docker images to see all the images.

image.png

step 4 - after this execute the command that creates the docker container based on the image.

docker run -p 8080:8080 ayshriv/**docker-spring-boot:latest**
Enter fullscreen mode Exit fullscreen mode

here the command docker run -p 8080:8080 ayshriv/docker-spring-boot:latest is used to start a Docker container from our image. The -p 8080:8080 part maps your computer’s port 8080 to the container’s port 8080, so you can access the app in your browser at http://localhost:8080. The ayshriv/docker-spring-boot:latest is the name of the image you're running from Docker Hub. This command makes your Spring Boot app run inside a container. here in 8080:8080 where first value represents the external port and the second value represents the container port. after executing this command we get the out like this and now our container is running successfully.

image.png

to check that our container is running or not we used the command that is docker ps that show all the 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 this approach of creating a Docker image using a Dockerfile is effective and widely used, one of the main disadvantages is that the resulting image can be quite large, especially when using a base image like OpenJDK. A larger image takes more time to build, transfer, and start, which can slow down development and deployment. Additionally, maintaining the Dockerfile requires extra effort, as you need to keep it updated whenever the application structure or dependencies change. here we show that the image size is 428MB that is very large now next approach helps to reduce the size and minimal efforts as well.

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Step 3- after this executing the given command that creates the docker container based on the image and also port mapping provided.

docker run -p 8080:8080 ayshriv/docker-spring-boot:latest
Enter fullscreen mode Exit fullscreen mode

now in this approach no need of Dockerfile and also the image size is also reducing. but there is one more problem here in our local system we need a Docker desktop is installed. to remove this problem we use Google JIB this is only used for java application to generate the imagef of our springboot application without need of Dockerfile and also the size is reduced.

Approach 3

Running a Spring Boot application as a container using Google JIB

GitHub - GoogleContainerTools/jib: 🏗 Build container images for your Java applications.

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>

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Step 3- after this executing the given command that creates the docker container based on the image and also port mapping provided.

docker run -p 8080:8080 ayshriv/docker-spring-boot:latest
Enter fullscreen mode Exit fullscreen mode

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 Common 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

Enter fullscreen mode Exit fullscreen mode

Conclusion

Using Docker with Spring Boot makes application deployment faster, more reliable, and scalable. With just a few steps, you can package your application and run it anywhere—on your local machine, cloud, or any server that supports Docker. This approach is widely used in DevOps and Microservices Architecture to ensure smooth deployment.

Start containerizing your Spring Boot apps today and experience the power of Docker!

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (1)

Collapse
 
jance_jacobs profile image
Jance Jacobs

Can you explain how Buildpacks automatically detect and include required dependencies for the Spring Boot app without a Dockerfile?

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️