Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ RUN npm ci --omit=dev --ignore-scripts && \

# Final image
FROM node:25-slim
RUN apt-get update && apt-get install -y wget openssl ca-certificates gosu && rm -rf /var/lib/apt/lists/* && \
RUN apt-get update && apt-get install -y wget openssl ca-certificates && rm -rf /var/lib/apt/lists/* && \
groupadd -r app && useradd -r -g app -m -d /home/app app
WORKDIR /app
COPY --from=builder /app/dist ./dist
Expand All @@ -69,4 +69,5 @@ ENV DATABASE_URL=file:/app/database/hemmelig.db
HEALTHCHECK --interval=30s --timeout=10s --start-period=10s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:3000/api/health/ready || exit 1

USER app
ENTRYPOINT ["/app/docker-entrypoint.sh"]
8 changes: 3 additions & 5 deletions scripts/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/bin/sh
set -e

# Fix permissions on mounted volumes (runs as root)
chown -R app:app /app/database /app/uploads 2>/dev/null || true

# Run migrations and start app as app user
exec gosu app sh -c 'npx prisma migrate deploy && exec npx tsx server.ts'
# Run migrations and start app
npx prisma migrate deploy
exec npx tsx server.ts
Comment on lines +4 to +6

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Runtime-mounted volume permissions may break database writes and file uploads

The old entrypoint ran as root first, allowing it to chown the data directories before switching to app. Now that the container starts directly as app, any volume mounted at /app/database or /app/uploads at runtime retains the host directory's ownership. If that ownership doesn't match the app user's UID/GID, npx prisma migrate deploy (and subsequent DB writes / file uploads) will fail with permission errors — a silent regression for anyone using named or bind-mount volumes.

Common deployment pattern that will break:

docker run -v /srv/hemmelig/data:/app/database \
           -v /srv/hemmelig/uploads:/app/uploads \
           hemmelig

If /srv/hemmelig/data is owned by root:root or any UID that doesn't match the app system user, the migration step will immediately error out.

Mitigation options (pick one):

  1. Keep a lightweight root → user handoff for volume fixup (most backward-compatible):
🔧 Proposed entrypoint with conditional volume permission fix
 #!/bin/sh
 set -e

-# Run migrations and start app
-npx prisma migrate deploy
-exec npx tsx server.ts
+# Fix ownership of mounted volumes if running as root (e.g. via docker run --user)
+# or if invoked by an orchestrator that maps host UIDs onto the volume.
+if [ "$(id -u)" = "0" ]; then
+  chown -R app:app /app/database /app/uploads
+  exec su-exec app "$0" "$@"
+fi
+
+# Run migrations and start app
+npx prisma migrate deploy
+exec npx tsx server.ts

(requires adding su-exec — a tiny, static binary — to the final image instead of the heavier gosu)

  1. Document the required host permissions and expose the UID/GID as build args so operators can match their host directories (see Dockerfile comment below).
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@scripts/docker-entrypoint.sh` around lines 4 - 6, The entrypoint now runs as
the non-root "app" user and fails when runtime-mounted volumes under
/app/database or /app/uploads are owned by the host; modify
scripts/docker-entrypoint.sh to perform a conditional chown/chgrp of
/app/database and /app/uploads when running as root (or detect mismatched
ownership) before running "npx prisma migrate deploy", then drop privileges to
the "app" user to run "exec npx tsx server.ts" (use a lightweight su-exec or
gosu wrapper like su-exec to switch from root to the app user); alternatively,
if you prefer not to change permissions at runtime, add documentation and
Dockerfile build args exposing APP_UID/APP_GID so operators can pre-set host
directory ownership to match the "app" user.

Loading