# 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"]