Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Servers, nginx and Docker

docker build runs my Spring Boot app instead of finishing the image

Your error

Started DemoApplication in 4.2 seconds (process running for 4.9)2 more lines it can show

Fix it fast

Most common cause, 1 of 3

A 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.

Fix
# remove:  RUN java -jar app.jar
COPY target/*.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]
Did this fix it?
Full steps for this cause: check, fix, verify

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.

  • Applies to Docker Engine 28-29 with BuildKit, Docker Desktop 4.x, Spring Boot 4.x, Maven 3.9 and 3.10, JDK 21 and 25
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
docker build --progress=plain --no-cache -t my-app . 2>&1 | tee build.log
grep -n -E "RUN|Started " build.log

Then find the line you see in this list and open its fix:

  1. 1A RUN line launches the server, so that build step never endsThe 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
  2. 2mvn package runs @SpringBootTest tests that boot the whole appThe banner appears under the RUN mvn package (or ./gradlew build) step, usually followed by a datasource or broker connection error and a failed step
  3. 3An AOT cache or CDS training run has no exit flagThe 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 ID
  • docker 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.

Check
grep -n -E '^(RUN|CMD|ENTRYPOINT)' Dockerfile

Fix

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.

Fix
# 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.

Verify
docker build -t my-app .
docker image ls my-app
docker run --rm -p 8080:8080 my-app

Confirmed by docs.docker.com

Did this fix it?

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.

Check
grep -rl '@SpringBootTest' src/test

Fix

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.

Fix
# build stage
RUN mvn -B package -DskipTests

Verify

The Maven step ends without a banner and the image appears in the list.

Verify
docker build --progress=plain -t my-app .
docker image ls my-app

Confirmed by maven.apache.org

Did this fix it?

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.

Check
grep -n -E 'AOTCacheOutput|ArchiveClassesAtExit' Dockerfile

Fix

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.

Fix
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.

Verify
docker run --rm --entrypoint ls my-app -l app.aot

Confirmed by docs.spring.io

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.