Hey there, Spring Boot lovers! 🌱
Ever wondered how to wrap your awesome Spring Boot app in a Docker container and make it run anywhere—like magic? 🪄
You’re in the right place! In this guide, we’ll go step-by-step to Dockerize your Spring Boot app and help you feel like a DevOps superhero 🦸.
🧠 What You’ll Learn
- What Docker is (super simply)
- How to create a Dockerfile for Spring Boot
- How to build and run your Docker container
- How to smile while doing it 😄
📦 Step 1: What’s Docker Anyway?
Imagine you’re sending your app to a friend. You don’t want to tell them to install Java, download dependencies, and fix bugs from “it works on my machine.” 😅
Docker solves that by putting everything your app needs in one box — called a container.
Think of it like a lunchbox for your app. 🍱
⚙️ Step 2: Create a Simple Spring Boot App
If you already have one, skip this part. If not:
- Go to https://start.spring.io
- Choose:
- Project: Maven
- Language: Java
- Spring Boot: Latest
- Dependencies: Spring Web
- Click Generate, unzip the project, and open it in your IDE (like IntelliJ or VS Code)
Then create a simple controller:
@RestController
public class HelloController {
@GetMapping("/")
public String hello() {
return "Hello from Dockerized Spring Boot! 🚢";
}
}
🧾 Step 3: Add a Dockerfile
In the root of your project, create a file called Dockerfile
(no file extension).
# Start from an official Java image
FROM openjdk:21-jdk-slim
# Set the working directory
WORKDIR /app
# Copy the built JAR file
COPY target/*.jar app.jar
# Expose port 8080
EXPOSE 8080
# Run the app
ENTRYPOINT ["java", "-jar", "app.jar"]
✅ Make sure your app is built first by running:
./mvnw clean package
You should get a file like: target/your-app-name-0.0.1-SNAPSHOT.jar
.
🛠️ Step 4: Build the Docker Image
Now let's build the Docker image (like baking the lunchbox):
docker build -t spring-boot-docker .
🏷️
-t spring-boot-docker
gives your image a name.
🚗 Step 5: Run the Container
Now run your app inside Docker:
docker run -p 8080:8080 spring-boot-docker
Now open your browser and visit:
http://localhost:8080
🎉 You should see:
Hello from Dockerized Spring Boot! 🚢
💡 Bonus Tips
- You can push your image to Docker Hub to share with the world.
- Want to rebuild? Use
docker build --no-cache -t spring-boot-docker .
- Check running containers with
docker ps
- Stop containers with
docker stop <container_id>
🧁 Wrapping It Up
Now you’ve got a portable, reliable, and awesome Dockerized Spring Boot app! 🌍
You just unlocked the superpower of deploying your Java backend anywhere — on your VPS, cloud server, or even a Raspberry Pi 🍓.
“Docker is like a box of chocolates. You always know what you're gonna get.” 🍫