DEV Community

SOVANNARO
SOVANNARO

Posted on

πŸš€ Deploy Your Spring Boot App with Docker

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:

  1. Go to https://start.spring.io
  2. Choose:
  • Project: Maven
  • Language: Java
  • Spring Boot: Latest
  • Dependencies: Spring Web
    1. 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! 🚒";
    }
}
Enter fullscreen mode Exit fullscreen mode

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

βœ… Make sure your app is built first by running:

./mvnw clean package
Enter fullscreen mode Exit fullscreen mode

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

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

Now open your browser and visit:

http://localhost:8080
Enter fullscreen mode Exit fullscreen mode

πŸŽ‰ 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.” 🍫



Top comments (0)