# Running your Spring Boot App with Docker

**In this article, we will learn:**

* What is Docker
    
* Why use Docker
    
* How to use Docker to run a Spring Boot application (we will keep it short and sweet)
    

## **What is Docker?**

Docker is a tool that allows us to run and distribute our applications in isolated environments called containers.

A **Docker container** is a **lightweight, portable, and isolated environment** where an application runs. It includes:

✅ **The application code**  
✅ **All its dependencies** (libraries, runtime, etc.)  
✅ **A minimal OS environment** (without the full OS)

This ensures the application **runs exactly the same way** across different machines, whether it’s your laptop, a server, or the cloud. 🚀

## **Why use Docker?**

Without Docker, developers often face:

* ❌ **"It works on my machine!"** but fails in production.
    
* ❌ **Dependency hell**—conflicting OS versions, libraries, and configurations.
    
* ❌ **Slow and bloated VMs** that take up too many resources.
    

✅ **Docker solves this** by packaging everything into a **lightweight, portable container**, so your app **runs the same way everywhere**—from your laptop to production.

With that in mind, let’s go ahead and use it to deploy a simple Spring Boot App that runs on port 8080.

## **♦︎** Prerequisites:

You need a Spring Boot application that runs on port 8080. I have done this recently, see my [article](https://code-like-a-woman.hashnode.dev/building-a-rest-api-with-spring-boot-in-10-steps) if you need guidance.

## **📌 Setting Up & Running your App in Docker**

### **1️⃣ Install Docker**

1. **Download and install** [**Docker Desktop**](https://www.docker.com/products/docker-desktop/) from Docker’s official website.
    
2. **Verify** the installation by running:
    
    ```bash
    docker --version
    ```
    

---

### **2️⃣ Create a** `Dockerfile` for Your App

1. Inside the **project root directory**, **create a new file** named `Dockerfile` (no extension).
    
2. **Write the following content inside** `Dockerfile`:
    
    ```plaintext
    # Use an official Java 21 runtime as the base image
    FROM eclipse-temurin:21-jdk-alpine
    
    # Set the working directory inside the container
    WORKDIR /app
    
    # Copy the built JAR file into the container
    COPY target/blog-posts-app.jar app.jar
    
    # Expose the application port
    EXPOSE 8080
    
    # Run the application
    CMD ["java", "-jar", "app.jar"]
    ```
    
    👉 **"Make sure to update** `blog-posts-app.jar` with your actual JAR filename and verify that the Java version in the `Dockerfile` matches your project requirements."
    

---

### **3️⃣ Modify** `pom.xml` to Control JAR Filename

1. Open `pom.xml` in the project directory.
    
2. Inside the `<build>` section, **add the following** to ensure the generated JAR file has a consistent name (the same name that you have used on your docker file):
    
    ```xml
    <finalName>blog-posts-app</finalName>
    ```
    

---

### **4️⃣ Build the Docker Image**

1. **Package the Spring Boot application** into a JAR file:
    
    ```bash
    # Package the Spring Boot app
    mvn clean package
    ```
    
2. **Build the Docker image**:
    
    ```bash
    # Build the Docker image
    docker build -t blog-posts-app .
    ```
    
    (Remember to use the name of your app)
    
    > 🚀 **Tip:** A **Docker image** is the final, packaged version of an application, created using a **Dockerfile**.  
    > It’s like a **pre-installed application** that you can run anywhere.
    

---

### **5️⃣ Run the Container**

1. **Start the container** from the built image:
    
    ```bash
    # Run the docker container
    docker run -d -p 8080:8080 --name blog-posts-app blog-posts-app
    ```
    
    (Replace in this command the name of your app and image name)
    
2. **Verify that the container is running**:
    
    ```bash
    ## List the running containers
    docker ps
    ```
    
3. **Test the application** by running it on your browser:
    
    For example, in my case is: [http://localhost:8080/api/posts](http://localhost:8080/api/posts)
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1741294103681/5071d676-c5b5-4dcc-96b1-4bc818a8f9a7.png align="center")
    

## 🎯 **What We Achieved Today:**

* 📦 Packaged our **Spring Boot app** with Maven.
    
* 📜 Created a **Dockerfile** to containerize the app.
    
* 🖼️ Built a **Docker image** from the Dockerfile.
    
* 🚀 Deployed the app inside a **Docker container**.
    
* 🔥 Successfully tested it in a **browser**.
    

Now, your Spring Boot app runs **in a portable, isolated environment**.  
✅ **Next step?** You can **add a database, use Docker Compose, or deploy it to the cloud!**

## **📌 Explore More**

Want to see this in action? \*\*Check out my project on GitHub:\*\*🔗 [**GitHub Repo**](https://github.com/mdjc/blog-posts-app/commit/fdffdf54e1d3d8864f266ca5c18270a2e485d984)
