Files
openwaf/Dockerfile.scratch
T
Pavlo Ratushnyi be6c608ada
ci / the operator page in a real browser (push) Successful in 2m38s
ci / lint the workflow and the shell (push) Successful in 13s
geo-freshness / the pinned geo database vs the vendor (push) Successful in 4s
ci / build, test, smoke (push) Successful in 2m48s
ci / build the image and smoke it (push) Successful in 4m23s
ci / the same smoke on an image with no base (push) Successful in 5m3s
the edge ships on an image with no base, and the 368 assertions agree it behaves
Dockerfile.scratch builds the same program self-contained and puts it on
scratch: 12 shared objects and the CA bundle — 18.7 MB — carried at their
absolute paths, because a scratch layer has nothing to borrow them from, and the
dynamic loader has to land at the path recorded inside the binary or the kernel
refuses to start the process. Measured on one arm64 host: chiseled 262 MB,
scratch 222 MB. scripts/compose-smoke.sh runs against it unchanged through
docker-compose.scratch.yml: 368 passed, 0 failed, and 368/0 again when the same
gate is called as scripts/ci-gates.sh scratch-smoke.

The first scratch image built here is why that second run matters. It started,
its own HEALTHCHECK reported healthy, and it still failed 7 of the 368. A
self-contained apphost takes its content root from the process's working
directory, a scratch image has no WORKDIR, so the process began in /, never found
the appsettings.json next to it, the reverse proxy had no routes, and every
request that should have reached the upstream answered the edge's own 404 —
including /boom's 502, which turned the "server errors alert, they never ban"
scene into a streak ban. The edge said it in its own log: "Request reached the
end of the middleware pipeline without being handled by application code."

The schema-script gate was red on the runner at 7c5e1ef (run 2462, exit 2):
`dotnet tool restore` from the repo root finds no manifest, prints "No tools were
restored." and exits 0, so the gate's own restore check passed vacuously and
`dotnet ef` then died. It now restores dotnet/dotnet-tools.json by path and asks
the tool for its version before trusting it.

NativeAOT, measured further: with the two alert-queue queries taken out,
`dotnet ef dbcontext optimize --precompile-queries` on EF 10.0.12 reports 0
precompilation errors and writes 8 interceptor files — 14 of whose declarations
have dots in the identifier, because the local is named from the entity type's
full name. The 4 refusals that remain are four shapes the generator cannot
translate at all: Contains over a captured collection, any method parameter
referenced inside a query (down to the CancellationToken), an ExecuteUpdate
set-list, and a literal whose type is not the column's. docs/ARCHITECTURE.md
carries the exception for each. No C# changed in this commit: the experiments
that produced those numbers were reverted, and the query rewrites kept at
7c5e1ef are the ones that made 40 refusals into 4.

Gates: scratch-image and scratch-smoke in scripts/ci-gates.sh and in `all`, and a
fifth CI job that runs the 368 against the scratch image on the runner.
2026-10-06 06:38:07 +02:00

164 lines
8.0 KiB
Docker
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# syntax=docker/dockerfile:1.7
#
# openwaf edge — FROM scratch: no base image, no package manager, no shell, no
# distro. The only things in the runtime layer are this program, the shared
# objects it was linked against, the CA bundle it needs to verify an upstream's
# certificate, and the geo database.
#
# What "FROM scratch" actually costs, measured rather than assumed: the runtime
# has no OS to borrow libraries from, so every shared object has to be carried
# in. They are resolved with ldd inside the build stage and copied at their
# absolute paths — the ELF interpreter (/lib64/ld-linux-x86-64.so.2 on x86-64)
# is recorded inside the apphost, so it must land at exactly that path or the
# kernel refuses to start the process. ldd cannot see what a program dlopen()s,
# so the NSS modules getaddinfo() uses to turn "upstream" into an address are
# added by name; without them the compose upstream by service name resolves to
# nothing and every request is a 502.
#
# Why self-contained and not NativeAOT, measured on this tree on 2026-10-06:
# the AOT build compiles and its binary creates its own schema, then refuses
# every EF query ("Query wasn't precompiled and dynamic code isn't supported
# with NativeAOT"), and EF 10.0.12's precompile-queries generator cannot produce
# code for this model — it refuses Contains over a captured collection, refuses
# any method parameter inside a query, throws NullReferenceException on an
# ExecuteUpdate set-list, and for the queries it does accept emits an invalid
# declaration per entity materialisation
# ("var openWaf.Domain.Security.BlockEntryPrimaryKeyProperties = …").
# scripts/aot-probe.sh reproduces the runtime half; docs/ARCHITECTURE.md carries
# the numbers. A static binary is the right shape for this image, and EF is the
# thing standing in the way, not the Dockerfile.
#
# Stages:
# 1. restore – NuGet layer, cached independently of source changes
# 2. build – self-contained publish for the build architecture, plus the
# /rootfs tree of shared objects the runtime layer will need
# 3. geoip – the database, downloaded by a static curl image
# 4. runtime – scratch
ARG DOTNET_VERSION=10.0
ARG DBIP_VERSION=2026-10
ARG DBIP_EDITION=country
FROM mcr.microsoft.com/dotnet/sdk:${DOTNET_VERSION} AS restore
WORKDIR /src
COPY dotnet/global.json dotnet/Directory.Build.props dotnet/NuGet.Config dotnet/OpenWaf.slnx ./
COPY dotnet/OpenWaf.Domain/OpenWaf.Domain.csproj OpenWaf.Domain/
COPY dotnet/OpenWaf.Infrastructure/OpenWaf.Infrastructure.csproj OpenWaf.Infrastructure/
COPY dotnet/OpenWaf.Edge/OpenWaf.Edge.csproj OpenWaf.Edge/
COPY dotnet/OpenWaf.Tests.Unit/OpenWaf.Tests.Unit.csproj OpenWaf.Tests.Unit/
COPY dotnet/OpenWaf.Tests.Integration/OpenWaf.Tests.Integration.csproj OpenWaf.Tests.Integration/
RUN dotnet restore OpenWaf.Edge/OpenWaf.Edge.csproj
FROM restore AS build
COPY dotnet/ ./
# Predefined ARGs are only substituted in a stage that declares them.
ARG TARGETARCH
# TARGETARCH is supplied by BuildKit; the RID is spelled out from it because
# --self-contained needs one and a scratch layer cannot make up for a wrong one.
RUN case "${TARGETARCH}" in \
amd64) RID=linux-x64 ;; \
arm64) RID=linux-arm64 ;; \
*) printf 'no self-contained RID for architecture %s\n' "${TARGETARCH}" >&2; exit 1 ;; \
esac && \
printf 'self-contained RID: %s\n' "$RID" && \
dotnet publish OpenWaf.Edge/OpenWaf.Edge.csproj \
-c Release \
-r "$RID" \
--self-contained true \
-p:DebugType=none \
-p:DebugSymbols=false \
-o /publish/api \
&& cd /publish/api \
&& rm -f *.pdb \
&& rm -rf BuildHost-net472 BuildHost-netcore
# The runtime layer's /usr, /lib and /etc, assembled from what the apphost and
# the runtime's own shared objects were linked against.
RUN set -eu; \
mkdir -p /rootfs; \
copy_at_path() { \
mkdir -p "/rootfs$(dirname "$1")"; \
cp -L "$1" "/rootfs$1"; \
}; \
# Every absolute path ldd prints, not just the "name => /path" ones: the
# dynamic loader is printed as a bare "/lib/ld-linux-aarch64.so.1 (0x...)"
# line with no arrow, and filtering on the arrow drops exactly the one file
# the kernel needs to start the process. Measured: the first scratch image
# built here started with `exec /app/api/OpenWaf.Edge: no such file or
# directory` — the ENOENT the kernel reports for a missing interpreter.
{ ldd /publish/api/OpenWaf.Edge; \
find /publish/api -name '*.so' -print0 | xargs -0 ldd; } 2>/dev/null \
| awk '{ for (i = 1; i <= NF; i++) if ($i ~ /^\//) print $i }' \
| sort -u > /needed.txt; \
printf 'loader line: %s\n' "$(grep -c 'ld-linux' /needed.txt)"; \
# ldd also lists the kernel's vdso (/dev/vdso/…), which is not a file to
# carry; anything it names that is not a regular file is skipped.
while read -r lib; do [ -f "$lib" ] || continue; copy_at_path "$lib"; done < /needed.txt; \
# dlopen(), so no ldd of anything here shows them: OpenSSL, which
# libSystem.Security.Cryptography.Native.OpenSsl.so probes at first TLS use
# (measured: an image without them starts, warms up, and then answers
# "No usable version of libssl was found" and never turns healthy), and
# getaddrinfo's name-service modules.
for mod in files dns; do \
for f in /lib/*/libnss_${mod}.so.2 /usr/lib/*/libnss_${mod}.so.2; do \
[ -e "$f" ] && copy_at_path "$f"; \
done; \
done; \
for mod in ssl crypto; do \
for f in /lib/*/lib${mod}.so.3 /usr/lib/*/lib${mod}.so.3; do \
[ -e "$f" ] && copy_at_path "$f"; \
done; \
done; \
# The CA bundle: an https upstream or an https alert webhook verifies a
# certificate, and a scratch image has no /etc/ssl to verify against.
copy_at_path /etc/ssl/certs/ca-certificates.crt; \
printf 'shared objects carried: %s\n' "$(find /rootfs/lib /rootfs/lib64 -type f 2>/dev/null | wc -l)"; \
printf 'rootfs bytes: %s\n' "$(du -sk /rootfs | cut -f1)"
RUN mkdir -p /publish/data
FROM curlimages/curl:8.10.1 AS geoip
ARG DBIP_VERSION
ARG DBIP_EDITION
RUN mkdir -p /tmp/geoip && \
curl --fail --silent --show-error --location --retry 3 --retry-delay 2 \
"https://download.db-ip.com/free/dbip-${DBIP_EDITION}-lite-${DBIP_VERSION}.mmdb.gz" \
| gunzip > /tmp/geoip/GeoLite2-Country.mmdb && \
ls -la /tmp/geoip/GeoLite2-Country.mmdb
# No base image at all. Numeric user: there is no /etc/passwd to look a name up
# in, and the volume mount points are staged with that ownership because there
# is no shell here to mkdir with at start-up.
FROM scratch
# WORKDIR is not cosmetic here. A self-contained apphost takes its content root
# from the process's working directory, and a scratch image has no WORKDIR, so
# without this the process starts in / and never finds appsettings.json next to
# it: the reverse proxy then has no routes and every request that should reach
# the upstream answers the edge's own 404. Measured — the first scratch image
# that passed its own HEALTHCHECK still failed 7 of the 368 compose assertions
# for exactly this reason, and the edge said it in its own log: "Request reached
# the end of the middleware pipeline without being handled by application code."
WORKDIR /app/api
COPY --from=build /rootfs/ /
COPY --from=build --chown=1654:1654 /publish/api /app/api
COPY --from=build --chown=1654:1654 /publish/data /app/data
COPY --from=geoip --chown=1654:1654 /tmp/geoip /app/geoip
USER 1654:1654
ENV ASPNETCORE_URLS=http://+:8080 \
ASPNETCORE_FORWARDEDHEADERS_ENABLED=false \
ConnectionStrings__OpenWaf="Data Source=/app/data/openwaf.db" \
Database__Provider=sqlite \
GEOIP_MAXMIND_DB_PATH=/app/geoip/GeoLite2-Country.mmdb \
HEALTHCHECK_PORT=8080
EXPOSE 8080
# The binary is the only probe available — there is no shell, no wget, no curl,
# and not even a coreutils `true` to fall back on.
HEALTHCHECK --interval=30s --timeout=5s --retries=3 --start-period=10s \
CMD ["/app/api/OpenWaf.Edge", "--healthcheck"]
ENTRYPOINT ["/app/api/OpenWaf.Edge"]