Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

feat: run the OpenGym web image as non-root

Matt Van Horn05c39396d603
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index f2c470392..ca17b597e 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -478,10 +478,13 @@ build:web-check:
script:
- docker build -f web/Dockerfile -t opengym-web:check .
# It serves the app shell, and the shell is this build (the Coach setup is in the bundle).
+ # The master is uid 101, on port 80 — not the base image's EXPOSE 8080.
- |
set -eu
docker run -d --name web-check -e BACKEND=127.0.0.1 -e PORT=3000 opengym-web:check
sleep 2
+ [ "$(docker exec web-check id -u)" = 101 ]
+ docker exec web-check grep -q '^Uid:[[:space:]]*101[[:space:]]' /proc/1/status
docker exec web-check wget -qO- http://127.0.0.1/ | grep -q '<div id="root">'
docker exec web-check sh -c 'grep -l "coach" /usr/share/nginx/html/assets/index-*.js >/dev/null'
docker rm -f web-check
diff --git a/frontend/src/lib/api-base.test.js b/frontend/src/lib/api-base.test.js
index 44cfe0c53..ea9861055 100644
--- a/frontend/src/lib/api-base.test.js
+++ b/frontend/src/lib/api-base.test.js
@@ -223,4 +223,25 @@ describe('the web container refuses a BASE_PATH it cannot serve', () => {
const dockerfile = readFileSync(join(WEB, 'Dockerfile'), 'utf8')
expect(dockerfile).toMatch(/^COPY --chmod=755 web\/05-check-base-path\.sh \/docker-entrypoint\.d\/$/m)
})
+
+ it('runs nginx as uid 101 and still listens on port 80', () => {
+ // nginx:alpine starts its master as root so it can create /var/cache/nginx, and a non-root
+ // master there dies on mkdir. The unprivileged base is uid 101, but apk cannot upgrade as
+ // that user, so the upgrade runs as root and 101 is the last USER — the entrypoint and the
+ // master both stay there. The listen port stays 80: compose, the probes and the healthcheck
+ // all address NGINX_PORT. The base image's EXPOSE 8080 is only metadata.
+ const dockerfile = readFileSync(join(WEB, 'Dockerfile'), 'utf8')
+ expect(dockerfile).toMatch(/^FROM --platform=\$BUILDPLATFORM node:22-alpine AS build$/m)
+ expect(dockerfile).toMatch(/^FROM nginxinc\/nginx-unprivileged:alpine$/m)
+ const users = [...dockerfile.matchAll(/^USER \S+$/gm)].map(m => m[0])
+ expect(users.at(-1)).toBe('USER 101')
+ const root = dockerfile.search(/^USER root$/m)
+ const upgrade = dockerfile.search(/^RUN apk upgrade --no-cache/m)
+ const finalUser = dockerfile.lastIndexOf('USER 101')
+ expect(root).toBeGreaterThanOrEqual(0)
+ expect(upgrade).toBeGreaterThan(root)
+ expect(finalUser).toBeGreaterThan(upgrade)
+ expect(dockerfile).toMatch(/^ENV NGINX_PORT=80$/m)
+ expect(dockerfile).toMatch(/^COPY web\/nginx\.conf\.template \/etc\/nginx\/templates\/default\.conf\.template$/m)
+ })
})
diff --git a/web/Dockerfile b/web/Dockerfile
index 79303ea27..43186ba57 100644
--- a/web/Dockerfile
+++ b/web/Dockerfile
@@ -26,9 +26,32 @@ COPY frontend/ ./
ARG APP_BUILD=""
RUN APP_BUILD="$APP_BUILD" npm run build
-FROM nginx:alpine
+FROM nginxinc/nginx-unprivileged:alpine
# Same reason as in api/Dockerfile: alpine fixes land faster than the nginx image is rebuilt.
-RUN apk upgrade --no-cache
+# The base's last instruction is USER 101, and apk then exits 99 — it cannot open its log as
+# that user. Upgrade as root, then switch back. 101 has to be the last USER in this file so
+# the entrypoint and the nginx master both stay uid 101. It can bind port 80; the base
+# image's EXPOSE 8080 is metadata, and nothing here listens on it.
+#
+# An upgrade can put the packaged nginx.conf back (a `user` directive, pid under /run) and
+# recreate /var/cache/nginx as root, which is the mkdir a non-root master dies on. Restore
+# the layout the base established, still as root, and idempotently: drop a user directive
+# if one is present, point pid at /tmp/nginx.pid, and insert the /tmp temp paths only when
+# client_body_temp_path is absent — a second insert duplicates them and nginx refuses the
+# config. chown/chmod cover /var/cache/nginx and /etc/nginx only, so uid 101 can render the
+# template. The static tree keeps the default modes; uid 101 only reads it.
+USER root
+RUN apk upgrade --no-cache \
+ && sed -i '/^[[:space:]]*user[[:space:]]/d' /etc/nginx/nginx.conf \
+ && sed -i 's,\(/var\)\{0\,1\}/run/nginx.pid,/tmp/nginx.pid,' /etc/nginx/nginx.conf \
+ && if ! grep -q 'client_body_temp_path' /etc/nginx/nginx.conf; then \
+ sed -i "/^http {/a \ proxy_temp_path /tmp/proxy_temp;\n client_body_temp_path /tmp/client_temp;\n fastcgi_temp_path /tmp/fastcgi_temp;\n uwsgi_temp_path /tmp/uwsgi_temp;\n scgi_temp_path /tmp/scgi_temp;\n" /etc/nginx/nginx.conf; \
+ fi \
+ && chown -R nginx:root /var/cache/nginx /etc/nginx \
+ && chmod -R g+w /var/cache/nginx /etc/nginx \
+ && grep -Eq '^[[:space:]]*pid[[:space:]]+/tmp/nginx\.pid;' /etc/nginx/nginx.conf \
+ && ! grep -Eq '^[[:space:]]*user[[:space:]]' /etc/nginx/nginx.conf
+USER 101
ARG VERSION=dev
ARG VCS_REF=dev
ARG BUILD_DATE=unknown