> ## Documentation Index
> Fetch the complete documentation index at: https://docs.reliantlabs.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Deploy Spring Boot with Reliant Forge

> Deploy a Spring Boot app with Postgres from its own Dockerfile: Forge adopts it in place, you map the managed database to a JDBC URL, Flyway or Liquibase migrates on startup, and it ships to Reliant hosting.

You will deploy an existing Spring Boot app, with a Dockerfile, to [Reliant hosting](https://docs.reliantlabs.io/hosting/overview.md) with a managed Postgres. Forge reads the Dockerfile and writes its declarations beside it. Spring needs more hand edits than other stacks, because the detector reads the port from the Dockerfile but does not read `pom.xml` or `build.gradle`: you add the database yourself. The detector output here is from Forge `v1.10.1-0.20261123064811-54313eead62f`.

## Before you begin

* Docker, and the app in a git repository with a root `Dockerfile` that builds on its own.
* Forge: `reliant forge` inside Reliant, or `npx -y @reliantlabs/forge@latest` anywhere else.
* A Reliant account with [app hosting billing](https://docs.reliantlabs.io/hosting/overview.md#billing). A JVM doesn't fit the anonymous plan (below).

## What the Dockerfile must do

A deploy runs `docker build` on the Dockerfile alone, so build the jar inside it. A Dockerfile that only does `COPY target/app.jar` fails, because `target/` exists only on your machine:

```dockerfile Dockerfile theme={null}
FROM eclipse-temurin:21-jdk AS build
WORKDIR /src
COPY . .
RUN ./mvnw -q -DskipTests package

FROM eclipse-temurin:21-jre
WORKDIR /app
COPY --from=build /src/target/*.jar app.jar
RUN useradd -r -u 10001 app
USER 10001
EXPOSE 8080
ENV JAVA_TOOL_OPTIONS="-XX:MaxRAMPercentage=75"
CMD ["java", "-jar", "app.jar"]
```

The detector takes the port from `EXPOSE` and sets `PORT=8080` in the container. Spring Boot ignores `PORT` unless you map it: put `server.port=${PORT:8080}` in `application.properties`. Spring binds all interfaces by default. `MaxRAMPercentage` sizes the heap from the container's memory limit instead of the JVM's default quarter, which leaves room for metaspace and threads without being killed.

## Postgres: a JDBC URL

Hosting hands each environment's managed Postgres to the app as a URL in `DATABASE_URL` (`postgres://user:pass@host/db` form). Spring's datasource wants `jdbc:postgresql://host/db` with the user and password separate, and doesn't parse the URL form. The least code is a small `DataSource` bean that reads `DATABASE_URL`:

```java DatabaseConfig.java theme={null}
@Bean
DataSource dataSource(@Value("${DATABASE_URL}") String url) {
    URI u = URI.create(url);
    String[] cred = u.getUserInfo().split(":", 2);
    HikariDataSource ds = new HikariDataSource();
    ds.setJdbcUrl("jdbc:postgresql://" + u.getHost() + ":" + (u.getPort() == -1 ? 5432 : u.getPort()) + u.getPath()
        + (u.getQuery() == null ? "" : "?" + u.getQuery()));
    ds.setUsername(URLDecoder.decode(cred[0], UTF_8));
    ds.setPassword(URLDecoder.decode(cred[1], UTF_8));
    ds.setMaximumPoolSize(5);
    return ds;
}
```

Then declare the database in each hosted env, as the detector does for other stacks:

```python deploy/kcl/prod/main.k (excerpt) theme={null}
_db = forge.ManagedDatabase {name = "orders", runtime = forge.OnHosted {}}

_bundle = forge.Bundle {
    project = "orders"
    control_plane = forge.ControlPlane {}
    workloads = [
        _hosted(wl.api) | {env.DATABASE_URL = forge.DatabaseRef {name = _db.name}}
    ]
    databases = [_db]
    secret_provider = forge.HostedSecrets {}
}
```

The database's name may not be a workload's name (`api`).

## Migrations: on startup

Flyway (`spring-boot-starter-flyway` or `flyway-core`) and Liquibase run on application startup, against the same `DataSource`, before Spring starts serving, so you don't need a separate `migrate` job. With one replica that is safe; with more, both tools take a database lock, so the second replica waits. Keep migrations backward-compatible, because the old release serves while the new one migrates. If you'd rather migrate before the rollout, add a `migrate` job running `java -jar app.jar --spring.main.web-application-type=none` with a profile that exits after Flyway.

**1. Preview and adopt**

```bash terminal theme={null}
reliant forge project new --in-place --kind app --dry-run
```

```text Output (trimmed) theme={null}
Detected:
  - workload api (service): builds Dockerfile, port 8080
Assumed (each is a `# check:` line in deploy/kcl; review them):
  - dev runs api's Dockerfile CMD on the host (java -jar app.jar): change it in deploy/kcl/dev/main.k if the app has a dev server
```

No Postgres is detected for a Maven or Gradle app. Adopt, then add the database as above to `staging/main.k` and `prod/main.k`. For dev, set the command in `deploy/kcl/dev/main.k` to `./mvnw spring-boot:run` and add a local Postgres (`forge.HostInfra {name = "postgres", ...}`; `reliant forge skill load adopt/deploy` shows the dev file).

```bash terminal theme={null}
reliant forge project new --in-place --kind app --name orders
```

**2. Size the workload**

The default hosted size is 250m CPU and 1 GiB of memory. A Spring Boot app with JPA usually wants 512 MiB to 1 GiB and starts slowly at 250m. Declare it in `workloads.k`, on the fixed shape of 4 GiB per vCPU in 125m steps:

```python deploy/kcl/workloads.k (excerpt) theme={null}
resources = fw.Resources {
    cpuRequestMillicores = 500
    memoryRequestBytes = forge.mib(2048)
}
```

**3. Secrets, build, deploy**

```bash terminal theme={null}
openssl rand -hex 32 | reliant forge env secret set --env prod JWT_SECRET
docker build .
reliant forge env render prod
reliant forge lint
reliant forge env deploy prod --yes
```

Deliver each secret as `env.JWT_SECRET = forge.ManagedSecret {name = "JWT_SECRET"}` on the workload. Check `application.properties` for `${JWT_SECRET:changeme}`: the default after the colon is what prod runs with if the secret is missing. Without billing, the deploy is queued and exits **7** with the billing link.

The anonymous plan (`--claimable`) caps the service at 125m CPU and **128 MiB**. Spring Boot's heap, metaspace and code cache alone exceed that, and at 125m it may not finish starting before its probe gives up. Sign in instead.

**4. Verify**

```bash terminal theme={null}
reliant forge env status prod
curl -s https://<your-url>/actuator/health
reliant forge obs check prod --wait --json
```

With Actuator, `{"status":"UP"}` includes the database check; switch the probe to `tiers.Probes {readinessPath = "/actuator/health/readiness"}`. `obs check` exits **0** proven good, **1** regressed, **2** not proven, **8** the wait ran out. The OpenTelemetry Java agent (`-javaagent:opentelemetry-javaagent.jar` in `JAVA_TOOL_OPTIONS`) reads the endpoint hosting sets and turns exit 2 (`no_request_telemetry`) into real verdicts.

## Spring gotchas

* **OOMKilled at startup:** raise `memoryRequestBytes`, or lower `MaxRAMPercentage`. Never set `-Xmx` above the limit.
* **Probe failures during a slow start:** the TCP probe passes only once Tomcat listens. Give a large app more CPU rather than a longer timeout.
* **`Failed to determine a suitable driver class`:** the datasource never got a JDBC URL. See the bean above.

See also: [Bring an existing app](https://docs.reliantlabs.io/guides/forge/bring-existing-app-any-language.md), [Managed Postgres](https://docs.reliantlabs.io/hosting/managed-postgres.md), [Secrets](https://docs.reliantlabs.io/hosting/secrets.md), [Hosting limits](https://docs.reliantlabs.io/hosting/limits.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.