Docker Image Packaging Standard

*Status:* authoritative · Source: the ft-docker-standards skill and the rules/deploy/docker.md rule · Applies to: container images of FT backend services

The standard for how FT backend service images are built and packaged. Developers and DevOps engineers read it. This page is the readable form of the ft-docker-standards skill. If they differ, the skill wins. Report the difference. The skill overrides any other Dockerfile convention.

It governs image packaging: base image and build stages, user and permissions, ENTRYPOINT and heap flags, .dockerignore and image-level verification. It does not govern the code inside the image. Package structure, DTOs, logging, database access and business-logic security belong to the Java standard.

Legend: a rule in D2 · Mandatory rules is mandatory. A rule in D3 · Recommended is recommended. A rule that names a thing to avoid forbids it.

Table 1. On this page
Section Topic

D1 · Why: three target environments, one image

the three target environments

D2 · Mandatory rules, D3 · Recommended

mandatory rules (1-7 and 9) and recommendations (8 and 10)

D4 · Reference Dockerfile

reference Dockerfile and measured result

D5 · Verification

test-image.sh and the three environment checks

D6 · Gotchas, D7 · Porting checklist

gotchas and porting checklist

Companion rule

rules/deploy/docker.md

Review checklist

one-screen check before review

D1 · Why: three target environments, one image

Every FT service image must run unmodified on all three environments.

Environment What it means for the image

OpenShift, restricted-v2 SCC (security context constraint, the OpenShift policy for pod identity)

The platform assigns an arbitrary UID at pod start. It ignores the USER of the image. That UID is always a member of group 0. Everything the process reads or writes must be accessible to group 0. Owner-based permissions are useless, because the owner never runs the container.

Vanilla Kubernetes

No SCC exists, so the pod spec sets a fixed runAsUser. In FT charts it is 1001, the same as the USER of the image. Here "run as the declared user" holds.

docker-compose

No orchestrator injects an identity. The USER of the image runs. Named volumes inherit the permissions of the mounted directory on first mount.

One Dockerfile, with one set of permissions, must satisfy all three at once. Section D2 · Mandatory rules lists the incidents behind the rules, and D6 · Gotchas lists the gotchas.

D2 · Mandatory rules

Rules 1 to 7 and rule 9 are mandatory. Each rule has one reason. The incidents behind the reasons follow the table.

# Rule Reason

1

USER 1001: numeric, never a name. Do not drop privileges with gosu or su-exec inside an entrypoint.

OpenShift assigns an arbitrary UID that has no entry in /etc/passwd, so a named USER does not resolve. With gosu or su-exec, PID 1 (the first process of the container) still starts as root.

2

Working directories are owned 1001:0 with g+rwX.

Restricted-v2 always puts the runtime UID in group 0. Write access for the owner protects nothing, and write access for group 0 is what an arbitrary UID has.

3

Multi-stage build. No JDK, Gradle, Maven, gcc, pip or poetry in the final layer. No build credentials in the final layer. Pass GitHub Packages credentials with BuildKit --mount=type=secret.

Build tooling at runtime widens the CVE (known vulnerability) surface, and a leftover ~/.gradle/gradle.properties or ~/.m2/settings.xml leaks a live secret in every pulled image. The secret exists at /run/secrets/<id> only during the RUN that mounts it and never persists in a layer.

4

.dockerignore is mandatory. COPY lists concrete paths, never a . glob.

A concrete COPY list cannot match a file nobody named.

5

No rolling repositories (alpine/edge, /main on an unpinned branch) on a stable base. This is forbidden without exception. Fix CVEs of base-image packages by bumping the base image tag, or with apk upgrade --no-cache inside the already pinned stable branch (see D6 · Gotchas).

A rolling branch trades a pinned, tested package set for whatever the branch holds on the day of the rebuild. The fixes usually reach the stable branch within weeks.

6

ENTRYPOINT in exec form: ["java", "-jar", "/app/app.jar"]. Do not use CMD java -jar /app/app.jar or ENTRYPOINT ["sh", "-c", "java -jar …​"].

The JVM is then PID 1 with no shell in between. This is the right practice with one less moving part. It does not fix a signal-handling defect.

7

Heap with -XX:MaxRAMPercentage=60.0, never a fixed -Xmx.

A fixed -Xmx ignores the memory limit of the pod, so the image wastes headroom or runs out of memory. -XX:+UseContainerSupport -XX:MaxRAMPercentage=60.0 in JAVA_TOOL_OPTIONS makes the JVM take 60% of the real cgroup limit (the memory limit of the container), whether the pod gets 512Mi or 4Gi. Section D6 · Gotchas describes the deploy-time trap that loses this flag.

9

Pin the base image by digest, not by a floating tag such as 25-jre-alpine. This is mandatory for a new or changed Dockerfile. An existing Dockerfile moves to a digest at its next edit. The reference Dockerfile in D4 · Reference Dockerfile still pins by tag: add the digest when you copy it.

A rebuild months later does not pick up an unrelated base change.

Incidents behind the rules

  • Rule 2. On service-api, OpenShift ran the pod as uid=1000880000. The log-directory write succeeded only because gid=0 with g+rwX.

  • Rule 4. The Dockerfile of TR069_Emulator used COPY ./. ./ with no .dockerignore. The glob matched .git. The whole .git directory was baked into every image, including a credential header persisted by actions/checkout with live admin:org, repo, write:packages scopes. It was served from a running production pod.

  • Rule 5. ftacs and hazelcast appended dl-cdn.alpinelinux.org/alpine/edge/main to fix specific CVEs.

  • Rule 6. The earlier docker-entrypoint.sh of service-api used exec gosu, which already forwarded SIGTERM (measured exit in 8.2 s on the old image). Do not claim that exec form fixes a signal-handling defect.

  • Rule 7. The -Xmx1g of tr069-emulator is the example the rule forbids.

D3 · Recommended

Rules 8 and 10 are recommended. Rule 9 (the digest pin) is mandatory and sits in D2 · Mandatory rules.

# Recommendation and reason

8

Alpine base where there are no native dependencies. ui/backend on Debian measured 423 MB. The comparable Alpine build of northbound-api measured 174 MB. Both are Spring Boot services without native dependencies. A service with a real native library (for example an Oracle client), or one where musl misbehaves (JNI, fontconfig, DNS edge cases), is the exception and takes a glibc base (-jre-jammy). The image test decides, not the assumption.

10

No duplicated artifact in the image. northbound-api copied the application jar into the image twice. That is dead weight with no benefit.

D4 · Reference Dockerfile

The service-api repository (branch docker-optimisation) is the verified reference. It is the first service that passed all three environments under this standard. Copy this file as the starting point for any other service. Section D7 · Porting checklist lists what must change for each service.

# syntax=docker/dockerfile:1.7
# FT reference Dockerfile. Standard: ft-ai-toolkit/skills/ft-docker-standards/SKILL.md
# Target environments: OpenShift (restricted-v2, arbitrary uid), vanilla k8s, docker-compose.

# --- build ---
FROM eclipse-temurin:25-jdk-alpine AS builder
WORKDIR /build

# Build description first: this layer is reused while dependencies are unchanged.
COPY gradlew .
COPY gradle gradle
COPY build.gradle settings.gradle ./
RUN chmod +x ./gradlew

# GitHub Packages credentials arrive via BuildKit secrets and never land in a layer.
RUN --mount=type=secret,id=github_user \
    --mount=type=secret,id=github_token \
    mkdir -p /root/.gradle && \
    printf 'publish.username=%s\n' "$(cat /run/secrets/github_user)" >  /root/.gradle/gradle.properties && \
    printf 'publish.token=%s\n'    "$(cat /run/secrets/github_token)" >> /root/.gradle/gradle.properties

COPY src src
RUN ./gradlew bootJar -x test --no-daemon

# --- runtime ---
FROM eclipse-temurin:25-jre-alpine

# Overriding ENTRYPOINT removes /__cacert_entrypoint.sh (custom-CA import).
# A service that needs a private CA mounts a truststore file and adds
# -Djavax.net.ssl.trustStore=/path -Djavax.net.ssl.trustStorePassword=...
# to JAVA_TOOL_OPTIONS.

# MaxRAMPercentage instead of -Xmx: the JVM must respect the container limit.
# A Helm or compose `env:` entry named JAVA_TOOL_OPTIONS REPLACES this value.
# Any override MUST include "-XX:+UseContainerSupport -XX:MaxRAMPercentage=60.0".
# A comment cannot sit inside a multi-line ENV: Docker does not parse it there.
ENV APP_HOME=/app \
    SPRING_PROFILES_ACTIVE=mysql \
    TZ=UTC \
    JAVA_TOOL_OPTIONS="-XX:+UseContainerSupport -XX:MaxRAMPercentage=60.0"

WORKDIR ${APP_HOME}

# uid 1001 with primary group 0: permissions go to the group, not the owner.
# `apk upgrade` pulls fixes already published in the pinned STABLE branch.
# It adds no repository line. The trade-off is accepted: the build is not
# reproducible over time within the stable branch.
RUN apk upgrade --no-cache \
 && apk add --no-cache curl tzdata \
 && adduser -D -H -u 1001 -G root appuser \
 && mkdir -p ${APP_HOME}/logs \
 && chown -R 1001:0 ${APP_HOME} \
 && chmod -R g+rwX ${APP_HOME}

COPY --from=builder --chown=1001:0 /build/build/libs/service-api.jar ${APP_HOME}/app.jar
COPY --chown=1001:0 version.xml ${APP_HOME}/version.xml

USER 1001

EXPOSE 8080 8443

# docker-compose is a supported target, so the healthcheck branches on
# SERVER_SSL_ENABLED. Otherwise an SSL container reports permanently unhealthy.
HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \
  CMD if [ "$SERVER_SSL_ENABLED" = "true" ]; then \
        curl -k -fsS "https://localhost:${HTTPS_PORT:-8443}/iot-webservice/actuator/health" || exit 1; \
      else \
        curl -fsS "http://localhost:${PORT:-8080}/iot-webservice/actuator/health" || exit 1; \
      fi

ENTRYPOINT ["java", "-jar", "/app/app.jar"]

LABEL org.opencontainers.image.description="Service API" \
      org.opencontainers.image.source="https://github.com/Friendly-Technologies/service-api"

Pilot evidence (service-api)

Metric Before (1.0.1_0.0.1) After (1.0.1_0.0.4)

Critical CVEs

4

0

High CVEs

37

13

Size

304 MB

169 MB

Runs as

root

uid 1001, or an arbitrary uid with gid 0

The drop from 4 Critical CVEs to 0 needed one change outside the Dockerfile. The Spring Boot plugin moved from 4.0.3 to 4.0.6 in build.gradle. The build also pins tomcat-embed-core, -el and -websocket to 11.0.24 (see D6 · Gotchas). Swapping Debian JRE for Alpine JRE cannot change application-level dependency CVEs.

Verified on OpenShift (acs, restricted-v2) under an arbitrary injected uid (1000880000, gid 0):

  • The pod rolled out healthy.

  • The write to /app/logs succeeded.

  • actuator/health returned 200.

  • MaxHeapSize was 1,289,748,480 bytes against a 2Gi limit. That is 60.06% of the limit and matches MaxRAMPercentage=60.0. The JVM default of 25% would have produced a smaller heap.

D5 · Verification

test-image.sh

The skill ft-docker-standards contains a script that checks an image (section test-image.sh). Save it as test-image.sh and run bash test-image.sh <image>. The script reads two optional variables: APP_DIR (default /app) and ARTIFACT (default $APP_DIR/app.jar). Set them for a service with another layout, for example APP_DIR=/opt/FTACS ARTIFACT=/opt/FTACS/bin/start.sh bash test-image.sh <image>. To prove a deployed pod after a rollout, use the skill ft-image-rollout-verification. It prints ok or FAIL for each check and ends with PASS when nothing failed.

Check How

The image declares USER 1001

docker image inspect, field Config.User

The process runs as uid 1001

id -u

The primary group is 0

id -g

$APP_DIR is writable by an arbitrary uid in group 0

run with --user 1234567:0 and touch $APP_DIR/logs/.w

No JDK at runtime

javac must be absent

No Gradle at runtime

run as --user 0:0: no gradle, no /root/.gradle

No build credentials

run as --user 0:0: no /root/.gradle/gradle.properties

No .git

run as --user 0:0: no $APP_DIR/.git, no $APP_DIR/HEAD

The artifact is present

$ARTIFACT exists

MaxRAMPercentage is set

JAVA_TOOL_OPTIONS contains it

The credential checks run as root on purpose. /root is mode 0700 and owned by root. As uid 1001 a check gets "permission denied", which reads as "not found" and prints a false ok.

The three environment checks

A pull request does not merge until all three checks pass for the changed image.

  1. docker-compose: run the image against a named volume. A bind mount on macOS gives a false pass (see D6 · Gotchas). Confirm the process is non-root (docker exec <c> id shows uid=1001 gid=0). Confirm that a write into the volume succeeds.

  2. Vanilla Kubernetes: render the chart with --set global.platform=kubernetes. Assert that the manifest carries runAsUser: 1001 and runAsNonRoot: true. Run kubectl apply --dry-run=client on the output and confirm it succeeds. This is a render-time check, not a live-cluster check. It was verified with helm template and kubectl apply --dry-run=client only. Do not describe it as a live deployment check.

  3. OpenShift: deploy to the real cluster. With oc exec, confirm that id shows an arbitrary uid with gid=0. Confirm that a write to the working and log directory succeeds. Confirm that the health endpoint returns 200. Confirm that MaxHeapSize (from java -XX:+PrintFlagsFinal -version | grep -i maxheapsize) matches the intended percentage of the real memory limit and not the 25% default.

D6 · Gotchas

Each gotcha caused a real, verified incident during the service-api pilot.

Gotcha What to do

gh workflow run --ref X does not set the build checkout branch (the service-api pipeline only)

build-dispatch.yml has its own workflow_dispatch input branch, default dev. --ref X selects only the revision of the workflow YAML. It does not change what actions/checkout fetches in build-callable.yml. Without -f branch=X the first service-api dispatch built and pushed dev as a root-uid image under the new hardened tag. Always pass -f branch=<branch>. Verify with docker history <image> --no-trunc | grep -iE "USER|ENTRYPOINT|adduser".

Spring Boot 4.0.6 manages tomcat-embed-core 11.0.21

The fix line is 11.0.22. Raising the Spring Boot plugin version alone does not clear the Tomcat CVEs. spring-boot-dependencies-4.0.6.pom still pins <tomcat.version>11.0.21</…​>. Add an explicit dependencyManagement pin to 11.0.24 for -core, -el and -websocket together.

apk upgrade --no-cache in the pinned stable branch is allowed. Adding a rolling repository is not.

The base image is built before some fixes land in the same stable branch (libexpat 2.8.1 to 2.8.2, p11-kit 0.25.5 to 0.26.2). apk upgrade adds no repository line. Adding alpine/edge/main, as the ftacs image did, is forbidden (D2 rule 5). The package name is libexpat, not expat. Accepted trade-off: the image is not reproducible over time within the stable branch.

A repository can hold a second Dockerfile that feeds live pipelines

service-api has Dockerfile.simple. It builds :dev and :latest from a pre-built jar, with no Gradle and no dependency access, through build-test-docker-deploy.yml and build-test-qa-deploy.yml. Deleting docker-entrypoint.sh and adding a .dockerignore that excludes build/ broke this pipeline three ways. Search the workflows for a second Dockerfile before you delete anything referenced by name. If .dockerignore excludes a directory that a secondary build needs, add a negation (!build/libs/*.jar). Do not exclude the whole tree.

A Helm env.JAVA_TOOL_OPTIONS replaces the ENV of the image

It does not append. Measured: the ENV of the image alone gave a 645 MB heap (the 25% default). One unrelated -D flag under the same variable name gave 268 MB, because the override dropped MaxRAMPercentage. Any chart that passes a -D flag under JAVA_TOOL_OPTIONS must carry -XX:+UseContainerSupport -XX:MaxRAMPercentage=60.0 in the same string. Check the per-environment overlay too. The Helm env: merge works per key across value files (chart default, shared environment values, component values, release overrides). The narrowest file wins on that one key.

The credential check of test-image.sh must run as --user 0:0

As uid 1001 the check meets /root (mode 0700) and gets "permission denied", which reads as "not found" and prints a false ok. This was verified by planting a leaked credential file and confirming that the check catches it. A check that has never failed proves nothing.

COPY ./. ./ matches .git

With no .dockerignore the whole repository is baked into the image, including the credential that actions/checkout persisted in .git/config. This is how the TR069_Emulator GitHub token reached a production pod. Fix both ends: a glob-free COPY list in the Dockerfile, and persist-credentials: false on the actions/checkout step.

Overriding ENTRYPOINT drops the /__cacert_entrypoint.sh of eclipse-temurin

That script makes USE_SYSTEM_CA_CERTS and custom-CA import work at container start. /opt/java/openjdk/lib/security/cacerts is root-owned 0644, so uid 1001 cannot import into it at runtime. A service that needs a private CA mounts a truststore file and adds -Djavax.net.ssl.trustStore=/path -Djavax.net.ssl.trustStorePassword=…​ to JAVA_TOOL_OPTIONS.

A VOLUME takes its contents from the volume at runtime, not from the image

No chown or chmod in the Dockerfile reaches data that exists only after the volume is mounted. Permissions on a mounted volume (for example a PVC seeded by an older root-run image) are a deployment-time concern. Fix them with a one-off ownership pass against the live volume.

Create, chown and chmod a writable directory in the same RUN

That RUN does the chown 1001:0 and chmod g+rwX pass. It must run before the COPY instructions that populate the directory. If you split mkdir from the ownership pass, or run the pass before the directory exists, the directory keeps the default ownership of the base image or of a later COPY.

D7 · Porting checklist

Check every line below. Do not assume it when you copy D4 · Reference Dockerfile to another service.

Item What to verify

COPY build.gradle settings.gradle ./

The form is glob-free on purpose. It fails if the settings file is absent or named settings.gradle.kts. Check the real filename per service: find . -maxdepth 1 -iname "settings.gradle*".

The build command

Maven or Gradle. For Gradle, confirm that the bootJar task exists in the build.gradle of the service.

The artifact path

build/build/libs/service-api.jar works only because build.gradle of service-api sets bootJar { archiveFileName = …​ } and jar { enabled = false }. A service without that config produces a differently named or an additional jar. Verify the output filename per service.

COPY version.xml

Confirm that the service has and needs this file.

SPRING_PROFILES_ACTIVE

Set the correct default profile for the service. Do not copy mysql unchecked.

The healthcheck context path

/iot-webservice/actuator/health is specific to service-api. Each service has its own context path and actuator base path.

EXPOSE

Match the real listening ports. Do not default to 8080 8443.

BuildKit secret ids and the publish.username and publish.token names

They must match what build.gradle or pom.xml of the service reads for GitHub Packages. The names are not identical across services.

The Java version in the base image tag (eclipse-temurin:25-…​)

Match the JDK or JRE version the service requires.

GitHub Packages dependencies

A service that consumes them (for example an internal api-common artifact) cannot build locally without access to the package repository. Build and test the image in CI against the pushed artifact. Do not rely on a local docker build as verification.

The companion rule: rules/deploy/docker.md

The toolkit also ships a short rule that loads when you edit a Dockerfile*, a .j2, .tpl or .tmpl template, a docker-compose*.yml or a .dockerignore. See Rules for how paths: loading works. The rule covers build and runtime practice around the image.

Topic Rule

Two-file pattern

Dockerfile is multi-stage and builds the JAR inside, for local parity. Dockerfile.simple copies a CI-built JAR, for CI/CD. The exact JAR that passed tests ships. CI stages the artifact in a dedicated directory such as ./dist/app.jar, so target/ and build/ stay in .dockerignore.

Image hygiene

No secrets in ENV, ARG or COPY, because a deleted line stays in the layer history. No curl-to-bash installs: use a pinned release with sha256sum -c, or copy from an official image. Minimum .dockerignore: .git, target/, build/, node_modules/, .env*, .pem, .key, logs.

Spring Boot layers

Layered JARs are mandatory in the multi-stage file. Enable layers in the boot plugin and extract with java -Djarmode=tools -jar app.jar extract --layers --destination extracted. Copy the layers from least to most volatile: dependencies, spring-boot-loader, snapshot-dependencies, application.

JVM flags

Set them through JAVA_TOOL_OPTIONS. Never set -Xmx or -Xms, because they silently override MaxRAMPercentage. State the GC explicitly: G1 for a heap of 2 GB or more, Parallel below 2 GB. With CPU limits, set -XX:ActiveProcessorCount to about twice the limit. The rule also lists -XX:+ExitOnOutOfMemoryError and heap-dump flags.

Timezone

Decide TZ or -Duser.timezone deliberately. Keep it consistent with what naive DATETIME columns store.

Registry

Push a semver tag and a git SHA tag. Enable tag immutability where the registry supports it. Deploy by digest. Scan on push and rescan on a schedule. A CRITICAL finding blocks.

The rule follows the standard

The rule follows the standard, and on any conflict the standard wins: Alpine where the service has no native dependency and the image test passes, a glibc base otherwise; USER 1001 with writable directories owned 1001:0 and g+rwX; MaxRAMPercentage=60.0, higher only with a measured off-heap headroom; and the base image pinned by digest in every new or changed Dockerfile.

Review checklist

  • USER 1001 is numeric. Working directories are 1001:0 with g+rwX.

  • The base image is pinned by digest in a new or changed Dockerfile.

  • Multi-stage build. No build tooling and no credentials in the final layer. Secrets arrive through --mount=type=secret.

  • .dockerignore exists. COPY lists concrete paths and has no . glob.

  • No rolling repository. apk upgrade --no-cache only inside the pinned stable branch.

  • ENTRYPOINT is in exec form.

  • JAVA_TOOL_OPTIONS carries -XX:+UseContainerSupport -XX:MaxRAMPercentage=60.0. No -Xmx. Chart and overlay overrides keep both flags.

  • The directory is created, owned and chmodded in one RUN, before the COPY that fills it.

  • The healthcheck context path, EXPOSE, profile and Java version match the service.

  • test-image.sh passes. Compose, Kubernetes render and OpenShift checks are done, and the Kubernetes claim says "render-time only".

  • The build dispatch passed -f branch=<branch>, and docker history confirms the content.