A Dockerfile helps package your Spring Boot app into a container. This makes it easy to run your app anywhere without setup hassles.
Dockerfile for Spring Boot in Spring Boot
FROM openjdk:17-jdk-slim ARG JAR_FILE=target/*.jar COPY ${JAR_FILE} app.jar ENTRYPOINT ["java", "-jar", "/app.jar"]
FROM sets the base image with Java installed.
COPY moves your built app jar into the container.
ENTRYPOINT runs your app when the container starts.
FROM openjdk:17-jdk-slim COPY target/myapp.jar app.jar ENTRYPOINT ["java", "-jar", "/app.jar"]
FROM openjdk:17-jdk-slim ARG JAR_FILE=target/*.jar COPY ${JAR_FILE} app.jar ENTRYPOINT ["java", "-jar", "/app.jar"]
EXPOSE to tell Docker the app listens on port 8080.FROM openjdk:17-jdk-slim COPY target/myapp.jar app.jar EXPOSE 8080 ENTRYPOINT ["java", "-jar", "/app.jar"]
This Dockerfile creates a container image for your Spring Boot app. It uses Java 17, copies your built jar, exposes port 8080, and runs the app.
FROM openjdk:17-jdk-slim ARG JAR_FILE=target/*.jar COPY ${JAR_FILE} app.jar EXPOSE 8080 ENTRYPOINT ["java", "-jar", "/app.jar"]
Make sure your Spring Boot app is built (e.g., with ./mvnw package) before building the Docker image.
The EXPOSE instruction does not publish the port but documents it; use -p flag when running the container to map ports.
Use docker build -t myapp . to build and docker run -p 8080:8080 myapp to run your container.
A Dockerfile packages your Spring Boot app with Java into a container.
Use FROM, COPY, EXPOSE, and ENTRYPOINT to define the container.
Build your app first, then build and run the Docker image to run your app anywhere.
Practice
Dockerfile in a Spring Boot project?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 BQuick Check:
Dockerfile = Package & run app [OK]
- Confusing Dockerfile with source code files
- Thinking Dockerfile configures app logic
- Assuming Dockerfile is for testing only
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.Final Answer:
COPY target/app.jar /app.jar -> Option DQuick Check:
COPY source destination [OK]
- Using MOVE instead of COPY
- Missing destination path in ADD
- Reversing source and destination paths
FROM openjdk:17-jdk-slim COPY target/app.jar /app.jar EXPOSE 8080 ENTRYPOINT ["java", "-jar", "/app.jar"]
What happens when you run the built Docker image?
Solution
Step 1: Analyze Dockerfile commands
FROM sets base image with Java 17. COPY adds app.jar. EXPOSE 8080 declares port. ENTRYPOINT runs the jar.Step 2: Understand container behavior
When run, container starts Java app listening on port 8080 as declared.Final Answer:
The container runs the Spring Boot app on port 8080 -> Option CQuick Check:
ENTRYPOINT runs app, EXPOSE 8080 [OK]
- Thinking EXPOSE runs the app
- Assuming default port 80 without config
- Ignoring ENTRYPOINT command
FROM openjdk:17-jdk-slim COPY app.jar /app.jar EXPOSE 8080 ENTRYPOINT ["java", "-jar", "/app.jar"]
But when running the container, it fails with 'file not found'. What is the likely cause?
Solution
Step 1: Check COPY source path
COPY expects a local file named 'app.jar' but likely the jar is named differently or in a different folder.Step 2: Understand error cause
If the jar is missing or misnamed, container cannot find '/app.jar' to run, causing failure.Final Answer:
The jar file is not named 'app.jar' in the build folder -> Option AQuick Check:
COPY source must match actual jar name [OK]
- Assuming EXPOSE affects file copying
- Miswriting ENTRYPOINT array syntax
- Blaming base image for missing file
Solution
Step 1: Compare base images
openjdk:17-jre-slim has only Java runtime, smaller than jdk images which include compilers.Step 2: Evaluate options for size and startup
Using jre-slim reduces image size and speeds startup. Alpine lacks Java by default, causing errors.Final Answer:
FROM openjdk:17-jre-slim COPY target/app.jar /app.jar EXPOSE 8080 ENTRYPOINT ["java", "-jar", "/app.jar"] -> Option AQuick Check:
Use JRE slim for smaller, faster images [OK]
- Using full JDK image unnecessarily
- Choosing Alpine without Java setup
- Ignoring image size impact
Ready to prove it?
Pass the test and this pattern moves unknown
