Fix: Run as root initially to fix volume permissions

This commit is contained in:
duongcamcute
2026-01-15 23:37:33 +07:00
parent 2db3f2759e
commit 346040f9c5
2 changed files with 27 additions and 10 deletions

View File

@@ -23,10 +23,9 @@ ENV NEXT_TELEMETRY_DISABLED 1
RUN npm run build
# Production image, copy all the files and run next
FROM base AS runner
# Install OpenSSL (required for Prisma) and Prisma CLI globally directly as root
# We install prisma@5.22.0 to match project dependencies
RUN apk add --no-cache openssl && \
# Install su-exec for user switching
RUN apk add --no-cache openssl su-exec && \
npm install -g prisma@5.22.0
WORKDIR /app
@@ -50,7 +49,8 @@ COPY --from=builder --chown=nextjs:nodejs /app/docker-entrypoint.sh ./
# Set permissions
RUN chmod +x ./docker-entrypoint.sh
USER nextjs
# Do not switch to user nextjs here, we let entrypoint handle it
# USER nextjs
EXPOSE 3000

View File

@@ -1,11 +1,28 @@
#!/bin/sh
# Dừng script nếu có lỗi
set -e
# Chạy migration database (tạo bảng nếu chưa có)
echo "Running database migrations..."
prisma migrate deploy
# Fix permissions for the database directory
# This script runs as root, so we can change ownership of the mounted volume
echo "Fixing permissions for /app/db..."
mkdir -p /app/db
chown -R nextjs:nodejs /app/db
if [ -f "/app/db/prod.db" ]; then
chown nextjs:nodejs /app/db/prod.db
fi
if [ -f "/app/db/prod.db-journal" ]; then
chown nextjs:nodejs /app/db/prod.db-journal
fi
if [ -f "/app/db/prod.db-shm" ]; then
chown nextjs:nodejs /app/db/prod.db-shm
fi
if [ -f "/app/db/prod.db-wal" ]; then
chown nextjs:nodejs /app/db/prod.db-wal
fi
# Switch to nextjs user to run migration and app
echo "Running database migrations..."
su-exec nextjs prisma migrate deploy
# Chạy ứng dụng Next.js
echo "Starting Next.js application..."
exec node server.js
# exec replaces the shell process, su-exec switches user
exec su-exec nextjs node server.js