# Add a Dockerfile to a Gradle Spring Boot App

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

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

## What the agent does

The agent reads the build before it writes the image, because a Dockerfile for a Gradle project is the build spelled out in stages. In a project like gothinkster/spring-boot-realworld-example-app, `build.gradle` applies `org.springframework.boot` 2.6.3, `io.spring.dependency-management` 1.0.11.RELEASE, `java`, the DGS codegen plugin 5.0.6 and Spotless 6.2.1, sets source and target compatibility to 11, and depends on the web, validation, HATEOAS and security starters, MyBatis 2.2.2, the DGS GraphQL starter 4.9.21, Flyway, jjwt 0.11.2, Joda-Time, `sqlite-jdbc` 3.36.0.3 and Lombok. `generateJava` reads schemas from `src/main/resources/schema`, `clean` deletes `./dev.db`, and `gradle/wrapper/gradle-wrapper.properties` pins Gradle 7.4. There is no `settings.gradle`, no `Dockerfile`, no `.dockerignore` and no Compose file; the README's Docker section says to run `./gradlew bootBuildImage --imageName spring-boot-realworld-example-app` and then `docker run -p 8081:8080`.

Two things in the repository decide the shape of the image. First, with no `settings.gradle`, Gradle names the project after the checkout directory, so the jar under `build/libs/` is called whatever the folder was called; the agent adds `settings.gradle` with `rootProject.name = 'realworld'` so the build is stable wherever it is cloned. Second, `src/main/resources/application.properties` points `spring.datasource.url` at `jdbc:sqlite:dev.db`, a file relative to the working directory, and keeps `jwt.secret` in the same file; Flyway creates the schema from `src/main/resources/db/migration/V1__create_tables.sql` on first start. The image therefore needs a writable data directory and a way to override both properties, which Spring's relaxed binding gives for free: `SPRING_DATASOURCE_URL` and `JWT_SECRET` in the environment.

The Dockerfile has two stages. The build stage starts from `eclipse-temurin:11-jdk-jammy`, copies `gradlew`, `gradle/`, `build.gradle` and `settings.gradle` first and runs `./gradlew --no-daemon dependencies` so the dependency download is cached as its own layer, then copies `src/` and runs `./gradlew --no-daemon bootJar -x test`, which also runs the DGS code generation, and finally `java -Djarmode=layertools -jar build/libs/realworld-0.0.1-SNAPSHOT.jar extract`, which Spring Boot 2.6 supports because its jars are layered by default. The runtime stage starts from `eclipse-temurin:11-jre-jammy`, creates a non-root user, copies the `dependencies/`, `spring-boot-loader/`, `snapshot-dependencies/` and `application/` layers in that order so a code change invalidates only the last one, sets `WORKDIR /app`, `VOLUME /data`, `SPRING_DATASOURCE_URL=jdbc:sqlite:/data/realworld.db` as a default, `EXPOSE 8080` and `ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]`. A `.dockerignore` lists `.gradle`, `build`, `*.db`, `.git` and the IDE directories from `.gitignore`. Tests are skipped in the image build because `.github/workflows/gradle.yml` already runs `./gradlew clean test` on every push and pull request.

The sandbox has no Docker, so the agent installs it first with `sudo apt-get install -y docker.io` and starts `dockerd`, then runs `docker build -t realworld .`, `docker run --rm -p 8080:8080 -e JWT_SECRET=... realworld` and `curl localhost:8080/tags`, the check the README itself suggests, and reads the Flyway migration line from the container log. It adds a `docker` job to `gradle.yml` that builds the image on pull requests without pushing it, and it keeps the README's `bootBuildImage` instructions, adding a paragraph on when to prefer each: the buildpack image needs no Dockerfile, the Dockerfile needs no builder image download and is what most registries and platforms expect. It runs `./gradlew spotlessJavaCheck` and `./gradlew test` once more to show the new `settings.gradle` changed nothing else.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add a Dockerfile to this Gradle Spring Boot app. Read build.gradle, gradle/wrapper/gradle-wrapper.properties, src/main/resources/application.properties, src/main/resources/db/migration, .gitignore, .github/workflows/gradle.yml and the README's Docker section first. Add settings.gradle with a fixed rootProject.name so the jar name does not depend on the checkout directory. Write a multi-stage Dockerfile: a JDK 11 build stage that caches the dependency download in its own layer, builds the boot jar without tests and extracts it with layertools; a JRE 11 runtime stage with a non-root user, the four layers copied in dependency order, a /data volume with the SQLite URL defaulting there, port 8080 exposed and JarLauncher as the entrypoint. Add a .dockerignore. Install Docker in the sandbox, build and run the image, curl /tags, and add a CI job that builds the image on pull requests without pushing. Keep the bootBuildImage instructions in the README and explain when to use each. Run the Gradle test and Spotless check, and open a pull request with the output.",
  "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

The pull request adds `Dockerfile`, `.dockerignore` and `settings.gradle`, and changes `.github/workflows/gradle.yml` and `README.md`. Its description pastes the `docker build` output with the cached dependency layer visible on a second build, the `docker image ls` line for the image, the container log through the Flyway migration and the Tomcat start, and the `curl localhost:8080/tags` response.

A reviewer sees that the image contains a JRE and the extracted layers rather than a JDK and a source tree, that the database lives on a volume, and that the JWT secret is read from the environment instead of being baked in. The `Java CI` workflow runs the tests on the pull request, and the new `docker` job proves the Dockerfile builds on a clean runner.

Before merging, a reviewer chooses the registry and tag scheme for the `docker` job if they want it to push, decides whether SQLite on a volume is what production should run or whether `SPRING_DATASOURCE_URL` should point at a server, and sets a real `JWT_SECRET` outside the repository. The application code and the Gradle build logic are unchanged.

## Run it on every Spring Boot service built with Gradle but shipped without an image

A platform team with several Gradle services sends this body once per repository to `POST /o/{org}/sessions`, each with its own 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 rather than opening a second pull request. Each run reads its own `build.gradle`, so a Kotlin DSL project gets `settings.gradle.kts`, a Java 17 project gets the 17 Temurin tags, and a multi-module build gets the module that applies the Spring Boot plugin as the image's subject.

After that, an automation on the Project with a `github` trigger on `pull_request.opened` and the prompt "if this pull request changes build.gradle, the Gradle wrapper or application.properties, check that the Dockerfile's base image, cached layer and environment defaults still match and comment with the line to change" keeps the image in step with the build. The trigger adds the pull request after the prompt; only work by an owner, member or collaborator fires it.

## Why run it on Rush?

A Dockerfile that is never built is a guess. The agent installs Docker in its own sandbox, builds the image twice to show which layers are cached, runs the container with the real Flyway migration and the real HTTP check from the README, and keeps the logs, so the review is of an image that started rather than of a file that reads well.

Every command stays with the run, from `apt-get` to `curl`, and the pull request quotes them. Several Gradle services can be containerised at once, each in its own sandbox with its own build log and pull request.

## Frequently asked questions

### Why not just use bootBuildImage?

It still works, and the README keeps it. It needs a Docker daemon reachable from Gradle and downloads a buildpack builder image on first use, and it gives you less control over the base image, the user and the volume. The Dockerfile is what most CI systems and registries expect and what a reviewer can read line by line. The pull request explains when each fits.

### Why add settings.gradle?

Without it Gradle takes the project name from the directory, so the jar is `spring-boot-realworld-example-app-0.0.1-SNAPSHOT.jar` in one clone and something else in another, and a `COPY` or `extract` line in the Dockerfile cannot name it. `rootProject.name` fixes the name, and the agent reruns the tests and Spotless to show nothing else changed.

### Where does the SQLite database live in the container?

On the `/data` volume, with `SPRING_DATASOURCE_URL` defaulting to `jdbc:sqlite:/data/realworld.db`. Flyway creates the tables there on first start. Pointing the variable at a PostgreSQL or MySQL URL and adding that driver is the change for a shared database, and the pull request names it.

### Why are tests skipped in the image build?

Because `gradle.yml` already runs `./gradlew clean test` on every push and pull request, and running the same suite inside `docker build` would double the build time without adding a check. The `docker` job builds the image on the same pull request, so a reviewer gets both results.

### Is the JWT secret in the image?

The value in `application.properties` still ships inside the jar, as it does today, but the runtime reads `JWT_SECRET` from the environment first through Spring's relaxed binding, and the `docker run` the agent shows passes one. The pull request recommends removing the committed value in a follow-up and says so plainly.

## More Gradle jobs

- [Add Error Prone to a Gradle Build](https://getrush.ai/run/add-errorprone/gradle/)
- [Add JaCoCo Coverage to a Gradle Spring Boot Project](https://getrush.ai/run/add-jacoco-coverage/gradle/)
- [Add OWASP Dependency-Check to a Gradle Build](https://getrush.ai/run/add-owasp-dependency-check/gradle/)

## The same job on other stacks

- [Add a Dockerfile to a Leptos Axum App](https://getrush.ai/run/add-dockerfile/leptos/)
- [Write a Dockerfile for a Litestar App](https://getrush.ai/run/add-dockerfile/litestar/)
- [Write a Dockerfile for a Maven Spring Boot App](https://getrush.ai/run/add-dockerfile/maven/)

## 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/)
