From 8b7bc0a650794bcd179c679456ca680c71e05dd9 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 14 Aug 2026 19:22:10 +0200 Subject: [PATCH] fix(docker): run on read-only filesystems, IPv6-less hosts and cpu-limited containers (#556) Five fixes to the Docker image, found while running it-tools on OpenShift, where containers run as an arbitrary UID. No application code is changed. Start on a read-only filesystem. `docker run --read-only` and Kubernetes readOnlyRootFilesystem crash-loop the container, because 20-envsubst-on-templates.sh checks its output directory with `[ ! -w $dir ]`. That only reads permission bits, so a read-only mount passes the check, the write fails with EROFS and `set -e` stops the entrypoint. 19-skip-envsubst-if-readonly.envsh now tests the directory with a real write and, on failure, points NGINX_ENVSUBST_TEMPLATE_DIR at a missing path so the envsubst script exits early. That is only safe if the config in the image is already correct, and today it is nginx's stock welcome config, so the Dockerfile renders the template at build time as well. Writable setups are unchanged: the write test passes, nothing is exported and the config is rendered at startup as before, so a custom $PORT still works. Skip IPv6 when the host has none. nginx.conf always listens on IPv6, so where IPv6 is off nginx fails with `socket() [::]:8080 failed (97: Address family not supported by protocol)`. 25-remove-ipv6-listen-if-unavailable.sh comments those lines out, using the same /proc/net/if_inet6 check as nginx's own 10-listen-on-ipv6-by-default.sh, and exits at once when IPv6 works. It probes the directory before editing, because `sed -i` rewrites through a temporary file there, so a writable file in a read-only directory is not enough, and it tolerates a failed edit so it can never stop the entrypoint. Match workers to the cpu limit. `worker_processes auto` counts the host's cores, not the container's cpu limit, so the image starts 256 workers on a 256-core host in any container size. `docker run -m 192m` is enough to be OOM-killed at startup. Setting NGINX_ENTRYPOINT_WORKER_PROCESSES_AUTOTUNE enables the script nginx already ships. Containers without a cpu limit keep one worker per core. This is the only change that affects a deployment that works today. Return 404 for missing assets. All paths fall back to index.html, so a missing build file returns 200 and text/html, and the browser reports a confusing MIME error instead of a 404. /assets/ now skips the SPA fallback. Sub-folder requests such as /it-tools/assets/... still match `location /`, so those deployments do not change. Stop remapping js. nginx.conf includes mime.types, which already maps js, then maps it again, so every startup logs a duplicate extension warning. mime.types has no entry for mjs, so mjs stays and js goes. Signed-off-by: Sebastian Danielsson --- Dockerfile | 19 +++++++ .../19-skip-envsubst-if-readonly.envsh | 29 ++++++++++ .../25-remove-ipv6-listen-if-unavailable.sh | 55 +++++++++++++++++++ nginx.conf | 11 +++- 4 files changed, 113 insertions(+), 1 deletion(-) create mode 100755 docker-entrypoint.d/19-skip-envsubst-if-readonly.envsh create mode 100755 docker-entrypoint.d/25-remove-ipv6-listen-if-unavailable.sh diff --git a/Dockerfile b/Dockerfile index 63d1d843..f92e3f67 100644 --- a/Dockerfile +++ b/Dockerfile @@ -33,7 +33,26 @@ ENV BASE_URL=${BASE_URL} COPY --from=build-stage /app/dist /usr/share/nginx/html COPY nginx.conf /etc/nginx/templates/default.conf.template +COPY docker-entrypoint.d/ /docker-entrypoint.d/ ENV PORT=8080 + +# nginx defaults to `worker_processes auto`, which counts the host's cores and +# ignores the container's cpu limit. On a large host that is hundreds of +# workers in a small container: `docker run -m 192m` is enough to get the +# image OOM-killed on startup. Let the entrypoint size the pool from the cgroup +# cpu quota instead. Uncapped containers still get one worker per core. +ENV NGINX_ENTRYPOINT_WORKER_PROCESSES_AUTOTUNE=1 + +# Render the template once here as well. The entrypoint normally re-renders it +# at startup, so a custom $PORT keeps working, but it cannot do so on a +# read-only filesystem (see docker-entrypoint.d/19-skip-envsubst-if-readonly.envsh). +# Without this the image would fall back to the stock welcome config from the +# base image: no SPA fallback, so deep links 404 on refresh, and no COOP/COEP, +# so the WebAssembly-backed tools stop working. +RUN envsubst '${PORT}' \ + < /etc/nginx/templates/default.conf.template \ + > /etc/nginx/conf.d/default.conf + EXPOSE $PORT CMD ["nginx", "-g", "daemon off;"] diff --git a/docker-entrypoint.d/19-skip-envsubst-if-readonly.envsh b/docker-entrypoint.d/19-skip-envsubst-if-readonly.envsh new file mode 100755 index 00000000..802834c0 --- /dev/null +++ b/docker-entrypoint.d/19-skip-envsubst-if-readonly.envsh @@ -0,0 +1,29 @@ +# vim:sw=4:ts=4:et +# +# 20-envsubst-on-templates.sh guards its output directory with `[ ! -w $dir ]`, +# which only inspects permission bits. It therefore does not notice a read-only +# *mount*: with `docker run --read-only` or a Kubernetes readOnlyRootFilesystem, +# /etc/nginx/conf.d still looks writable, envsubst runs anyway, the redirect +# fails with EROFS and `set -e` takes the container down. The image +# crash-loops on every start. +# +# Probe the directory with a real write. If it fails, point the template +# directory at a path that does not exist so 20-envsubst-on-templates.sh +# returns early, and let nginx use the config rendered into the image at build +# time instead. Sourced, not executed, so the export reaches later scripts. +# +# On a writable filesystem -- every deployment that works today -- the probe +# succeeds and nothing is changed. + +_envsubst_output_dir="${NGINX_ENVSUBST_OUTPUT_DIR:-/etc/nginx/conf.d}" +_envsubst_probe="$_envsubst_output_dir/.readonly-probe.$$" + +if [ -d "${NGINX_ENVSUBST_TEMPLATE_DIR:-/etc/nginx/templates}" ] \ + && ! (touch "$_envsubst_probe" 2>/dev/null && rm -f "$_envsubst_probe" 2>/dev/null); then + if [ -z "${NGINX_ENTRYPOINT_QUIET_LOGS:-}" ]; then + echo "19-skip-envsubst-if-readonly.envsh: info: $_envsubst_output_dir is not writable, keeping the config baked into the image" + fi + export NGINX_ENVSUBST_TEMPLATE_DIR=/nonexistent-read-only-filesystem +fi + +unset _envsubst_output_dir _envsubst_probe diff --git a/docker-entrypoint.d/25-remove-ipv6-listen-if-unavailable.sh b/docker-entrypoint.d/25-remove-ipv6-listen-if-unavailable.sh new file mode 100755 index 00000000..9c3f45f0 --- /dev/null +++ b/docker-entrypoint.d/25-remove-ipv6-listen-if-unavailable.sh @@ -0,0 +1,55 @@ +#!/bin/sh +# vim:sw=4:ts=4:et +# +# nginx.conf listens on both IPv4 and IPv6. On a host or container runtime with +# IPv6 disabled, nginx refuses to start: +# +# socket() [::]:8080 failed (97: Address family not supported by protocol) +# +# Drop the IPv6 listen directives in that case. This runs after +# 20-envsubst-on-templates.sh so it edits the rendered config, and it is a +# no-op wherever IPv6 is available, which is the case the image already +# handles today. + +set -e + +ME=$(basename "$0") + +entrypoint_log() { + if [ -z "${NGINX_ENTRYPOINT_QUIET_LOGS:-}" ]; then + echo "$@" + fi +} + +# Same check the stock 10-listen-on-ipv6-by-default.sh uses. +if [ -f "/proc/net/if_inet6" ]; then + exit 0 +fi + +output_dir="${NGINX_ENVSUBST_OUTPUT_DIR:-/etc/nginx/conf.d}" + +[ -d "$output_dir" ] || exit 0 + +# `sed -i` rewrites through a temporary file in the directory, so the directory +# has to be writable, not only the file. Probe it with a real write, the same +# way 19-skip-envsubst-if-readonly.envsh does. +probe="$output_dir/.ipv6-probe.$$" +if ! (touch "$probe" 2>/dev/null && rm -f "$probe" 2>/dev/null); then + entrypoint_log "$ME: info: ipv6 not available, but $output_dir is not writable (read-only file system?), leaving the config alone" + exit 0 +fi + +for conf in "$output_dir"/*.conf; do + [ -f "$conf" ] || continue + grep -q -E '^[[:space:]]*listen[[:space:]]+\[::\]' "$conf" || continue + + # An individual file can still be mounted read-only inside a writable + # directory, so a failed edit must not take the entrypoint down with it. + if sed -i -E 's/^([[:space:]]*)(listen[[:space:]]+\[::\].*)$/\1# \2 # disabled by '"$ME"': no IPv6 on this host/' "$conf" 2>/dev/null; then + entrypoint_log "$ME: info: ipv6 not available, disabled IPv6 listen in $conf" + else + entrypoint_log "$ME: info: ipv6 not available, but can not modify $conf, leaving it alone" + fi +done + +exit 0 diff --git a/nginx.conf b/nginx.conf index 373805ff..7094e4fb 100644 --- a/nginx.conf +++ b/nginx.conf @@ -16,7 +16,16 @@ server { include /etc/nginx/mime.types; types { - application/javascript js mjs; + # mime.types already maps js, remapping it here only emits a + # "duplicate extension" warning on every startup. + application/javascript mjs; + } + + # Hashed build output. Without this, a missing chunk falls through to + # index.html below and is served as text/html with a 200, which the browser + # rejects with an opaque MIME error instead of a plain 404. + location /assets/ { + try_files $uri =404; } location / {