Bird
Raised Fist0
Spring Bootframework~20 mins

Dockerfile for Spring Boot in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Spring Boot Docker Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of Docker build command
You have this Dockerfile for a Spring Boot app:
FROM openjdk:17-jdk-slim
COPY target/app.jar app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]

What will be the output of docker build -t springboot-app . if the target/app.jar file is missing?
ASuccessfully builds the image with tag springboot-app
BBuild completes but image is empty
CWarning about missing file but image builds
DError: COPY failed: stat target/app.jar: no such file or directory
Attempts:
2 left
💡 Hint
Think about what happens if the file to copy does not exist during build.
❓ Configuration
intermediate
2:00remaining
Correct Dockerfile for multi-stage build
Which Dockerfile correctly uses a multi-stage build to compile a Spring Boot app with Maven and produce a minimal final image?
A
FROM maven:3.8.6-openjdk-17
COPY . /app
WORKDIR /app
RUN mvn clean package
ENTRYPOINT ["java", "-jar", "target/app.jar"]
B
FROM maven:3.8.6-openjdk-17 AS build
WORKDIR /app
COPY pom.xml .
COPY src ./src
RUN mvn clean package -DskipTests
FROM openjdk:17-jdk-slim
COPY --from=build /app/target/app.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]
C
FROM openjdk:17-jdk-slim
COPY target/app.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]
D
FROM openjdk:17-jdk-slim
RUN mvn clean package
COPY target/app.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]
Attempts:
2 left
💡 Hint
Multi-stage builds separate build environment and runtime environment.
❓ Troubleshoot
advanced
2:00remaining
Why does the Spring Boot app fail to start in Docker?
You built a Docker image for your Spring Boot app but when running the container, it exits immediately with no logs. The Dockerfile is:
FROM openjdk:17-jdk-slim
COPY target/app.jar app.jar
ENTRYPOINT java -jar app.jar

What is the most likely cause?
AThe base image openjdk:17-jdk-slim does not support Spring Boot
BThe app.jar file is missing in the image
CENTRYPOINT should be an array of strings, not a single string
DThe container needs environment variables to start
Attempts:
2 left
💡 Hint
How Docker interprets ENTRYPOINT differs between string and array syntax.
🔀 Workflow
advanced
2:00remaining
Order of steps to build and run Spring Boot Docker container
Put these steps in the correct order to build and run a Spring Boot app Docker container:
1. Run the container with docker run
2. Write the Dockerfile
3. Build the jar with Maven
4. Build the Docker image with docker build
A1,2,3,4
B2,1,3,4
C1,3,2,4
D2,3,1,4
Attempts:
2 left
💡 Hint
Think about what you need before building the image and running the container.
✅ Best Practice
expert
2:00remaining
Best practice for reducing Spring Boot Docker image size
Which option is the best practice to reduce the size of a Spring Boot Docker image?
AUse a multi-stage build with Maven and copy only the jar to a slim OpenJDK base image
BUse the full OpenJDK image and copy all source code into the image
CBuild the jar inside the container every time it runs
DUse a base image with JRE instead of JDK and copy the jar
Attempts:
2 left
💡 Hint
Think about separating build and runtime environments.

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

  1. Step 1: Understand the role of Dockerfile

    A Dockerfile contains instructions to build a Docker image that packages the app and its environment.
  2. 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.
  3. Final Answer:

    To define how to package and run the Spring Boot app inside a container -> Option B
  4. 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

  1. Step 1: Understand COPY command syntax

    The COPY command syntax is: COPY source_path destination_path inside the image.
  2. Step 2: Apply to Spring Boot jar

    We copy the built jar from local 'target/app.jar' to '/app.jar' inside the image.
  3. Final Answer:

    COPY target/app.jar /app.jar -> Option D
  4. Quick Check:

    COPY source destination [OK]
Hint: COPY source then destination path [OK]
Common Mistakes:
  • Using MOVE instead of COPY
  • Missing destination path in ADD
  • Reversing source and destination paths
3. Given this Dockerfile snippet:
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?
medium
A. The container runs but does not start the app
B. The container fails because EXPOSE is missing
C. The container runs the Spring Boot app on port 8080
D. The container runs the app but on port 80

Solution

  1. Step 1: Analyze Dockerfile commands

    FROM sets base image with Java 17. COPY adds app.jar. EXPOSE 8080 declares port. ENTRYPOINT runs the jar.
  2. Step 2: Understand container behavior

    When run, container starts Java app listening on port 8080 as declared.
  3. Final Answer:

    The container runs the Spring Boot app on port 8080 -> Option C
  4. Quick Check:

    ENTRYPOINT runs app, EXPOSE 8080 [OK]
Hint: ENTRYPOINT runs app; EXPOSE shows port [OK]
Common Mistakes:
  • Thinking EXPOSE runs the app
  • Assuming default port 80 without config
  • Ignoring ENTRYPOINT command
4. You wrote this Dockerfile:
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?
medium
A. The jar file is not named 'app.jar' in the build folder
B. EXPOSE 8080 is missing
C. ENTRYPOINT syntax is incorrect
D. Base image openjdk:17-jdk-slim does not support Java

Solution

  1. 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.
  2. Step 2: Understand error cause

    If the jar is missing or misnamed, container cannot find '/app.jar' to run, causing failure.
  3. Final Answer:

    The jar file is not named 'app.jar' in the build folder -> Option A
  4. Quick Check:

    COPY source must match actual jar name [OK]
Hint: Check jar filename matches COPY source [OK]
Common Mistakes:
  • Assuming EXPOSE affects file copying
  • Miswriting ENTRYPOINT array syntax
  • Blaming base image for missing file
5. You want to optimize your Spring Boot Dockerfile to use a smaller image and faster startup. Which Dockerfile snippet achieves this best?
hard
A. FROM openjdk:17-jre-slim COPY target/app.jar /app.jar EXPOSE 8080 ENTRYPOINT ["java", "-jar", "/app.jar"]
B. FROM alpine COPY target/app.jar /app.jar EXPOSE 8080 ENTRYPOINT ["java", "-jar", "/app.jar"]
C. FROM openjdk:17-jdk-slim COPY target/app.jar /app.jar EXPOSE 8080 ENTRYPOINT ["java", "-jar", "/app.jar"]
D. FROM openjdk:17-jdk COPY target/app.jar /app.jar EXPOSE 8080 ENTRYPOINT ["java", "-jar", "/app.jar"]

Solution

  1. Step 1: Compare base images

    openjdk:17-jre-slim has only Java runtime, smaller than jdk images which include compilers.
  2. Step 2: Evaluate options for size and startup

    Using jre-slim reduces image size and speeds startup. Alpine lacks Java by default, causing errors.
  3. Final Answer:

    FROM openjdk:17-jre-slim COPY target/app.jar /app.jar EXPOSE 8080 ENTRYPOINT ["java", "-jar", "/app.jar"] -> Option A
  4. Quick Check:

    Use JRE slim for smaller, faster images [OK]
Hint: Use JRE slim base image for smaller Docker images [OK]
Common Mistakes:
  • Using full JDK image unnecessarily
  • Choosing Alpine without Java setup
  • Ignoring image size impact
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard