📖 Scenario: You have created a simple Spring Boot application and want to package it into a Docker container. This will help you run your app anywhere without worrying about the environment setup.
🎯 Goal: Build a Dockerfile that creates a Docker image for your Spring Boot application. The Dockerfile will start from a base Java image, copy your app's jar file, and set the command to run it.
📋 What You'll Learn
Create a Dockerfile starting from the OpenJDK 17 base image
Copy the Spring Boot jar file named app.jar into the image
Set the command to run the jar file using java -jar app.jar
Expose port 8080 for the application
💡 Why This Matters
🌍 Real World
Dockerizing Spring Boot apps is common to deploy them easily on cloud servers or local machines without setup issues.
💼 Career
Knowing how to write Dockerfiles for Java apps is a key skill for DevOps engineers and backend developers working with containerized deployments.
Progress0 / 4 steps
1
Create the base image
Write a line in the Dockerfile to use the OpenJDK 17 image with slim version as the base image. Use FROM openjdk:17-slim.
Spring Boot
Hint
The FROM instruction sets the base image for your Docker image.
2
Copy the Spring Boot jar file
Add a line to copy the file named app.jar from your project folder into the Docker image's root directory. Use COPY app.jar /app.jar.
Spring Boot
Hint
The COPY instruction copies files from your computer into the Docker image.
3
Expose port 8080
Add a line to expose port 8080 in the Dockerfile using EXPOSE 8080. This tells Docker the app listens on this port.
Spring Boot
Hint
The EXPOSE instruction documents the port your app uses.
4
Set the command to run the app
Add a line to run the Spring Boot jar file using CMD ["java", "-jar", "/app.jar"]. This tells Docker how to start your app.
Spring Boot
Hint
The CMD instruction sets the default command to run when the container starts.
Practice
(1/5)
1. What is the main purpose of a Dockerfile in a Spring Boot project?
easy
A. To write the Java code for the Spring Boot application
B. To define how to package and run the Spring Boot app inside a container
C. To configure the database connection settings
D. To create unit tests for the Spring Boot app
Solution
Step 1: Understand the role of Dockerfile
A Dockerfile contains instructions to build a Docker image that packages the app and its environment.
Step 2: Identify its use in Spring Boot
For Spring Boot, it defines how to copy the app jar, expose ports, and run the app in a container.
Final Answer:
To define how to package and run the Spring Boot app inside a container -> Option B
Quick Check:
Dockerfile = Package & run app [OK]
Hint: Dockerfile builds container image for your app [OK]
Common Mistakes:
Confusing Dockerfile with source code files
Thinking Dockerfile configures app logic
Assuming Dockerfile is for testing only
2. Which of the following is the correct syntax to copy the Spring Boot jar file into the Docker image in a Dockerfile?
easy
A. COPY /app.jar target/app.jar
B. MOVE target/app.jar /app.jar
C. ADD target/app.jar
D. COPY target/app.jar /app.jar
Solution
Step 1: Understand COPY command syntax
The COPY command syntax is: COPY source_path destination_path inside the image.
Step 2: Apply to Spring Boot jar
We copy the built jar from local 'target/app.jar' to '/app.jar' inside the image.