# Write a Dockerfile for a Maven Spring Boot App

Yes. An agent writes a multi-stage Dockerfile for your Maven Spring Boot app from the layered jar extract, builds and runs it, adds a CI image build, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-dockerfile/maven/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent starts from the build, because a Dockerfile for a Maven project is the build written down twice: once to cache and once to run. In a project like the `complete` module of spring-guides/gs-rest-service, `pom.xml` inherits `spring-boot-starter-parent` 4.0.8, sets `java.version` to 17, depends on `spring-boot-starter-webmvc` and its test starter, and applies `spring-boot-maven-plugin` so `./mvnw package` produces a runnable jar. `.mvn/wrapper/maven-wrapper.properties` pins Maven 3.9.16 with the script-only wrapper, `GreetingController` answers `GET /greeting` with a `Greeting` record, and `GreetingControllerTests` checks it with `RestTestClient` under `@SpringBootTest`. The repository's `.github/workflows/continuous-integration-build.yml` builds every module with Maven and Gradle on JDK 17, and `.gitignore` already lists `target`. There is no Dockerfile and no `.dockerignore`.

The Dockerfile has three stages. The build stage starts from a Temurin 17 JDK image, copies `.mvn`, `mvnw` and `pom.xml` first and runs `./mvnw -B dependency:go-offline`, so the dependency download is a cached layer that survives source changes, then copies `src` and runs `./mvnw -B package`, tests included. The extract stage copies the jar and runs `java -Djarmode=tools -jar application.jar extract --layers --destination extracted`, the `tools` jar mode Spring Boot 4 ships inside every repackaged jar, which splits it into `dependencies`, `spring-boot-loader`, `snapshot-dependencies` and `application`. The runtime stage starts from the matching Temurin 17 JRE image, copies those four directories in that order so a code change invalidates only the last layer, runs as a non-root user, exposes 8080 and ends with `ENTRYPOINT ["java", "-jar", "application.jar"]`. The pull request names the base images and explains the choice of Temurin 17 over the Liberica image in Spring's own reference, since `java.version` is 17.

A `.dockerignore` keeps `target`, `.git` and the IDE files from `.gitignore` out of the build context, which otherwise ships a developer's last build to the daemon on every `docker build`. The agent does not use `spring-boot:build-image` and its buildpacks, because the request was for a Dockerfile, but it names that option in the pull request for a reviewer who prefers no Dockerfile at all.

The sandbox has no Docker, so the agent installs it, starts the daemon, builds the image from the `complete` directory and runs it with the port published. It then calls `curl localhost:8080/greeting?name=Docker` and gets `{"id":1,"content":"Hello, Docker!"}`, the same response `README.adoc` documents, and checks the container's process is not root. It runs `./mvnw --batch-mode clean package` as the CI workflow does, adds a job to that workflow that builds the image on `ubuntu-latest` with `docker build complete`, and leaves `test/run.sh` and the Gradle and Kotlin modules alone.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Write a Dockerfile for this Maven Spring Boot application. Read pom.xml, .mvn/wrapper/maven-wrapper.properties, every file under src, .gitignore and the workflows under .github first. Write a three-stage Dockerfile: a build stage on a Temurin JDK image matching java.version that copies .mvn, mvnw and pom.xml, runs ./mvnw -B dependency:go-offline so dependencies cache, then copies src and runs ./mvnw -B package; an extract stage that runs java -Djarmode=tools -jar application.jar extract --layers --destination extracted; and a runtime stage on the matching Temurin JRE image that copies the dependencies, spring-boot-loader, snapshot-dependencies and application directories in that order, runs as a non-root user, exposes 8080 and starts with java -jar application.jar. Add a .dockerignore that excludes target and .git. Docker is not installed here: install docker.io, start dockerd, build the image, run it with the port published and show a curl of /greeting. Run ./mvnw --batch-mode clean package, add a CI job that builds the image, and open a pull request that names the base images and explains the layer order.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request that adds `Dockerfile` and `.dockerignore` beside `pom.xml` and adds one job to the CI workflow. The description names the two base image tags, lists the four extracted layers in copy order with one sentence on why that order is cheapest to rebuild, shows the `docker build` output and the `curl` response, and states that `./mvnw clean package` still passes with the one test the module has.

The run's record holds the Docker install, the build with its layer cache hits on a second run, the container start and the request, so a reviewer can see the image answered before reading a line of the Dockerfile.

Before merging, a reviewer decides whether to pin the base images to digests, whether to skip tests in the image build and rely on CI for them, and whether to enable the AOT cache training run Spring's reference describes for Java 25 images, which does not apply to a Java 17 runtime. The application code and `pom.xml` are unchanged.

## Run it on every Spring Boot service without an image

A team with several Maven services starts one run per repository from its own server: one `POST /o/{org}/sessions` each, with that repository in `resources` and a `request_id` that is a UUID v5 of the repository's full name, so a retried call returns the run already started instead of a second pull request. Each run reads that service's own `pom.xml` for its Java version and parent, and builds the image that project needs.

To keep the image honest afterwards, an automation on the Project with a `github` trigger on `pull_request.opened` and `pull_request.synchronize` and the prompt "if this pull request changes pom.xml, the Maven wrapper or the Dockerfile, build the image and comment with the result" reads each new pull request. The trigger puts the pull request after the prompt; only work by an owner, member or collaborator fires it, never a fork's.

## Why run it on Rush?

A Spring Boot Dockerfile copied from a blog post is usually a single `COPY target/*.jar` that works, ships the whole jar as one layer, and makes every deploy pull every dependency again. Getting the layered form right means knowing which jar mode this Boot version ships and which directories come out of it, and the only way to be sure is to run the extract. The agent does that in its own sandbox, with Docker installed for that run alone, and shows the layers and the curl in the pull request.

Every command stays with the run, from the Maven build to the container's answer, and the pull request cites them. Doing the same for each Java service a team owns costs no more attention than one, because each run has its own machine and its own record.

## Frequently asked questions

### Why the tools jar mode rather than unzipping the jar?

Spring Boot's repackaged jar carries a layers index and the `spring-boot-jarmode-tools` dependency so that `java -Djarmode=tools -jar app.jar extract --layers` splits it the way the index says, with a small `application.jar` that references the extracted dependencies. Unzipping by hand works but has to reproduce that layout, and the jar mode is what Spring's own reference uses for Boot 4.

### Why run the tests inside the image build?

Because `./mvnw package` runs them by default and the image then proves the same thing CI proves. It makes the build stage slower, and a reviewer who runs tests in CI already can add `-DskipTests` to that one line; the agent leaves them on and says so, since a slow correct build beats a fast one that skips the only check.

### What about the Gradle and Kotlin modules in the same repository?

The run writes the Dockerfile for the Maven module it was pointed at. The Gradle module would need the jar path changed from `target` to `build/libs`, which is a one-line build argument in the same file, and the pull request notes it. Nothing under `initial`, `complete-kotlin` or `test/run.sh` changes.

### Why Temurin and not the image in Spring's documentation?

Spring's reference uses a Liberica JRE image for Java 25 with the AOT cache. This project's `pom.xml` sets `java.version` to 17, so the agent matches that with Temurin 17 JDK and JRE tags, which the pull request names. Switching to Liberica, or to Java 25 with the training run, is a two-line change the reviewer can make deliberately.

## More Maven jobs

- [Add the Maven Enforcer Plugin to a Build](https://getrush.ai/run/add-enforcer-plugin/maven/)
- [Add Error Prone to a Maven Build](https://getrush.ai/run/add-errorprone/maven/)
- [Add JaCoCo Code Coverage to a Maven Project](https://getrush.ai/run/add-jacoco-coverage/maven/)

## The same job on other stacks

- [Add a Dockerfile to a Medusa Backend](https://getrush.ai/run/add-dockerfile/medusa/)
- [Write a Dockerfile for a Reflex App](https://getrush.ai/run/add-dockerfile/reflex/)
- [Add a Dockerfile to a Rocket App](https://getrush.ai/run/add-dockerfile/rocket/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
