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 / {