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
This commit is contained in:
11
Dockerfile
11
Dockerfile
@@ -22,8 +22,8 @@ RUN pnpm build
|
|||||||
# Production stage
|
# Production stage
|
||||||
FROM node:22-alpine AS production
|
FROM node:22-alpine AS production
|
||||||
|
|
||||||
# Install pnpm
|
# Install pnpm and su-exec
|
||||||
RUN npm install -g pnpm ts-node
|
RUN npm install -g pnpm ts-node && apk add --no-cache su-exec
|
||||||
|
|
||||||
# Set working directory
|
# Set working directory
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
@@ -53,9 +53,12 @@ RUN adduser -S nextjs -u 1001
|
|||||||
# Make start script executable
|
# Make start script executable
|
||||||
RUN chmod +x /app/start.sh
|
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
|
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 port
|
||||||
EXPOSE 3000
|
EXPOSE 3000
|
||||||
|
9
start.sh
9
start.sh
@@ -1,11 +1,14 @@
|
|||||||
#!/bin/sh
|
#!/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/users
|
||||||
mkdir -p /app/.storage/bookings
|
mkdir -p /app/.storage/bookings
|
||||||
mkdir -p /app/.storage/treatments
|
mkdir -p /app/.storage/treatments
|
||||||
mkdir -p /app/.storage/availability
|
mkdir -p /app/.storage/availability
|
||||||
mkdir -p /app/.storage/cancellation-tokens
|
mkdir -p /app/.storage/cancellation-tokens
|
||||||
|
|
||||||
# Start the application
|
# Change ownership to nextjs user
|
||||||
exec node server-dist/index.js
|
chown -R nextjs:nodejs /app/.storage
|
||||||
|
|
||||||
|
# Start the application as nextjs user
|
||||||
|
exec su-exec nextjs node server-dist/index.js
|
||||||
|
Reference in New Issue
Block a user