Docker for a Spring Boot App: Run the Tests in the Image Build, or Skip Them on Purpose
The blog's Dockerfile runs mvnw package with -DskipTests=${SKIP_TESTS}, false by default, so every docker build runs all 53 tests and a red one means no image. --build-arg SKIP_TESTS=true cuts the package step from 41 to 23 seconds, which is only safe after CI has run the tests.
What you will learn
- Run the test suite fast with mvnw test before Docker
- Read a two-stage Dockerfile whose package step runs the tests
- See a red test stop docker build so no image is made
- Use a SKIP_TESTS build arg only when CI has already run the tests
Before you start
- Unit Tests With AI for a Spring Boot Service
- Docker images and containers
The blog has 53 tests, and today it goes into a Docker image. So where do the tests run? On your machine, and inside the image build too, unless you turn them off on purpose.
Taste it, or check it at the door
In your own kitchen you cook, taste and fix fast: that's mvnw test. On a bakery's packing line a batch is checked before the box is sealed: that's docker build, and the box is the image. A bad batch gets no box. You can skip the check and seal sooner, but then nobody tasted it.
The fast loop, without Docker
./mvnw test -Dtest=CommentServiceTest
./mvnw test
One class: 5 tests, 0 failures, about 9.5 seconds. Everything: 53 tests, 0 failures, about 22 seconds. Just Maven and the JDK on your machine. Change the code, run the tests, read the result.
The Dockerfile
# Stage 1: build the jar with the project's own Maven wrapper - and run the tests on the way
FROM eclipse-temurin:25-jdk AS build
WORKDIR /app
COPY mvnw pom.xml ./
COPY .mvn .mvn
RUN sed -i 's/\r$//' mvnw && chmod +x mvnw # LF endings + executable, even after a Windows checkout
RUN ./mvnw -B -q dependency:go-offline # its own layer: cached until pom.xml changes
COPY src ./src
ARG SKIP_TESTS=false
RUN ./mvnw -B package -DskipTests=${SKIP_TESTS}
# Stage 2: only a Java runtime and the jar
FROM eclipse-temurin:25-jre
WORKDIR /app
COPY --from=build /app/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]
Stage one starts from a JDK, which has everything needed to build. The wrapper and the pom are copied first and the dependencies downloaded, so Docker caches that layer until pom.xml changes. Then the code comes in. ARG SKIP_TESTS=false is the switch, and package runs the tests before it builds the jar. Stage two is a small Java runtime that gets only the jar, as app.jar, and starts it on port 8080. The JDK and the tests stay behind in stage one.
target/
.idea/
*.iml
uploads/
desktop.ini
.dockerignore keeps target, the IDE files and the uploaded photos out of the build context, so Docker receives only about 150 KB.
docker build runs the tests
docker build -t studyeasy-blog .
In the build output, the dependency step took about 28.5 seconds. Then the package step runs mvnw package with skip tests false, and you see the tests start: CommentServiceTest 5 tests, PostServiceTest 1, and so on, ending with 53 tests run, 0 failures, build success. That step took about 41 seconds. Only then does Docker name the image.
A red test stops the image
In a scratch copy, one expectation was changed. The build runs again, the test fails at line 68 (expected "Thanks for this", but was "Thanks for this!"), Maven reports 53 tests, 1 failure, build failure, and Docker stops with exit code 1. There's no naming line, so there's no image and nothing to ship.
SKIP_TESTS=true: faster, unchecked
docker build --build-arg SKIP_TESTS=true -t studyeasy-blog .
Maven says the tests are skipped and the build succeeds. The package step drops from about 41.3 to 23 seconds. The catch: nothing checked the code, so a red test would still produce an image.
The image starts the blog
Either way you get an image of 592 MB. docker run --rm -p 8080:8080 studyeasy-blog (the README command; the lecture maps host port 18080 instead) starts the blog in about 9.5 seconds, and a GET on the home page answers 200 with the title "Home | StudyEasy Blog". A compose file builds from the same Dockerfile and starts Mailpit next to it for the reset mails:
services:
blog:
build: .
ports:
- "8080:8080"
environment:
SPRING_MAIL_HOST: mailpit # the reset mail goes to the mailpit container
BLOG_BASE_URL: http://localhost:8080
depends_on:
- mailpit
mailpit:
image: axllent/mailpit
ports:
- "8025:8025" # the inbox: http://localhost:8025
For images, layers and volumes from the start, see Docker essentials.
Which one should you use?
The usual pattern: on your machine, mvnw test is the fast loop. CI, a server that runs the same mvnw test on every push, comes next. Only after that does the image build run with SKIP_TESTS=true, because the code is already tested. If a test is red it stops at CI and no image is built. Run them in the build by default, and skip them only on purpose.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Unit Tests With AI for a Spring Boot Service: Mockito Mocks, Given-When-Then and a Green Run
CommentServiceTest runs the real CommentService with three Mockito mocks for its repositories: no Spring, no database, five tests in a fraction of a second. An AI drafts it from a short prompt; we read each test, run it, and learn to read the red message when an expectation is wrong.
The Test Pyramid in Spring Boot: Unit, Slice and Whole-App Tests
One small ticket desk, three kinds of test: four plain unit tests in 20 ms, two @WebMvcTest slice tests in 4.4 s, one @SpringBootTest flow in 711 ms. Where each rule belongs, and what you should never test at all.
Mockito and Test Doubles: Stubs, Mocks and Fakes for One Java Class
Test TicketService.close on its own: a stub repository that answers findById, a mock notifier that records send, verify to check both calls, and a HashMap fake that really stores the closed ticket.
Spring Boot Test Slices: @WebMvcTest with MockMvc and @DataJpaTest
Start only the layer you test. Three MockMvc tests for a ticket controller (200 with JSON, 404, a JSON list), the 'no qualifying bean' failure when the mock is missing, two @DataJpaTest tests on H2, and bean counts of 124, 80 and 224.
