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.

fix(k8s): the web container listens on 8080 in the Helm chart and the example manifests

Duarte Santos407b29c72fad
diff --git a/api/test/k8s-web-port.test.js b/api/test/k8s-web-port.test.js
new file mode 100644
index 000000000..f8add8968
--- /dev/null
+++ b/api/test/k8s-web-port.test.js
@@ -0,0 +1,45 @@
+/* The web image runs as uid 101. Docker lets it bind port 80 (ip_unprivileged_port_start=0 in
+ the container); most Kubernetes runtimes do not, and nginx then dies on bind(). The image's
+ own default stays 80 so compose setups keep working, and the Helm chart and the example
+ manifests set an unprivileged port themselves — the container port, the probes and the
+ Service following it. */
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import fs from 'node:fs';
+import path from 'node:path';
+import { fileURLToPath } from 'node:url';
+
+const ROOT = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', '..');
+const read = f => fs.readFileSync(path.join(ROOT, f), 'utf8');
+
+test('the image keeps NGINX_PORT=80 for Docker', () => {
+ assert.match(read('web/Dockerfile'), /^ENV NGINX_PORT=80$/m);
+});
+
+test('kubernetes/: the web container listens on an unprivileged port, and everything follows it', () => {
+ const dep = read('kubernetes/deployment.yaml');
+ const web = dep.slice(dep.indexOf('- name: web'));
+ const port = Number(/- name: NGINX_PORT\s+value: "(\d+)"/.exec(web)?.[1]);
+ assert.ok(port >= 1024, 'NGINX_PORT ' + port);
+ assert.match(web, new RegExp(`- containerPort: ${port}\\s+name: web`));
+ for (const probe of ['readinessProbe', 'livenessProbe']) {
+ const block = web.slice(web.indexOf(probe), web.indexOf(probe) + 120);
+ assert.match(block, /port: web\b/, probe);
+ }
+ assert.match(read('kubernetes/service.yaml'), /targetPort: web\b/);
+});
+
+test('helm/: NGINX_PORT and the container port are web.service.port, unprivileged by default', () => {
+ const values = read('helm/values.yaml');
+ const webBlock = values.slice(values.indexOf('\nweb:'));
+ const port = Number(/\n service:[\s\S]*?\n port: (\d+)/.exec(webBlock)?.[1]);
+ assert.ok(port >= 1024, 'web.service.port ' + port);
+ assert.match(read('helm/templates/configmap.yaml'), /NGINX_PORT: \{\{ \.Values\.web\.service\.port \| quote \}\}/);
+ const dep = read('helm/templates/deployment.yaml');
+ assert.match(dep, /- name: web\s+containerPort: \{\{ \.Values\.web\.service\.port \}\}/);
+ assert.match(read('helm/templates/service.yaml'), /targetPort: web\b/);
+ for (const probe of ['livenessProbe', 'readinessProbe']) {
+ const block = webBlock.slice(webBlock.indexOf(probe), webBlock.indexOf(probe) + 80);
+ assert.match(block, /port: web\b/, probe);
+ }
+});
diff --git a/docs/SELF_HOSTING.md b/docs/SELF_HOSTING.md
index b92245b02..df5bdb53c 100644
--- a/docs/SELF_HOSTING.md
+++ b/docs/SELF_HOSTING.md
@@ -449,6 +449,11 @@ BASE_PATH= # subpath openGym is served under, e.g. /gym — see
SESSION_DAYS=90 # how long a sign-in lasts
```
+`NGINX_PORT` stays 80 under Docker. The web container runs as an unprivileged user (uid 101),
+which can bind a port below 1024 only because Docker sets `net.ipv4.ip_unprivileged_port_start=0`
+in the container. On another runtime (Kubernetes, rootless Podman) set `NGINX_PORT` to 1024 or
+higher, e.g. 8080, or set that sysctl yourself; the Helm chart and `kubernetes/` use 8080.
+
`RESOLVER` only matters off Docker. nginx re-resolves `BACKEND` on every `/api` request so a
recreated API container does not leave it proxying to a dead IP, and `127.0.0.11` is where
Docker answers those lookups. Nothing listens there on another runtime, and an unreachable
diff --git a/docs/SELF_HOSTING_KUBERNETES.md b/docs/SELF_HOSTING_KUBERNETES.md
index 657ed2cd9..76eac6340 100644
--- a/docs/SELF_HOSTING_KUBERNETES.md
+++ b/docs/SELF_HOSTING_KUBERNETES.md
@@ -25,6 +25,11 @@ kubectl apply -k kubernetes/
Notes:
+- The web container listens on 8080 (`NGINX_PORT`), not on the image's default 80: it runs as
+ uid 101, which can bind a port below 1024 only where the runtime sets
+ `net.ipv4.ip_unprivileged_port_start=0`, as Docker does and most Kubernetes runtimes do not.
+ The Service still answers on port 80 and targets the container's `web` port by name. Writing
+ your own manifests, keep `NGINX_PORT` at 1024 or higher, or set that sysctl on the pod.
- The manifests create and use the `fitness` namespace (`kubernetes/namespace.yaml`, set on every
resource by `kubernetes/kustomization.yaml`; rename it in both), and a Gateway
called `eg` in `envoy-gateway-system` with an `https` listener; change both to match your
diff --git a/helm/README.md b/helm/README.md
index 2900db0a9..80874e5ca 100644
--- a/helm/README.md
+++ b/helm/README.md
@@ -104,7 +104,7 @@ Browsers allow passkeys over plain `http://` only on `localhost`.
|---|---|---|
| `api.env.*` | ConfigMap `<release>-api` | Every setting from `.env.example`, under the same name. An empty value is left out, so the app default applies. `PORT` comes from `api.service.port`. |
| `api.secretEnv.*` | Secret `<release>-api` | `ADMIN_UIDS`. You can add more keys; each becomes an env var. |
-| `web.env.*` | ConfigMap `<release>-web` | `BASE_PATH`, `MEDIA_UPLOAD_MAX`, `CF_CONNECTING_IP`. The chart sets `NGINX_PORT`, `PORT` and `BACKEND=127.0.0.1` itself. |
+| `web.env.*` | ConfigMap `<release>-web` | `BASE_PATH`, `MEDIA_UPLOAD_MAX`, `CF_CONNECTING_IP`. The chart sets `NGINX_PORT`, `PORT` and `BACKEND=127.0.0.1` itself; `NGINX_PORT` is `web.service.port`, 8080 (the image runs as uid 101, which cannot bind 80 on most Kubernetes runtimes). |
`TRUST_PROXY` stays off on purpose. The web container overwrites `X-Forwarded-For` with the
address it was reached from, which here is the ingress or gateway pod. Turning `TRUST_PROXY` on
diff --git a/helm/templates/deployment.yaml b/helm/templates/deployment.yaml
index 4235fc3b3..073ab8a91 100644
--- a/helm/templates/deployment.yaml
+++ b/helm/templates/deployment.yaml
@@ -66,6 +66,8 @@ spec:
envFrom:
- configMapRef:
name: {{ include "helm.fullname" . }}-web
+ # NGINX_PORT (the ConfigMap) is the same web.service.port, 8080 unless changed: uid 101
+ # cannot bind 80 here. The probes and the Service's targetPort name this port.
ports:
- name: web
containerPort: {{ .Values.web.service.port }}
diff --git a/helm/values.yaml b/helm/values.yaml
index 6e131a184..70bb45939 100644
--- a/helm/values.yaml
+++ b/helm/values.yaml
@@ -153,7 +153,11 @@ web:
pullPolicy: IfNotPresent
tag: 1.3.9
service:
- port: 80
+ # The port nginx listens on inside the pod: NGINX_PORT and the container's `web` port, which
+ # the Service and the probes follow by name. Not the image's default 80: the image runs as
+ # uid 101, which can bind a port below 1024 only where the runtime sets
+ # net.ipv4.ip_unprivileged_port_start=0. Docker does; most Kubernetes runtimes do not.
+ port: 8080
# Rendered into the web ConfigMap. NGINX_PORT, PORT and BACKEND (127.0.0.1, same pod) are set
# by the chart.
env:
diff --git a/kubernetes/deployment.yaml b/kubernetes/deployment.yaml
index ad911b42a..4e5045965 100644
--- a/kubernetes/deployment.yaml
+++ b/kubernetes/deployment.yaml
@@ -67,8 +67,11 @@ spec:
image: ghcr.io/duartesantos8/opengym-web:1.3.10 # same version as the api container
imagePullPolicy: IfNotPresent
env:
+ # Not the image's default 80: the image runs as uid 101, which can bind a port below
+ # 1024 only where the runtime sets net.ipv4.ip_unprivileged_port_start=0 (Docker
+ # does, most Kubernetes runtimes do not). The Service targets this port by name.
- name: NGINX_PORT
- value: "80"
+ value: "8080"
# Both containers share the pod's network, so the API is on localhost. An IP here
# also means nginx never needs RESOLVER (Docker's DNS by default) to find it.
- name: BACKEND
@@ -76,17 +79,17 @@ spec:
- name: PORT
value: "3000"
ports:
- - containerPort: 80
+ - containerPort: 8080
name: web
protocol: TCP
readinessProbe:
httpGet:
path: /
- port: 80
+ port: web
livenessProbe:
httpGet:
path: /
- port: 80
+ port: web
initialDelaySeconds: 30
# No volume: the exercise stills and animations (180 px) are part of the web image since
# v1.4.0. They are licensed from Gym visual for openGym only; see NOTICE.md.
diff --git a/web/Dockerfile b/web/Dockerfile
index 68155dab3..464f93d31 100644
--- a/web/Dockerfile
+++ b/web/Dockerfile
@@ -30,8 +30,10 @@ FROM nginxinc/nginx-unprivileged:alpine
# Same reason as in api/Dockerfile: alpine fixes land faster than the nginx image is rebuilt.
# 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.
+# the entrypoint and the nginx master both stay uid 101. It can bind port 80 under Docker,
+# which sets net.ipv4.ip_unprivileged_port_start=0; the base image's EXPOSE 8080 is metadata,
+# and nothing here listens on it. Another runtime (most Kubernetes ones, Podman rootless)
+# needs NGINX_PORT=1024 or higher, or that sysctl: helm/ and kubernetes/ use 8080.
#
# 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