From a390643458b498218905a26211d8fb3157e6627d Mon Sep 17 00:00:00 2001 From: yiling Date: Tue, 23 Jun 2026 09:46:56 +0800 Subject: [PATCH] feat(docker): add root entrypoint with privilege dropping for data volumes This updates the Dockerfile and adds a scripts/docker-entrypoint.sh script. Under native Linux (like Debian), when docker-compose mounts a host folder as a volume (e.g. `./data:/app/data`), and the folder doesn't exist, the docker daemon creates the folder as root-owned (`root:root`). Because the container runs as a non-root user `bun`, it gets permission denied (`EACCES`) when trying to write to the data directory. To resolve this in a beginner-friendly way without manual host-side permissions updates: 1. The runner stage now installs `gosu`. 2. At the end of the Dockerfile, the user switches back to `root` so the container launches as root. 3. On launch, the entrypoint script executes. Since it runs as root, it automatically changes ownership of `/app/data` to `bun:bun`. 4. The entrypoint script then uses `gosu` to drop privileges and launch the actual server processes as the non-root `bun` user. --- Dockerfile | 8 ++++++++ scripts/docker-entrypoint.sh | 13 +++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 scripts/docker-entrypoint.sh diff --git a/Dockerfile b/Dockerfile index 1130e2fc..4d93fdd3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -47,6 +47,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ socat \ ca-certificates \ bubblewrap \ + gosu \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* @@ -69,5 +70,12 @@ COPY --chown=bun:bun --from=builder /app/prisma.config.ts.prod ./prisma.config.t # Expose port (Bun Hono defaults to 3000) EXPOSE 3000 +# Switch back to root at the end so the container runs the entrypoint as root +USER root +COPY scripts/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh +RUN chmod +x /usr/local/bin/docker-entrypoint.sh + +ENTRYPOINT ["docker-entrypoint.sh"] + # Default command: run migrations and start API server CMD ["sh", "-c", "bun prisma migrate deploy && bun index.js"] diff --git a/scripts/docker-entrypoint.sh b/scripts/docker-entrypoint.sh new file mode 100644 index 00000000..10ae2ac6 --- /dev/null +++ b/scripts/docker-entrypoint.sh @@ -0,0 +1,13 @@ +#!/bin/sh +set -e + +# Check if /app/data exists and is not owned by the bun user (UID 1000) +if [ -d "/app/data" ]; then + if [ "$(stat -c '%u' /app/data)" != "1000" ]; then + echo "Correcting permissions on /app/data..." + chown -R bun:bun /app/data + fi +fi + +# Run the CMD as the bun user +exec gosu bun "$@"