Docker images

Build a container image for your StoatFlow app with the StoatFlow build conventions (Jib) — base image, JVM flags, reproducible builds, container-aware heap sizing, ports and env — on Gradle or Maven.

The supported way to containerize a StoatFlow app is the StoatFlow build conventions' Jib integration — no Dockerfile, no Docker daemon to assemble layers. It produces a reproducible image wired with the JVM flags StoatFlow needs and an entrypoint that sizes the heap from the container's memory limit at startup. Both the Gradle plugin and the Maven conventions build the same image.

Build the image (Jib)

Container support is opt-in. Apply the conventions (see Project setup), set mainClass, and turn the docker build on:

// build.gradle.kts
stoatflow {
    mainClass.set("com.example.MainKt")
    docker {
        enabled.set(true)
        imageName.set("acme/word-count")   // default: the Gradle project name
    }
}

Then build the image into your local Docker daemon:

./gradlew jibDockerBuild

Jib reads your compiled classes and dependencies and assembles an OCI image directly — no Dockerfile, no intermediate fat JAR to manage. The image is tagged latest and the project version, named after imageName (or the project name if unset).

jibDockerBuild / mvn -Pstoatflow-docker package needs a reachable Docker daemon to receive the image, but Jib does not run a docker build — it constructs the layers itself. On macOS the build resolves the docker executable from common install paths (/usr/local/bin/docker, /opt/homebrew/bin/docker) when the daemon's PATH doesn't include them.

What the build configures

The Jib image has the following baked in:

AspectValueKnob
Base imageeclipse-temurin:25-jre-noblebaseImage
Platformlinux/arm64 on an aarch64 host, otherwise linux/amd64—
Tagslatest and the project version—
creationTimeHEAD commit timestamp (reproducible)reproducibleBuild
Exposed port8080 — the runtime's HTTP + metrics serverport
User1000:1000 (non-root)user
Working directory/app—
Entrypointa heap-sizing script (/app/stoatflow-entrypoint.sh), then the JVM flags — --enable-native-access=ALL-UNNAMED, -XX:+UseG1GC, plus your jvmFlags — and -cp @/app/jib-classpath-file @/app/jib-main-class-file, which run your main class. The script sizes the heap and execs java with the restjvmFlags, mainClass (required)
Command (CMD)empty — arguments you give the container are passed to your main (see Container arguments)—
EnvSTOATFLOW_STATE_DIR=/data/state (the application's stoatflow.state.dir); STOATFLOW_MEMORY_PROFILE=balanced; STOATFLOW_HEAP_MB only when heapMb is setstateDir, memoryProfile, heapMb

Every knob in the right column has the same name in the Gradle docker { } block and a stoatflow.docker.<name> Maven property — the full enumeration with types and defaults is in the Gradle plugin reference and the Maven reference.

JVM flags

The image always carries the flags StoatFlow requires at runtime, the same ones applied to the run task. They are part of the image's entrypoint: the script sizes the heap, then starts java with them, the application classpath and your mainClass, which an image build therefore requires.

  • --enable-native-access=ALL-UNNAMED — RocksDB's state store uses the Foreign Function & Memory API.
  • -XX:+UseG1GC — short, predictable GC pauses, set explicitly because the JVM only auto-selects G1 on "server-class" hardware (≥ 2 CPUs and ≥ ~2 GB memory). See Installation for the reasoning.

Anything you add via jvmFlags is appended after these (and jvmFlags is itself overridable — the native-access flag is always re-added on top):

stoatflow {
    docker {
        enabled.set(true)
        jvmFlags.add("-XX:+ExitOnOutOfMemoryError")
        jvmFlags.add("-Dsome.property=value")
    }
}

Reproducible builds

By default the image is reproducible: its creationTime is pinned to your HEAD commit timestamp, not wall-clock — so identical source produces an identical digest with a meaningful Created date, friendly to layer caching and supply-chain verification. Outside a git repo the timestamp falls back to the Unix epoch. Set reproducibleBuild = false (Gradle) / <stoatflow.docker.reproducibleBuild>false</…> (Maven) to restore wall-clock timestamps.

Container-aware heap sizing

The Jib image does not hardcode -Xmx. Its entrypoint is the launcher's half of the memory plan: it reads the container's memory limit at startup, sizes the heap from it, and execs java with the result. The engine then sizes RocksDB from what the JVM was given, so no RocksDB number is baked into the image. The same image right-sizes itself at a 256 MiB limit or a 64 GiB one — you set the container memory limit, not a JVM flag.

The memory limit is resolved from the first available source, in order:

  1. cgroup v2 (/sys/fs/cgroup/memory.max) — modern containers (Ubuntu 22.04+, Kubernetes 1.25+).
  2. cgroup v1 (/sys/fs/cgroup/memory/memory.limit_in_bytes) — older runtimes.
  3. /proc/meminfo — bare-metal Linux. The heap is sized as for a container, but the engine plans no RocksDB pool without a cgroup limit: RocksDB gets one 256 MiB cache.
  4. If none report a total, the entrypoint passes -XX:MaxRAMPercentage=75.0 and lets the JVM decide.

From the limit M the entrypoint computes:

PartRuleFlag
JVM native memorymax(160 MiB, min(M / 4, 512 MiB)) — metaspace, code cache, GC structures, thread stacks, direct buffers. G1, metaspace and the code cache measured 130–150 MiB resident on small pods, hence the floor; from 640 MiB up it is M / 4—
Headroommax(32 MiB, 5 % of M) — page cache, the kernel, sockets; the 5 % governs from 640 MiB up—
Heapthe memory profile's brackets, at least 64 MiB — or STOATFLOW_HEAP_MB, which can set any heap-Xmx
Direct memoryclamp(3 % of M, 64 MiB, 128 MiB), inside the JVM's native memory-XX:MaxDirectMemorySize
NIO buffer cache256 KiB per thread, so a large Kafka fetch leaves no per-thread direct buffer of its size behind-Djdk.nio.maxCachedBufferSize=262144
glibc arenas2, unless MALLOC_ARENA_MAX is already set — by default glibc keeps up to 8 per core, and every thread calling into RocksDB can hold oneMALLOC_ARENA_MAX=2 (exported)

The rest, M − JVM native − heap − headroom, is the RocksDB pool the engine plans at start.

VariableDefaultEffect
STOATFLOW_MEMORY_PROFILEbalancedThe heap's brackets: balanced, state-heavy (a smaller heap, a larger RocksDB pool), heap-heavy (in-memory stores, large suppress buffers) or lean (small, low-throughput apps: a quarter of the first GiB for the heap). Set at build time by the memoryProfile knob; override it at run time. See Memory profiles.
STOATFLOW_HEAP_MBunsetA fixed heap in MiB instead of the brackets. The engine plans RocksDB from what it leaves. Set at build time by the heapMb knob, or at run time.
MALLOC_ARENA_MAX2glibc's arena count. Set it yourself to override; the entrypoint keeps a value already set.

The entrypoint logs its decision to stderr at startup, so it is visible in the container logs:

StoatFlow heap sizing: source=cgroup-v2, total=16384 MiB, profile=balanced, jvm=512 MiB, headroom=819 MiB, heap=3277 MiB (-Xmx3277m), direct=128 MiB (-XX:MaxDirectMemorySize), MALLOC_ARENA_MAX=2

and, for a stateless app in a 256 MiB pod on lean:

StoatFlow heap sizing: source=cgroup-v2, total=256 MiB, profile=lean, jvm=160 MiB, headroom=32 MiB, heap=64 MiB (-Xmx64m), direct=64 MiB (-XX:MaxDirectMemorySize), MALLOC_ARENA_MAX=2

With STOATFLOW_HEAP_MB set, the line names it instead of the profile. When a fixed heap leaves no room in the container — jvm + heap + headroom above the limit — it logs a WARNING naming the shortfall: expect an OOM kill, and use a larger limit or a smaller STOATFLOW_HEAP_MB. The engine's own start-up line, Memory plan: …, then shows how it split the RocksDB pool.

STOATFLOW_HEAP_MB must be a positive plain integer (MiB) without leading zeros — e.g. 2048, not 2g, 0 or 02048 — and STOATFLOW_MEMORY_PROFILE one of the four profiles. The entrypoint fails fast on anything else, and the Gradle and Maven plugins fail the build on an unknown memoryProfile or a non-positive heapMb.
Your own launcher works too. The engine reads the container's limit and the heap the JVM actually runs with, so an image with its own -Xmx, or java -jar in a container, gets the same RocksDB plan from whatever heap it was given. Run one JVM per container: several JVMs in one cgroup each plan against the whole limit. The native runtime image uses the same brackets with a smaller share for its own memory and a lower heap floor.
Small pods. With STOATFLOW_MEMORY_PROFILE=lean, a stateless app fits a 256 MiB container and a stateful one with a few stores 512 MiB; below these sizes, or with more stores than the cache holds, the engine WARNs at start. A native image fits half those sizes — 128 and 256 MiB — and is the better fit at 256 MiB and below. G1 stays the collector; the optional JVM flags that trim a small pod further are in Tuning → Small pods.

Run the container

The image exposes port 8080 — the runtime's HTTP admin + Prometheus metrics server (see REST API and Metrics) — and reads its own configuration (bootstrap servers, licence, topics) from application.yaml and environment overrides (see Runtime config). The runtime keeps its RocksDB state in the image's STOATFLOW_STATE_DIR (/data/state by default): the variable sets stoatflow.state.dir and wins over the value in your application.yaml, so mount a volume there to persist the state across restarts. A JVM image contains that directory, writable by its non-root user (a native image runs as root): without a volume the state lives inside the container (and is gone with it), and a Docker volume mounted there needs no chown. To use another path, change stateDir at build time, or set STOATFLOW_STATE_DIR at run time (docker run -e, a Kubernetes env: entry), which replaces the image's value:

docker run --rm \
  -e KAFKA_BOOTSTRAP_SERVERS=host.docker.internal:9092 \
  -e STOATFLOW_LICENCE_KEY="sfl1...your licence from the onboarding email..." \
  -e STOATFLOW_LICENCE_ENVIRONMENT=prd-eu \
  -v stoatflow-state:/data/state \
  -p 8080:8080 \
  --memory 4g \
  acme/word-count:latest

--memory is the limit the entrypoint and the engine plan against; without one there is no plan, and RocksDB runs on one 256 MiB cache. Add -e STOATFLOW_MEMORY_PROFILE=state-heavy to give RocksDB more of it, or -e STOATFLOW_MEMORY_PROFILE=lean with a small --memory for a small, low-rate app.

A Kubernetes deployment then needs only a memory limit (the entrypoint sizes the heap from it) and a volume for the state dir — see Kubernetes:

resources:
  limits:
    memory: 2Gi          # the entrypoint sizes the heap from this
volumeMounts:
  - { name: state, mountPath: /data/state }   # matches stateDir

Any configuration key can be passed with -e as in the docker run above: a key's variable is its YAML path with every . and - as _, upper-cased (stoatflow.lanes.count is STOATFLOW_LANES_COUNT, runtime.http.port is RUNTIME_HTTP_PORT) — see How configuration works.

Secrets from a mounted directory

A credential is better kept out of the environment, where docker inspect shows it. The runtime reads ${secret:name} placeholders from files in /run/secrets — where Docker mounts its secrets — so the YAML names the file and the container mounts it:

# application.yaml
stoatflow:
  licence:
    key: ${secret:licence}
  kafka:
    consumer:
      sasl.jaas.config: ${secret:jaas}

With Docker Compose, each secret becomes /run/secrets/<name>:

# compose.yaml
services:
  word-count:
    image: acme/word-count:latest
    environment:
      STOATFLOW_LICENCE_ENVIRONMENT: prd-eu
    secrets: [licence, jaas]
    volumes:
      - stoatflow-state:/data/state
secrets:
  licence:
    file: ./secrets/licence
  jaas:
    file: ./secrets/jaas.conf
volumes:
  stoatflow-state:

With plain docker run, mount a directory there: -v "$PWD/secrets:/run/secrets:ro". The files are read once at start (one trailing newline removed), a missing one fails the start naming its path, and their values are masked in /config and the logs. STOATFLOW_SECRETS_DIR moves the directory. See Secrets from files.

Container arguments

The whole JVM command line — flags, classpath and main class — is the image's entrypoint, so arguments you give the container reach your application instead: docker run <image> --some-arg and a Kubernetes container's args: arrive in main(String[] args), and the flags and the main class stay. Don't override the entrypoint itself (docker run --entrypoint, a Kubernetes command:): that replaces the JVM command line, heap sizing included. To add JVM options, use the jvmFlags knob at build time or the JAVA_TOOL_OPTIONS environment variable at run time; for application settings, use environment overrides (see Runtime config).

Rolling your own Dockerfile

The Jib path is the supported, simplest route and right-sizes the heap automatically — prefer it. If your pipeline standardizes on docker build, build the fat JAR (./gradlew shadowJar / mvn package) and write a thin image over it: a eclipse-temurin:25-jre-noble base, COPY the -all.jar, and launch java --enable-native-access=ALL-UNNAMED -XX:+UseG1GC -jar app.jar. You lose the entrypoint's half of the memory plan — pin -Xmx yourself, and set -XX:MaxDirectMemorySize, -Djdk.nio.maxCachedBufferSize=262144 and MALLOC_ARENA_MAX=2 as the entrypoint would. The engine still plans RocksDB from the container's limit and the heap you gave it.

For a native image (GraalVM), the containerized build is a docker build against a bundled, repo-independent Dockerfile.native — see Native image.

Next steps