0
0
Spring Bootframework~10 mins

Dockerfile for Spring Boot in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Dockerfile for Spring Boot
Start: Write Dockerfile
Choose base image
Copy jar file into image
Set entrypoint to run jar
Build Docker image
Run container
Spring Boot app runs inside container
This flow shows how a Dockerfile is created and used to build and run a Spring Boot application inside a container.
Execution Sample
Spring Boot
FROM openjdk:17-jdk-slim
COPY target/app.jar app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]
This Dockerfile uses an OpenJDK base image, copies the Spring Boot jar, and sets the command to run it.
Execution Table
StepDockerfile InstructionActionResult
1FROM openjdk:17-jdk-slimSelect base image with Java 17Base image ready with Java environment
2COPY target/app.jar app.jarCopy Spring Boot jar into imageapp.jar available inside image
3ENTRYPOINT ["java", "-jar", "/app.jar"]Set container start commandContainer will run the jar on start
4docker build -t springboot-app .Build Docker image from DockerfileImage 'springboot-app' created
5docker run -p 8080:8080 springboot-appRun container exposing port 8080Spring Boot app runs and listens on port 8080
💡 Container runs until stopped; Spring Boot app serves requests on port 8080
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
Docker ImageNoneopenjdk:17-jdk-slimopenjdk + app.jarConfigured with ENTRYPOINTBuilt as springboot-appRunning container with app
Key Moments - 3 Insights
Why do we use 'COPY target/app.jar app.jar' instead of copying the whole project?
Because the Docker image only needs the compiled jar file to run the app, not the source code. This is shown in execution_table step 2.
What does ENTRYPOINT do in the Dockerfile?
ENTRYPOINT sets the command that runs when the container starts, here it runs the Spring Boot jar. See execution_table step 3.
Why do we expose port 8080 when running the container?
Spring Boot listens on port 8080 by default, so exposing it allows access from outside the container. See execution_table step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the base image used in step 1?
Aopenjdk:8-jdk
Bubuntu:latest
Copenjdk:17-jdk-slim
Dspringboot:latest
💡 Hint
Check the Dockerfile Instruction column in step 1 of the execution_table.
At which step is the Spring Boot jar copied into the Docker image?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look for the COPY instruction in the execution_table.
If we change the ENTRYPOINT to run a different jar, which step would change?
AStep 2
BStep 3
CStep 1
DStep 5
💡 Hint
ENTRYPOINT is set in step 3 according to the execution_table.
Concept Snapshot
Dockerfile for Spring Boot:
FROM openjdk:17-jdk-slim
COPY target/app.jar app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]
Build image with 'docker build -t name .'
Run container with 'docker run -p 8080:8080 name'
Spring Boot app runs inside container on port 8080
Full Transcript
This visual execution shows how to create a Dockerfile for a Spring Boot application. First, we select a base image with Java 17. Then we copy the compiled Spring Boot jar file into the image. Next, we set the ENTRYPOINT to run the jar when the container starts. After writing the Dockerfile, we build the Docker image and then run a container exposing port 8080. The Spring Boot app runs inside the container and listens on port 8080. Key points include copying only the jar file, setting the correct ENTRYPOINT, and exposing the right port for access.