mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-22 14:01:42 +03:00
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 <sebastian.danielsson@proton.me>
59 lines
2.1 KiB
Docker
59 lines
2.1 KiB
Docker
# build stage
|
|
FROM --platform=$BUILDPLATFORM node:24-alpine AS build-stage
|
|
# Set environment variables for non-interactive npm installs
|
|
ENV NPM_CONFIG_LOGLEVEL=warn
|
|
ENV CI=true
|
|
|
|
RUN apk add --update python3 make g++\
|
|
&& rm -rf /var/cache/apk/*
|
|
|
|
WORKDIR /app
|
|
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./
|
|
COPY patches patches
|
|
COPY stubs stubs
|
|
RUN npm install -g pnpm@11 && pnpm i --ignore-scripts --frozen-lockfile
|
|
COPY . .
|
|
ARG BASE_URL
|
|
ENV BASE_URL=${BASE_URL}
|
|
ARG VITE_AVAILABLE_LOCALES
|
|
ENV VITE_AVAILABLE_LOCALES=${VITE_AVAILABLE_LOCALES}
|
|
ENV VITE_VERCEL_ENV=production
|
|
RUN pnpm build
|
|
|
|
# production stage
|
|
FROM nginxinc/nginx-unprivileged:stable-alpine AS production-stage
|
|
|
|
LABEL maintainer="ShareVB <sharevb@gmail.com>" \
|
|
org.opencontainers.image.authors="ShareVB <sharevb@gmail.com>"
|
|
LABEL org.opencontainers.image.source=github.com/sharevb/it-tools
|
|
|
|
ENV VITE_VERCEL_ENV=production
|
|
ARG BASE_URL
|
|
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;"]
|