Shoomer

  • Docker
    DockerShow More
    Monolith to Microservices: A Docker + K8s Migration Story
    8 Min Read
    Docker Security Best Practices | Scout Trivy Scans
    8 Min Read
    CI/CD with Docker and Kubernetes with Examples YML
    10 Min Read
    Docker Networking Deep Dive | Bridge, Host, Overlay
    9 Min Read
    Docker Volumes and Bind Mounts Explained with Examples
    7 Min Read
  • Kubernetes
    KubernetesShow More
    Zero to Hero Kubernetes Crash Course – Minikube, kubectl, Helm Quickstart
    7 Min Read
    Spring Boot Web Crash Course 2025 – REST APIs, Controllers, Get/Post
    7 Min Read
    K8s Crash Course – Learn Containers to Clusters (Hands-On in 2025)
    7 Min Read
    Spring Data JPA Crash Course 2025 – Repository, Query Methods & Paging
    7 Min Read
    Spring Boot for Web Development – Crash Course with Thymeleaf & MVC
    7 Min Read
  • CICD Pipelines
    CICD PipelinesShow More
    What is GitOps with ArgoCD: Deep Dive into Architecture
    10 Min Read
    CI/CD with Docker and Kubernetes with Examples YML
    10 Min Read
  • Pages
    • About Us
    • Contact Us
    • Cookies Policy
    • Disclaimer
    • Privacy Policy
    • Terms of Use
Notification Show More
Font ResizerAa
Font ResizerAa

Shoomer

  • Learning & Education
  • Docker
  • Technology
  • Donate US
Search
  • Home
  • Categories
    • Learning & Education
    • Technology
    • Docker
  • More Foxiz
    • Donate US
    • Complaint
    • Sitemap
Follow US
Home » How to Containerize a Spring Boot App with Docker
Docker

How to Containerize a Spring Boot App with Docker

shoomer
By shoomer
Last updated: June 11, 2025
Share

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.

Contents
Table of ContentsDockerfile for Spring BootSample DockerfileExplanation of Dockerfile InstructionsBuilding the Spring Boot JAR.dockerignore UsageExample .dockerignore FileKey Benefits of .dockerignoreBuilding and Running the ContainerStep 1 – Build the Docker ImageStep 2 – Verify the ImageStep 3 – Run the ContainerExposing PortsWhy Expose Ports?Methods of Port MappingVerify Port BindingRunning the Spring Boot AppStop the Running ContainerStart the Stopped ContainerRemove the ContainerRemove the Docker ImageExternal Resources for Further LearningFinal Thoughts

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

  1. Dockerfile for Spring Boot
  2. .dockerignore Usage
  3. Building and Running the Container
  4. Exposing Ports
  5. Running the Spring Boot App
  6. External Resources for Further Learning
  7. 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

  1. FROM
    Specifies the base image. Here, we use openjdk, which contains the Java Development Kit (JDK) required to run our Spring Boot app.
  2. WORKDIR
    Sets the working directory inside the container to /app.
  3. COPY
    Copies the Spring Boot JAR file from your local machine into the container.
  4. EXPOSE
    Declares the port on which the application will run. Note that this does not publish the port but informs Docker of the intent.
  5. ENTRYPOINT
    Defines the command to be executed when the container starts. Here, it tells Docker to run the Spring Boot app using java -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

  1. Reduces the size of the Docker image by excluding unnecessary files (e.g., logs, version control metadata, and IDE settings).
  2. Minimizes security risks by preventing sensitive files (e.g., .env) from being included in the image.
  3. 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

  1. Single Port Mapping: docker run -p 8080:8080 spring-boot-app Maps container port 8080 to host port 8080.
  2. Dynamic Port Mapping: docker run -P spring-boot-app Randomly maps container ports to available host ports. Run docker 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.

Share This Article
Facebook Email Copy Link Print
Previous Article What is Kubernetes and Why Use It? YAML Configuration Setup
Next Article Docker Compose for Local Microservices | Multi-Container Orchestration
Leave a Comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Empowering Tomorrow's Leaders through Understanding Child Development and Learning

Learning to thrive

Daily Feed

Zero to Hero Kubernetes Crash Course – Minikube, kubectl, Helm Quickstart
June 23, 2025
Spring Boot Web Crash Course 2025 – REST APIs, Controllers, Get/Post
June 23, 2025
K8s Crash Course – Learn Containers to Clusters (Hands-On in 2025)
June 23, 2025
Spring Data JPA Crash Course 2025 – Repository, Query Methods & Paging
June 23, 2025

You Might Also Like

Docker

What is Docker? A Beginner’s Guide Spring Boot Setup

June 10, 2025
Docker

Docker Security Best Practices | Scout Trivy Scans

June 11, 2025
Docker

Docker Networking Deep Dive | Bridge, Host, Overlay

June 11, 2025
Docker

What is Kubernetes and Why Use It? YAML Configuration Setup

June 10, 2025
@Copyright 2025
  • Docker
  • Technology
  • Learning & Education
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?