← Back to list

Dockerizing Spring Boot Apps the Right Way

Containerization has become a core practice in modern backend development. For Java developers, Docker offers a streamlined way to package…

Pallavi Shankar Sutar · 2025-09-02 10:13 · 2 claps · 2.1 min read
#dockerfiles #docker-compose #dockerizing
Open on Medium ↗
Wiki topics: 🌐 · Web Development ☁️ · DevOps & Cloud

Dockerizing Spring Boot Apps the Right Way

Containerization has become a core practice in modern backend development. For Java developers, Docker offers a streamlined way to package and deploy Spring Boot applications across different environments with consistent behaviour.

In this blog, we’ll walk through how to Dockerize a Spring Boot app effectively, with real examples and best practices — including Docker Compose integration for managing multi-container environments.

🔹 Preparing Your Spring Boot Application

Before Dockerizing your Spring Boot app, ensure it’s set up to build a self-contained executable JAR. You can do this using either Maven or Gradle.

✅ Maven Setup

Ensure your pom.xml includes:

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

Then, build the app with:

./mvnw clean package

This will create a fat JAR in the /target directory — perfect for Docker.

🔸 Creating an Optimized Dockerfile

Here’s a Docker Spring Boot example using a basic Dockerfile:

FROM openjdk:17-jdk-slim
VOLUME /tmp
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

🔍 Explanation:

  • Base image: Uses a minimal JDK image to reduce size.
  • COPY: Transfers the JAR to the container.
  • ENTRYPOINT: Runs the application when the container starts.

🔸 Building and Running the Docker Image

🧱 Build the Docker image:

docker build -t my-spring-app .

🚀 Run the container:

docker run -p 8080:8080 my-spring-app

Your Spring Boot app should now be available at http://localhost:8080.

🔸 Adding Docker Compose for Multi-Container Setup

If your application connects to a database or other services, Docker Compose can simplify orchestration.

✅ Example: Spring Boot + PostgreSQL

Create a docker-compose.yml:

version: '3.8'
services:
app:
build: .
ports:
- "8080:8080"
depends_on:
- db
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/mydb
SPRING_DATASOURCE_USERNAME: user
SPRING_DATASOURCE_PASSWORD: password
db:
image: postgres:13
environment:
POSTGRES_DB: mydb
POSTGRES_USER: user
POSTGRES_PASSWORD: password
ports:
- "5432:5432"

🌐 Run both services:

docker-compose up - build

This spins up both your Spring Boot app and PostgreSQL database, fully wired via environment variables.

🔸 Best Practices for Dockerizing Spring Boot Apps

✅ 1. Use Lightweight Base Images

Instead of openjdk, consider eclipse-temurin or distroless for production:

FROM eclipse-temurin:17-jre-alpine

✅ 2. Externalize Configuration

Avoid hardcoding sensitive values. Use environment variables or bind-mount external config files.

docker run -e SPRING_PROFILES_ACTIVE=prod -p 8080:8080 my-spring-app

✅ 3. Use .dockerignore

Prevent unnecessary files from bloating the image:

target/

.git/

*.md

✅ 4. Add Health Checks in Compose

healthcheck:

test: [“CMD”, “curl”, “-f”, “http://localhost:8080/actuator/health"]

interval: 30s

timeout: 10s

retries: 5

✅ 5. Use Multi-Stage Builds (Optional)

For cleaner builds with Maven inside Docker:

Build Stage

FROM maven:3.8.5-openjdk-17 AS build
COPY . /app
WORKDIR /app
RUN mvn clean package

Run Stage

FROM openjdk:17-jdk-slim
COPY - from=build /app/target/*.jar app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]

🔹 Conclusion

Dockerizing Spring Boot apps isn’t just about running your app in a container — it’s about doing it in a way that supports scalability, environment consistency, and collaboration. With a clean Dockerfile, efficient image layering, and Docker Compose for multi-service setups, you can prepare your backend for real-world deployment pipelines.


메타데이터
post_id
0b906d760fdd
slug
dockerizing-spring-boot-apps-the-right-way-0b906d760fdd
url
https://medium.com/@techblogsbypallavi/dockerizing-spring-boot-apps-the-right-way-0b906d760fdd
canonical_url
https://medium.com/@techblogsbypallavi/dockerizing-spring-boot-apps-the-right-way-0b906d760fdd
author_url
https://medium.com/@techblogsbypallavi
status
ok
fetched_at
2026-06-22 12:55:45