docker build runs my Spring Boot app instead of finishing the image
Fix it fast
Most common cause, 1 of 3A RUN line launches the server, so that build step never ends
Delete the RUN line that starts the jar and put the same command in an exec-form ENTRYPOINT.
# remove: RUN java -jar app.jar
COPY target/*.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]Short answer
When the Spring Boot banner shows up in docker build output and no image appears, a build step is running the application. RUN executes at build time, so RUN java -jar never returns; mvn package also boots the context in @SpringBootTest tests; and an AOT or CDS training run needs -Dspring.context.exit=onRefresh. Start the jar from ENTRYPOINT instead.
Which cause do you have?
Rebuild with plain progress output, which prints each step's own output under its step number. The step that was active when the banner appeared names the cause.
docker build --progress=plain --no-cache -t my-app . 2>&1 | tee build.log
grep -n -E "RUN|Started " build.logThen find the line you see in this list and open its fix:
- 1A RUN line launches the server, so that build step never ends
The Started line sits under a step such as RUN java -jar app.jar or RUN mvn spring-boot:run, and the step timer keeps counting - 2mvn package runs @SpringBootTest tests that boot the whole app
The banner appears under the RUN mvn package (or ./gradlew build) step, usually followed by a datasource or broker connection error and a failed step - 3An AOT cache or CDS training run has no exit flag
The step that hangs contains -XX:AOTCacheOutput (Java 25) or -XX:ArchiveClassesAtExit
What does the error look like?
Started DemoApplication in 4.2 seconds (process running for 4.9)docker build keeps running after the Spring Boot banner and never prints the image IDdocker image ls shows no new image after the build is stopped with Ctrl+C
Cause 1 of 3
A RUN line launches the server, so that build step never ends
You seeThe Started line sits under a step such as RUN java -jar app.jar or RUN mvn spring-boot:run, and the step timer keeps counting
Check
A Spring Boot app that runs inside docker build forever is almost always started by a RUN instruction. Docker runs a RUN command while building and commits its layer only after the command exits, and a web server does not exit. CMD and ENTRYPOINT behave differently: they record what a container should run and execute nothing during the build.
grep -n -E '^(RUN|CMD|ENTRYPOINT)' DockerfileFix
Delete the RUN line that starts the jar and put the same command in an exec-form ENTRYPOINT. The jar then starts only when someone runs a container from the image, and in exec form java is PID 1, so docker stop's SIGTERM reaches it.
# remove: RUN java -jar app.jar
COPY target/*.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]Verify
The build prints an image ID, the image is listed, and the app boots only when the container starts.
docker build -t my-app .
docker image ls my-app
docker run --rm -p 8080:8080 my-appConfirmed by docs.docker.com
Cause 2 of 3
mvn package runs @SpringBootTest tests that boot the whole app
You seeThe banner appears under the RUN mvn package (or ./gradlew build) step, usually followed by a datasource or broker connection error and a failed step
Check
If the app boots during the Maven step of docker build, the tests are starting it. @SpringBootTest builds the application context through SpringApplication, so beans that connect at start-up reach out for their database or broker. The database and broker your tests expect are normally not reachable from a build step, the tests fail, the step exits non-zero and no image is written.
grep -rl '@SpringBootTest' src/testFix
Skip the test run inside the image build and run the tests in CI, where the services exist. -DskipTests still compiles the test sources; -Dmaven.test.skip=true skips compiling them as well.
# build stage
RUN mvn -B package -DskipTestsVerify
The Maven step ends without a banner and the image appears in the list.
docker build --progress=plain -t my-app .
docker image ls my-appConfirmed by maven.apache.org
Cause 3 of 3
An AOT cache or CDS training run has no exit flag
You seeThe step that hangs contains -XX:AOTCacheOutput (Java 25) or -XX:ArchiveClassesAtExit
Check
Spring Boot's layered Dockerfile examples start the app once during the build to record an AOT cache or a CDS archive. That training run depends on -Dspring.context.exit=onRefresh, which ends the process as soon as the application context has refreshed. Drop the flag and the app keeps serving, so the step never returns.
grep -n -E 'AOTCacheOutput|ArchiveClassesAtExit' DockerfileFix
Add the exit flag to the training run. At refresh time every non-lazy singleton has already been created, so beans that open connections on start-up still need their service, or a profile that keeps them lazy, during this step.
RUN java -XX:AOTCacheOutput=app.aot -Dspring.context.exit=onRefresh -jar application.jar
ENTRYPOINT ["java", "-XX:AOTCache=app.aot", "-jar", "application.jar"]Verify
The training step finishes on its own and the cache file is inside the image.
docker run --rm --entrypoint ls my-app -l app.aotConfirmed by docs.spring.io
How do I stop it coming back?
- Keep a multi-stage Dockerfile whose runtime stage ends in one exec-form ENTRYPOINT, and never call the server with RUN outside a training run that carries -Dspring.context.exit=onRefresh.
- Run @SpringBootTest suites in the CI job that has the database service attached, and keep -DskipTests on the Maven step inside the Dockerfile.
- Give the CI image-build job a time limit, so a step that boots a server fails within minutes instead of holding a runner.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: Dispatch Pro, TaxiDex.