About Us Contact Us Write for Us Advertise
Home > Java > Dockerizing a Spring Boot App (Beginner Guide)
Java

Dockerizing a Spring Boot App (Beginner Guide)

Learn to containerize Spring Boot with Docker: what containers and images are, writing a Dockerfile, build and run commands, multi-stage builds, and docker-compose.

Shiv Pandey
Shiv Pandey
Oct 08, 2026 | 135 views
Dockerizing a Spring Boot App (Beginner Guide)

"It works on my machine!" — the most famous excuse in software, and the problem Docker solves. Docker packages your app and everything it needs to run into a portable box called a container, so it behaves identically on your laptop, a teammate's machine, and the production server. It's a cornerstone of modern deployment, and Spring Boot apps are a perfect fit. Let's demystify it.

The problem: environments differ

Your app runs fine on your computer — but the server has a different Java version, missing settings, or a different OS, and suddenly it breaks. Setting up each environment identically by hand is tedious and error-prone. Docker eliminates this by shipping the environment with the app.

What Docker actually is (plain English)

  • A container is a lightweight, isolated box that holds your app plus its runtime, libraries, and config — everything it needs.
  • A container runs the same way everywhere, because it carries its own environment. No more "works on my machine."
  • An image is the blueprint; a container is a running instance of that image. (Think class vs object, if that helps.)
Analogy: a container is like a shipping container for software. Whatever's inside, it has a standard shape, so any ship, truck, or crane (any machine running Docker) can carry it without caring about the contents. Package once, run anywhere.

The Dockerfile: your app's recipe

You describe how to build your app's image in a file called Dockerfile. For a Spring Boot app, it's short — build the jar, then run it:

# start from a base image that has Java
FROM eclipse-temurin:17-jre

# copy your built jar into the image
COPY target/myapp.jar app.jar

# the command that runs when the container starts
ENTRYPOINT ["java", "-jar", "/app.jar"]

That's genuinely it for a basic Spring Boot app. FROM picks a starting image with Java already installed, COPY adds your jar, and ENTRYPOINT says how to launch it. Remember from Lesson 1 that Spring Boot bundles its own web server — that's why "just run the jar" is enough; there's no separate server to install.

Building and running

Two commands take you from Dockerfile to a running container:

# build the image (tag it "myapp")
docker build -t myapp .

# run a container, mapping port 8080 to your machine
docker run -p 8080:8080 myapp

Now your app runs inside a container, reachable at localhost:8080 — and that same image will run identically on any server with Docker. Package once, run anywhere.

Multi-stage builds (a pro touch)

A common improvement: build the jar inside Docker too, using a "multi-stage" build. The first stage compiles with the full JDK; the final stage keeps only the slim runtime + jar, producing a smaller, cleaner image:

# stage 1: build
FROM maven:3.9-eclipse-temurin-17 AS build
COPY . .
RUN mvn clean package -DskipTests

# stage 2: run (small final image)
FROM eclipse-temurin:17-jre
COPY --from=build target/*.jar app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]

Don't worry if this looks advanced — just know it exists. The simple Dockerfile above is perfectly fine to start with.

Adding a database with docker-compose

Real apps need a database too. Docker Compose lets you define and run your app and a PostgreSQL container together with a single file and command, so your whole stack starts with docker compose up. It's the natural next step once you're comfortable with single containers — and how many teams run everything locally.

Docker felt intimidating from the outside — all that infrastructure vocabulary — but the core idea clicked fast once I framed it as "a box that carries my app's whole environment with it." That framing is the whole thing. This website ships as a Docker image, which means the exact same build runs in every environment, and deployment becomes "run this image" instead of a fragile manual setup. My advice: start with the simple three-line Dockerfile above, get your app running in a container locally, and then explore compose and multi-stage builds. Once you've containerised one app, you've unlocked how modern software is shipped everywhere.

Key takeaways

  • Docker packages your app + its environment into a portable container that runs identically everywhere.
  • An image is the blueprint; a container is a running instance. A Dockerfile describes how to build the image.
  • For Spring Boot: FROM a Java base image, COPY the jar, ENTRYPOINT runs it — then docker build and docker run.
  • Use multi-stage builds for smaller images and Docker Compose to run your app with a database together.

← Previous: Spring Lesson 15 — Async & Scheduling
Next: Spring Lesson 17 — Deploying to Production →
↑ Back to the Spring Boot roadmap

Related Articles

Deploying a Spring Boot App to Production (Beginner Guide)
Java

Deploying a Spring Boot App to Production (Beginner Guide)

Build Your First Spring Boot App (Step-by-Step Beginner Guide)
Java

Build Your First Spring Boot App (Step-by-Step Beginner Guide)

Spring vs Spring Boot: The Difference Explained (Beginner Guide)
Java

Spring vs Spring Boot: The Difference Explained (Beginner Guide)

Spring Boot Project Ideas (Beginner to Advanced)
Java

Spring Boot Project Ideas (Beginner to Advanced)