fix(docker): the container starts on large volumes and under runAsUser 1000

The entrypoint no longer runs chown -R over data and uploads on every start;
it only hands over files that are not node's yet, and logs when it does.
Started as a non-root user, it skips chown and gosu and runs Node directly.
The chart exposes podSecurityContext and securityContext for that setup.
This commit is contained in:
Maurice
2026-11-01 10:49:28 +01:00
parent 8692b8790c
commit 51b991f851
7 changed files with 128 additions and 5 deletions
+8
View File
@@ -1274,6 +1274,14 @@ jobs:
--set httpRoute.enabled=true \
--set 'httpRoute.parentRefs[0].name=gateway' \
> "$out/http-route.yaml"
helm template trek charts/trek \
--set podSecurityContext.runAsUser=1000 \
--set podSecurityContext.runAsGroup=1000 \
--set podSecurityContext.runAsNonRoot=true \
--set podSecurityContext.seccompProfile.type=RuntimeDefault \
--set securityContext.allowPrivilegeEscalation=false \
--set 'securityContext.capabilities.drop[0]=ALL' \
> "$out/non-root.yaml"
helm template trek charts/trek \
--set ingress.enabled=true \
--set fullnameOverride=custom-trek \
+1
View File
@@ -56,4 +56,5 @@ See `values.yaml` for more options.
- `COOKIE_SECURE` is auto-derived (on when `NODE_ENV=production` or `FORCE_HTTPS=true`). Set `env.COOKIE_SECURE: "false"` only during local testing without TLS. **Not recommended for production.**
- Web Push needs no chart settings: TREK generates its key pair on first start and keeps it in the database on the data PVC. To bring your own pair, set `env.VAPID_PUBLIC_KEY` and `secretEnv.VAPID_PRIVATE_KEY` together (and optionally `env.VAPID_SUBJECT`); changing the pair later means every device has to turn push on again.
- Set `env.OIDC_DISCOVERY_URL` to override the auto-constructed OIDC discovery endpoint. Required for providers (e.g. Authentik) that expose it at a non-standard path.
- `podSecurityContext` defaults to `fsGroup: 1000` with `fsGroupChangePolicy: OnRootMismatch`, and `securityContext` (the container's) is empty. The container then starts as root, hands any file under `/app/data` and `/app/uploads` that is not owned by `node` yet to `node`, and drops to `node`. To run as `node` from the start, for example under the `restricted` Pod Security Standard, set `podSecurityContext.runAsUser: 1000` and `runAsGroup: 1000`; the image then skips that step and the privilege drop. See the example in `values.yaml`.
- Use `extraVolumes` and `extraVolumeMounts` to add your own volumes to the pod. To give the booking import more KItinerary extractors, mount a ConfigMap at `/usr/local/share/kitinerary/extractors`, with one `.json` and one `.js` key for each extractor; see the example in `values.yaml`.
+7 -1
View File
@@ -29,12 +29,18 @@ spec:
- name: {{ .name }}
{{- end }}
{{- end }}
{{- with .Values.podSecurityContext }}
securityContext:
fsGroup: 1000
{{- toYaml . | nindent 8 }}
{{- end }}
containers:
- name: trek
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
{{- with .Values.securityContext }}
securityContext:
{{- toYaml . | nindent 12 }}
{{- end }}
{{- with .Values.resources }}
resources:
{{- toYaml . | nindent 12 }}
+28
View File
@@ -10,6 +10,34 @@ image:
# processes on one host, which network filesystems do not provide.
updateStrategy: Recreate
# Pod-level security context. fsGroup gives the node user (uid/gid 1000) group access
# to the data and uploads volumes; OnRootMismatch lets the kubelet skip its recursive
# permission walk once the volume root already matches, so a big PVC does not hold up
# every start.
#
# By default the container starts as root, hands any files that are not node's yet to
# node, and then drops to node itself. To start it as node straight away (needed under
# the "restricted" Pod Security Standard), add the user here and lock the container
# down below; the image then skips the ownership step and the privilege drop:
# podSecurityContext:
# fsGroup: 1000
# fsGroupChangePolicy: OnRootMismatch
# runAsUser: 1000
# runAsGroup: 1000
# runAsNonRoot: true
# seccompProfile:
# type: RuntimeDefault
# securityContext:
# allowPrivilegeEscalation: false
# capabilities:
# drop: [ALL]
podSecurityContext:
fsGroup: 1000
fsGroupChangePolicy: OnRootMismatch
# Security context of the trek container itself. Empty by default.
securityContext: {}
# Optional image pull secrets for private registries
imagePullSecrets: []
# - name: my-registry-secret
+21 -4
View File
@@ -20,11 +20,28 @@ if [ ! -f /app/server/dist/index.js ] || [ ! -d /app/node_modules/tsconfig-paths
exit 1
fi
# Best effort: a read-only host filesystem or a locked-down bind mount must keep
# booting, exactly as it did before.
chown -R node:node /app/data /app/uploads 2>/dev/null || true
# cd into server/ so tsconfig-paths/register finds tsconfig.json and
# ../node_modules resolves correctly.
cd /app/server
# Already started as a non-root user (Kubernetes runAsUser, docker --user): there
# is nothing to chown and no privilege to drop, and gosu would only fail to switch
# users. Run Node as whoever we are; the volumes are that user's to provide.
if [ "$(id -u)" != "0" ]; then
exec node --require tsconfig-paths/register dist/index.js
fi
# Hand the mounted data dirs to node, touching only what is not node's yet. A
# blanket `chown -R` rewrote every inode on every start, which on a large or
# network-backed volume (NFS, CephFS, Longhorn) kept the container busy for
# minutes before Node printed its first line (#2918). Best effort: a read-only
# host filesystem or a locked-down bind mount must keep booting.
for dir in /app/data /app/uploads; do
[ -d "$dir" ] || continue
if [ -n "$(find "$dir" \( ! -user node -o ! -group node \) -print -quit 2>/dev/null)" ]; then
echo "Correcting ownership of $dir for the node user..."
find "$dir" \( ! -user node -o ! -group node \) -exec chown -h node:node {} + 2>/dev/null || true
fi
done
exec gosu node node --require tsconfig-paths/register dist/index.js
@@ -49,4 +49,37 @@ describe('entrypoint script', () => {
it('keeps the volume-over-/app preflight wording the wiki quotes', () => {
expect(source).toContain('FATAL: TREK application files are missing from the image.');
});
// A blanket `chown -R` over the volumes rewrote every inode on every start; on a
// large or network-backed PVC the container sat there for minutes without a log
// line, which looked like gosu hanging (#2918).
// Only the commands count; the comments are free to talk about the old chown.
const code = source
.split('\n')
.filter((line) => !line.trimStart().startsWith('#'))
.join('\n');
it('never chowns the volumes recursively and unconditionally', () => {
expect(code).not.toMatch(/chown\s+-R/);
expect(code).toContain('! -user node -o ! -group node');
expect(code).toContain('Correcting ownership of $dir for the node user...');
});
// Under runAsUser: 1000 there is no privilege to drop and gosu cannot switch
// users, so the non-root branch has to exec Node itself before any chown or gosu.
it('starts Node directly, without chown or gosu, when not running as root', () => {
const nonRootCheck = code.indexOf('if [ "$(id -u)" != "0" ]; then');
expect(nonRootCheck).toBeGreaterThan(-1);
const branchEnd = code.indexOf('\nfi\n', nonRootCheck);
expect(branchEnd).toBeGreaterThan(nonRootCheck);
const branch = code.slice(nonRootCheck, branchEnd);
expect(branch).toContain('exec node --require tsconfig-paths/register dist/index.js');
expect(branch).not.toContain('gosu');
expect(code.indexOf('chown')).toBeGreaterThan(branchEnd);
expect(code.indexOf('gosu')).toBeGreaterThan(branchEnd);
});
it('still drops to node through gosu when started as root', () => {
expect(source).toContain('exec gosu node node --require tsconfig-paths/register dist/index.js');
});
});
+30
View File
@@ -190,6 +190,36 @@ persistence:
size: 1Gi # uploaded files — increase if you expect large media uploads
```
### Security Context
```yaml
podSecurityContext:
fsGroup: 1000
fsGroupChangePolicy: OnRootMismatch
securityContext: {}
```
With these defaults the container starts as root, gives every file under `/app/data` and `/app/uploads` that does not belong to the `node` user (uid 1000) to `node`, and then drops to `node`. Files that already belong to `node` are left alone, so a large uploads volume does not slow down the start. If ownership has to be corrected, the log says `Correcting ownership of /app/uploads for the node user...` before TREK's own start-up lines.
To run TREK as `node` from the first process on, for example in a namespace that enforces the `restricted` Pod Security Standard, set the user in the pod security context. The image notices it is not root and starts Node directly, with no ownership step and no privilege drop:
```yaml
podSecurityContext:
fsGroup: 1000
fsGroupChangePolicy: OnRootMismatch
runAsUser: 1000
runAsGroup: 1000
runAsNonRoot: true
seccompProfile:
type: RuntimeDefault
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop: [ALL]
```
The volumes then have to be writable by uid 1000 or gid 1000. `fsGroup` takes care of that on storage that supports it; on NFS and other volumes the kubelet does not relabel, give the export to uid 1000 yourself. You no longer need to override the container command for this.
### Resource Limits
```yaml