CodeGym /Courses /Module 5. Spring /Lecture 149: Hands-on: building a Docker image for a Spri...

Lecture 149: Hands-on: building a Docker image for a Spring Boot app

Module 5. Spring
Level 22 , Lesson 8
Available

Quick reminder: Docker lets you:

  • Create containers that include not only your app but all its dependencies.
  • Provide portability, since the container runs the same everywhere.
  • Isolate apps from each other.
  • Simplify deployment to the cloud, to servers, or even to your local laptop.

Ready? Let's dive in!


Project structure

First, make sure you have a Spring Boot app that builds a jar via Maven. For example, your project structure might look like this:


my-spring-boot-app/
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com.example.demo/
│   │   │       ├── DemoApplication.java
│   │   │       └── controllers/
│   │   │           └── HelloController.java
│   │   └── resources/
│   │       └── application.properties
├── pom.xml
├── target/
│   └── my-spring-boot-app-0.0.1-SNAPSHOT.jar
└── Dockerfile

We'll create the Dockerfile today.


Step 1: Create a Dockerfile

Dockerfile is the "recipe" for building a Docker image. Open your project and create a new file named Dockerfile in the project root.

Minimal example Dockerfile


# Use a base image with Java installed
FROM openjdk:17-jdk-slim

# Set the working directory inside the container
WORKDIR /app

# Copy the app's jar file into the container
COPY target/my-spring-boot-app-0.0.1-SNAPSHOT.jar app.jar

# Specify the command to run the app
ENTRYPOINT ["java", "-jar", "app.jar"]
  • FROM openjdk:17-jdk-slim
    Specifies the base image for the container. This is a lightweight image with Java 17.
  • WORKDIR /app
    Sets the working directory inside the container where the app will run.
  • COPY target/my-spring-boot-app-0.0.1-SNAPSHOT.jar app.jar
    Copies the app.jar from the local target directory into the container's /app directory.
  • ENTRYPOINT ["java", "-jar", "app.jar"]
    Specifies the command that runs when the container starts.

Step 2: Build the Docker image

Now that the Dockerfile is ready, you can build the Docker image. Run these commands in your terminal:


# Go to the project folder
cd /path/to/my-spring-boot-app

# Build the jar if you don't have it yet
mvn clean package

# Build the Docker image
docker build -t my-spring-boot-app:latest .
  • docker build
    Command to build a Docker image.
  • -t my-spring-boot-app:latest
    Specifies the name (tag) for the image you're creating. That's "my-spring-boot-app" with the "latest" tag.
  • .
    Specifies the directory where the Dockerfile is located.

Step 3: Run the Docker container

After building the image you can run it as a container:


docker run -d -p 8080:8080 --name spring-app my-spring-boot-app:latest
  • -d
    Runs the container in the background (detached mode).
  • -p 8080:8080
    Maps host port 8080 to container port 8080.
  • --name spring-app
    Sets a name for the container.
  • my-spring-boot-app:latest
    Specifies the Docker image to use.

Now your app should be reachable at http://localhost:8080.


Step 4: Verify the container is running

To make sure the container is running, run these commands:


# Check running containers
docker ps

# View the app logs
docker logs spring-app

You should see logs from your Spring Boot app.


Tips and common mistakes

  1. If the jar isn't being created, make sure you successfully built the project with Maven (mvn clean package).
  2. If the Docker image won't build, check that the paths in your Dockerfile are correct.
  3. If the app inside the container won't start, check the logs with docker logs spring-app.

Additional Dockerfile tweaks

1. Passing environment variables

You can pass variables into the container:


ENV SERVER_PORT=8080
ENTRYPOINT ["java", "-jar", "app.jar", "--server.port=${SERVER_PORT}"]

Now you can set the port when running the container:


docker run -d -p 9090:9090 -e SERVER_PORT=9090 my-spring-boot-app:latest

2. Making the Docker image smaller

Slim images save space. Instead of the openjdk image, consider using distroless:


FROM gcr.io/distroless/java:17
COPY target/my-spring-boot-app-0.0.1-SNAPSHOT.jar app.jar
CMD ["app.jar"]

What's next?

With Docker your apps can run the same way on your dev laptop, on a server, or in the cloud. Next up we'll learn how to push Docker images to registries and deploy them to platforms like AWS or Kubernetes!

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