CodeGym /Courses /Module 5. Spring /Lecture 148: Integrating Docker with Spring Boot for cont...

Lecture 148: Integrating Docker with Spring Boot for containerizing applications

Module 5. Spring
Level 22 , Lesson 7
Available

Before we dive into code, let's talk about why Docker matters. Imagine your Spring Boot project runs perfectly on your local machine, but on the server it starts acting up: the wrong Java version, missing libraries, or maybe the server just went on a coffee break. Docker fixes that by creating containers that are self-contained, portable, and predictable.

Now that you're ready to turn your Spring Boot project into a container, let's jump into the practical part.


Setting up Docker for Spring Boot

To integrate Spring Boot with Docker, we need a few key steps. It all starts with creating a Dockerfile.

Step 1: Create a Dockerfile

Dockerfile is the magic file that tells Docker how to package your application. Inside we specify a base image (for example, openjdk), copy your JAR file, and tell Docker how to run the app.

Here's what a minimal Dockerfile for Spring Boot looks like:


# Use a base image with Java 17 (or your version)
FROM eclipse-temurin:17-jdk

# Set the working directory inside the container
WORKDIR /app

# Copy the JAR file into the container
COPY target/your-app-name-1.0.0.jar app.jar

# Specify the command to run the application
ENTRYPOINT ["java", "-jar", "app.jar"]
  • FROM: base image for the container (in this case, it's OpenJDK).
  • WORKDIR: sets the working directory inside the container.
  • COPY: moves files from your local project into the container.
  • ENTRYPOINT: specifies the command that runs when the container starts.

Note: target/your-app-name-1.0.0.jar is your built JAR file, produced earlier with Maven (mvn package).

Step 2: Build the Docker image

Once the Dockerfile is ready, build the Docker image. An image is the "packaged version" of your app that contains everything it needs:


docker build -t your-app-name:1.0.0 .
  • -t your-app-name:1.0.0: gives your image a name and version.
  • .: points to the current directory where the Dockerfile lives.

If everything went well, you should see something like:


Successfully built <image-id>

You can check if the image was created with:


docker images

Step 3: Run the container

Now that we have an image, we can run a container. Do it like this:


docker run -p 8080:8080 your-app-name:1.0.0
  • -p 8080:8080: maps a port from your local machine to the container. For example, your Spring Boot application serves requests on localhost:8080.
  • your-app-name:1.0.0: the image name we just built.

After running that command, the app should be available at http://localhost:8080.


Practice: Improving the Dockerfile

The minimal Dockerfile is fine for getting started, but we can make it better.

You can optimize Docker images to reduce their size. One common approach is multi-stage builds.

Here's an improved Dockerfile:


# First stage: build the application
FROM maven:3.8.7-eclipse-temurin-17 AS builder

WORKDIR /build
COPY pom.xml ./
COPY src ./src

RUN mvn clean package -DskipTests

# Second stage: create a minimal image
FROM eclipse-temurin:17-jre

WORKDIR /app
COPY --from=builder /build/target/your-app-name-1.0.0.jar app.jar

ENTRYPOINT ["java", "-jar", "app.jar"]

What's happening here:

  1. First stage: we pull maven and build the project (skipping tests to speed things up).
  2. Second stage: we use a lightweight jre image (just the JVM, no build tools), and copy the built JAR into the container.

This approach reduces the final image size because Maven and the source code don't end up in the final image.


Practice: Building and testing the container

Full cycle:

  1. Create the JAR for your app:
    
    mvn clean package
    
  2. Build the image:
    
    docker build -t your-app-name:1.0.0 .
    
  3. Run the container:
    
    docker run -p 8080:8080 your-app-name:1.0.0
    

Now you can test the app in your browser or with Postman.


Working with environment variables

In real apps you often need to configure parameters (like database connection). Instead of hardcoding them, it's better to use environment variables.

Let's update the Dockerfile to work with environment variables:


ENV SPRING_PROFILES_ACTIVE=prod

ENTRYPOINT ["java", "-jar", "-Dspring.profiles.active=${SPRING_PROFILES_ACTIVE}", "app.jar"]

Now the active Spring profile can be set via the SPRING_PROFILES_ACTIVE variable. For example:


docker run -p 8080:8080 -e SPRING_PROFILES_ACTIVE=dev your-app-name:1.0.0

Common errors and how to avoid them

  1. Error: "Connection refused" when trying to access the container. This happens if ports weren't mapped. Make sure you run with -p 8080:8080.
  2. Error: "No such file or directory" when starting the container. Make sure the path to the JAR in the COPY inside the Dockerfile is correct.
  3. Image is too large. Use multi-stage builds like shown above.
  4. Application can't find the database. Configure environment variables so the app can connect to external resources like the database.

How Docker helps in real life?

  • On interviews: knowing how to create containers and work with Dockerfile is a must-have for modern devs.
  • On projects: with Docker you can spin up both local environments and production environments quickly.
  • In microservices: every microservice can run in its own container. Updating, scaling, and managing them gets way easier.

If you manage to master Docker and Spring Boot integration, you'll become indispensable to your team! Get ready to use this knowledge in future lectures about cloud deployment and microservice architecture.

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION