Skip to content

Commit d6e92b1

Browse files
akramclaude
andcommitted
feat(docker): multistage builds with CRYPTO_BACKEND=openssl support
Cherry-pick multistage Dockerfiles and add CRYPTO_BACKEND build arg. Pass CRYPTO_BACKEND=openssl to build the gateway with system OpenSSL instead of aws-lc. Works with Varsha's crypto refactor (ai-openshell#5046). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 203cef3 commit d6e92b1

3 files changed

Lines changed: 235 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# syntax=docker/dockerfile:1.4
2+
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
# In-cluster (OpenShift/Buildah) variant of Dockerfile.gateway.
6+
#
7+
# Upstream CI builds the GNU-linked openshell-gateway binary inside the
8+
# project's Nix devShell with the gnu cross target (z3 and aws-lc statically
9+
# embedded, standard ELF interpreter), and stages it under
10+
# deploy/docker/.build/prebuilt-binaries. This multi-stage Dockerfile
11+
# reproduces that exact artifact in a builder stage so clusters without the Nix
12+
# CI pipeline can build the gateway image directly from source. The final stage
13+
# is identical to Dockerfile.gateway.
14+
15+
# In a multi-stage build, an ARG that feeds a `FROM` must be declared before the
16+
# very first FROM (global pre-FROM scope). Declaring it between the two stages
17+
# makes Buildah attach it to the builder stage and fail the second FROM with
18+
# "no FROM statement found".
19+
ARG GATEWAY_BASE_IMAGE=gcr.io/distroless/cc-debian13:nonroot@sha256:d97bc0a941b8d4be647dc0ee75b264ddbb772f1ac5ba690a4309c00723b23775
20+
21+
# ---- Builder: glibc base + Nix, reproduce upstream's cross build -------------
22+
#
23+
# The build must run on a glibc/FHS base (like the CI runners), NOT on the
24+
# minimal nixos/nix image: cross-compiling emits host build-scripts linked for
25+
# x86_64-unknown-linux-gnu whose ELF interpreter is /lib64/ld-linux-x86-64.so.2,
26+
# which the nixos/nix (Alpine, store-only) image does not provide.
27+
FROM debian:bookworm-slim AS builder
28+
29+
RUN apt-get update && apt-get install -y --no-install-recommends \
30+
curl xz-utils ca-certificates git && rm -rf /var/lib/apt/lists/*
31+
32+
# Single-user Nix (no daemon) as root. NIX_CONFIG disables per-build users
33+
# during the install itself (the installer reads config before our nix.conf
34+
# exists); the written nix.conf carries that to later `nix develop` runs, plus
35+
# flakes and the flake's cachix substituter for prebuilt toolchain/z3/aws-lc.
36+
RUN export NIX_CONFIG="build-users-group =" && \
37+
mkdir -m 0755 /nix && \
38+
curl -L https://nixos.org/nix/install -o /tmp/nix-install.sh && \
39+
sh /tmp/nix-install.sh --no-daemon && \
40+
mkdir -p /etc/nix && \
41+
printf 'experimental-features = nix-command flakes\naccept-flake-config = true\nbuild-users-group =\nsandbox = false\n' > /etc/nix/nix.conf
42+
43+
# Put single-user Nix on PATH directly (Buildah RUN is not a login shell, so the
44+
# profile script is unreliable) and point it at Debian's CA bundle for HTTPS
45+
# substituter fetches.
46+
ENV HOME=/root \
47+
PATH=/root/.nix-profile/bin:/nix/var/nix/profiles/default/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
48+
NIX_SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt
49+
50+
WORKDIR /build
51+
COPY . .
52+
53+
# Build the gateway exactly like CI: same devShell, same cargo command and
54+
# target triple. The gnu cross toolchain emits a standard ELF interpreter, so
55+
# the binary runs on distroless without post-processing. Everything runs in a
56+
# single layer: build, stage the binary at /, then delete the Nix store and
57+
# Cargo target so the committed builder layer stays small.
58+
ARG CRYPTO_BACKEND=aws-lc
59+
RUN if [ "${CRYPTO_BACKEND}" = "openssl" ]; then \
60+
sed -i 's/features = \["aws-lc"\]/features = ["openssl"]/' crates/openshell-gateway/Cargo.toml; \
61+
fi
62+
RUN nix develop -c bash -euo pipefail -c '\
63+
GIT_DIR=/nonexistent cargo auditable build --release \
64+
--target x86_64-unknown-linux-gnu \
65+
--package openshell-gateway --bin openshell-gateway' && \
66+
cp target/x86_64-unknown-linux-gnu/release/openshell-gateway /openshell-gateway && \
67+
cd / && rm -rf /nix /build /root/.cache /tmp/*
68+
69+
# ---- Runtime: identical to deploy/docker/Dockerfile.gateway ------------------
70+
#
71+
# Distroless Debian provides the glibc runtime required by the binary.
72+
FROM ${GATEWAY_BASE_IMAGE} AS gateway
73+
74+
ARG TARGETARCH
75+
76+
WORKDIR /app
77+
78+
COPY --from=builder /openshell-gateway /usr/local/bin/openshell-gateway
79+
80+
USER 1000:1000
81+
EXPOSE 8080
82+
83+
ENTRYPOINT ["/usr/local/bin/openshell-gateway"]
84+
CMD ["--bind-address", "0.0.0.0", "--port", "8080"]
85+
86+
# --- OpenSSL/FIPS support ---
87+
# Override CRYPTO_BACKEND=openssl to build with system OpenSSL instead of aws-lc
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# syntax=docker/dockerfile:1.4
2+
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
# In-cluster (OpenShift/Buildah) variant of Dockerfile.sandbox.
6+
#
7+
# Upstream CI produces the static musl `openshell-sandbox` binary by building
8+
# inside the project's Nix devShell with the musl cross target, and stages it
9+
# under deploy/docker/.build/prebuilt-binaries. This multi-stage Dockerfile
10+
# reproduces that exact binary in a builder stage so clusters without the Nix
11+
# CI pipeline can build the sandbox image directly from source. The final stage
12+
# is identical to Dockerfile.sandbox.
13+
14+
# ---- Builder: glibc base + Nix, reproduce upstream's cross build -------------
15+
#
16+
# The build must run on a glibc/FHS base (like the CI runners), NOT on the
17+
# minimal nixos/nix image: cross-compiling emits host build-scripts linked for
18+
# x86_64-unknown-linux-gnu whose ELF interpreter is /lib64/ld-linux-x86-64.so.2,
19+
# which the nixos/nix (Alpine, store-only) image does not provide.
20+
FROM debian:bookworm-slim AS builder
21+
22+
RUN apt-get update && apt-get install -y --no-install-recommends \
23+
curl xz-utils ca-certificates git && rm -rf /var/lib/apt/lists/*
24+
25+
# Single-user Nix (no daemon) as root. NIX_CONFIG disables per-build users
26+
# during the install itself (the installer reads config before our nix.conf
27+
# exists); the written nix.conf carries that to later `nix develop` runs, plus
28+
# flakes and the flake's cachix substituter for prebuilt toolchain/z3/aws-lc.
29+
RUN export NIX_CONFIG="build-users-group =" && \
30+
mkdir -m 0755 /nix && \
31+
curl -L https://nixos.org/nix/install -o /tmp/nix-install.sh && \
32+
sh /tmp/nix-install.sh --no-daemon && \
33+
mkdir -p /etc/nix && \
34+
printf 'experimental-features = nix-command flakes\naccept-flake-config = true\nbuild-users-group =\nsandbox = false\n' > /etc/nix/nix.conf
35+
36+
# Put single-user Nix on PATH directly (Buildah RUN is not a login shell, so the
37+
# profile script is unreliable) and point it at Debian's CA bundle for HTTPS
38+
# substituter fetches.
39+
ENV HOME=/root \
40+
PATH=/root/.nix-profile/bin:/nix/var/nix/profiles/default/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
41+
NIX_SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt
42+
43+
WORKDIR /build
44+
COPY . .
45+
46+
# Build the sandbox binary exactly like CI: same devShell, same cargo command
47+
# and target triple. Everything runs in a single layer: build, stage the static
48+
# binary at /, then delete the Nix store and Cargo target. The binary is static
49+
# musl (it needs nothing from /nix at runtime), so the committed builder layer
50+
# shrinks to the binary alone, keeping the intermediate commit fast and within
51+
# the node's ephemeral-storage budget.
52+
RUN nix develop -c bash -euo pipefail -c '\
53+
GIT_DIR=/nonexistent cargo auditable build --release \
54+
--target x86_64-unknown-linux-musl \
55+
--package openshell-sandbox --bin openshell-sandbox' && \
56+
cp target/x86_64-unknown-linux-musl/release/openshell-sandbox /openshell-sandbox && \
57+
cd / && rm -rf /nix /build /root/.cache /tmp/*
58+
59+
# ---- Runtime: identical to deploy/docker/Dockerfile.sandbox ------------------
60+
#
61+
# Runtime-specific bootstrap tools belong to their compute drivers rather than
62+
# this portable image, so the sandbox runtime image is a bare scratch layer.
63+
FROM scratch AS sandbox
64+
65+
ARG TARGETARCH
66+
67+
# Keep the binary root-owned for image-volume mounts and executable by the
68+
# sandbox runtime's non-root UID.
69+
COPY --from=builder --chmod=0555 /openshell-sandbox /openshell-sandbox
70+
71+
# Drivers may override this identity to match the admitted workload. Keep the
72+
# standalone image non-root by default.
73+
USER 65532:65532
74+
75+
ENTRYPOINT ["/openshell-sandbox"]
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# syntax=docker/dockerfile:1.4
2+
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
# In-cluster (OpenShift/Buildah) variant of Dockerfile.supervisor.
6+
#
7+
# Upstream CI builds the dynamically linked GNU `openshell-supervisor` binary
8+
# inside the project's Nix devShell with the gnu cross target (z3 and aws-lc
9+
# statically embedded, standard ELF interpreter), and stages it under
10+
# deploy/docker/.build/prebuilt-binaries. This multi-stage Dockerfile reproduces
11+
# that exact artifact in a builder stage so clusters without the Nix CI pipeline
12+
# can build the supervisor image directly from source. The final stage is
13+
# identical to Dockerfile.supervisor.
14+
15+
# ---- Builder: glibc base + Nix, reproduce upstream's cross build -------------
16+
#
17+
# The build must run on a glibc/FHS base (like the CI runners), NOT on the
18+
# minimal nixos/nix image: cross-compiling emits host build-scripts linked for
19+
# x86_64-unknown-linux-gnu whose ELF interpreter is /lib64/ld-linux-x86-64.so.2,
20+
# which the nixos/nix (Alpine, store-only) image does not provide.
21+
FROM debian:bookworm-slim AS builder
22+
23+
RUN apt-get update && apt-get install -y --no-install-recommends \
24+
curl xz-utils ca-certificates git && rm -rf /var/lib/apt/lists/*
25+
26+
# Single-user Nix (no daemon) as root. NIX_CONFIG disables per-build users
27+
# during the install itself (the installer reads config before our nix.conf
28+
# exists); the written nix.conf carries that to later `nix develop` runs, plus
29+
# flakes and the flake's cachix substituter for prebuilt toolchain/z3/aws-lc.
30+
RUN export NIX_CONFIG="build-users-group =" && \
31+
mkdir -m 0755 /nix && \
32+
curl -L https://nixos.org/nix/install -o /tmp/nix-install.sh && \
33+
sh /tmp/nix-install.sh --no-daemon && \
34+
mkdir -p /etc/nix && \
35+
printf 'experimental-features = nix-command flakes\naccept-flake-config = true\nbuild-users-group =\nsandbox = false\n' > /etc/nix/nix.conf
36+
37+
# Put single-user Nix on PATH directly (Buildah RUN is not a login shell, so the
38+
# profile script is unreliable) and point it at Debian's CA bundle for HTTPS
39+
# substituter fetches.
40+
ENV HOME=/root \
41+
PATH=/root/.nix-profile/bin:/nix/var/nix/profiles/default/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
42+
NIX_SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt
43+
44+
WORKDIR /build
45+
COPY . .
46+
47+
# Build the supervisor exactly like CI: same devShell, same cargo command and
48+
# target triple. The gnu cross toolchain emits a standard ELF interpreter, so
49+
# the binary runs on a plain Debian base without post-processing. Everything
50+
# runs in a single layer: build, stage the binary at /, then delete the Nix
51+
# store and Cargo target so the committed builder layer stays small.
52+
RUN nix develop -c bash -euo pipefail -c '\
53+
GIT_DIR=/nonexistent cargo auditable build --release \
54+
--target x86_64-unknown-linux-gnu \
55+
--package openshell-supervisor --bin openshell-supervisor' && \
56+
cp target/x86_64-unknown-linux-gnu/release/openshell-supervisor /openshell-supervisor && \
57+
cd / && rm -rf /nix /build /root/.cache /tmp/*
58+
59+
# ---- Runtime: identical to deploy/docker/Dockerfile.supervisor ---------------
60+
#
61+
# The dynamically linked GNU supervisor binary needs a glibc runtime plus a CA
62+
# bundle for its outbound TLS connections.
63+
FROM debian:bookworm-slim AS supervisor
64+
65+
ARG TARGETARCH
66+
67+
RUN apt-get update \
68+
&& apt-get install -y --no-install-recommends ca-certificates \
69+
&& rm -rf /var/lib/apt/lists/*
70+
71+
COPY --from=builder --chmod=0555 /openshell-supervisor /openshell-supervisor
72+
73+
ENTRYPOINT ["/openshell-supervisor"]

0 commit comments

Comments
 (0)