From ffc21a76e701832004b4c860656c6483a1bdb2b6 Mon Sep 17 00:00:00 2001 From: elpatron Date: Wed, 1 Oct 2025 23:07:33 +0200 Subject: [PATCH] Fix: Resolve permission issues with .storage directories - Install su-exec in Dockerfile for user switching - Modified start.sh to create directories as root, then change ownership - Container starts as root but switches to nextjs user for app execution - This prevents permission denied errors when creating .storage directories --- Dockerfile | 11 +++++++---- start.sh | 9 ++++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/Dockerfile b/Dockerfile index 7449fd9..68d2d8b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,8 +22,8 @@ RUN pnpm build # Production stage FROM node:22-alpine AS production -# Install pnpm -RUN npm install -g pnpm ts-node +# Install pnpm and su-exec +RUN npm install -g pnpm ts-node && apk add --no-cache su-exec # Set working directory WORKDIR /app @@ -53,9 +53,12 @@ RUN adduser -S nextjs -u 1001 # Make start script executable RUN chmod +x /app/start.sh -# Change ownership of the app directory +# Change ownership of the app directory (but keep root for .storage) RUN chown -R nextjs:nodejs /app -USER nextjs +RUN chown root:root /app/.storage 2>/dev/null || true + +# Don't switch to nextjs user here - the start script will handle it +# USER nextjs # Expose port EXPOSE 3000 diff --git a/start.sh b/start.sh index 62bd17d..32cf6a8 100644 --- a/start.sh +++ b/start.sh @@ -1,11 +1,14 @@ #!/bin/sh -# Create .storage directories if they don't exist +# Create .storage directories if they don't exist (as root) mkdir -p /app/.storage/users mkdir -p /app/.storage/bookings mkdir -p /app/.storage/treatments mkdir -p /app/.storage/availability mkdir -p /app/.storage/cancellation-tokens -# Start the application -exec node server-dist/index.js +# Change ownership to nextjs user +chown -R nextjs:nodejs /app/.storage + +# Start the application as nextjs user +exec su-exec nextjs node server-dist/index.js