chore(docker): update database path and permissions in Docker configuration

- Modified docker-compose.yml to change DATABASE_URL path for consistency.
- Updated Dockerfile to copy Prisma schema and set a temporary DATABASE_URL for client generation.
- Enhanced CMD to ensure proper permissions for the data directory and user switching during application startup.
- Changed README.md file permissions to executable.
This commit is contained in:
Julien Froidefond
2025-10-31 14:00:50 +01:00
parent e4e49df60b
commit 08f3fb6e85
3 changed files with 12 additions and 8 deletions

View File

@@ -14,6 +14,10 @@ WORKDIR /app
# Install dependencies based on the preferred package manager
COPY package.json pnpm-lock.yaml* ./
# Copy Prisma schema for postinstall script
COPY prisma ./prisma
# Set dummy DATABASE_URL for Prisma client generation during postinstall
ENV DATABASE_URL="file:/tmp/build.db"
RUN \
if [ -f pnpm-lock.yaml ]; then pnpm install --frozen-lockfile; \
else echo "Lockfile not found." && exit 1; \
@@ -38,7 +42,7 @@ RUN pnpm run build
FROM base AS runner
# Set timezone to Europe/Paris and install sqlite3 for backups
RUN apk add --no-cache tzdata sqlite
RUN apk add --no-cache tzdata sqlite su-exec
RUN ln -snf /usr/share/zoneinfo/Europe/Paris /etc/localtime && echo Europe/Paris > /etc/timezone
WORKDIR /app
@@ -68,19 +72,19 @@ COPY --from=builder /app/prisma ./prisma
# Copy pnpm node_modules (includes .pnpm store with Prisma client)
COPY --from=builder /app/node_modules ./node_modules
# Create data directory for SQLite and backups
RUN mkdir -p /app/data/backups && chown -R nextjs:nodejs /app/data
# Create data directory for SQLite and backups (will be overridden by volume mount but ensures it exists)
RUN mkdir -p /app/data/backups && chmod -R 777 /app/data
# Set all ENV vars before switching user
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
ENV TZ=Europe/Paris
USER nextjs
EXPOSE 3000
# Start the application with Prisma migrations
# Fix permissions for data directory (volume mount may have wrong ownership)
# Then switch to nextjs user and run migrations
# For fresh DBs: use db push to apply schema, then mark migrations as applied
# For existing DBs: use migrate deploy to apply incremental migrations
CMD ["sh", "-c", "set +e; if ! pnpm prisma migrate deploy; then echo 'Migration failed, using db push for fresh database...'; pnpm prisma db push --accept-data-loss --skip-generate; for migration in prisma/migrations/*/; do if [ -d \"$migration\" ] && [ -f \"$migration/migration.sql\" ]; then migration_name=$(basename \"$migration\"); pnpm prisma migrate resolve --applied \"$migration_name\" 2>/dev/null || true; fi; done; fi; set -e; exec node server.js"]
CMD ["sh", "-c", "mkdir -p /app/data/backups && chown -R nextjs:nodejs /app || chmod -R 755 /app || true; chmod -R 777 /app/data && chown -R nextjs:nodejs /app/data || true; exec su-exec nextjs sh -c 'set +e; if ! pnpm prisma migrate deploy; then echo \"Migration failed, using db push for fresh database...\"; pnpm prisma db push --accept-data-loss --skip-generate; for migration in prisma/migrations/*/; do if [ -d \"$migration\" ] && [ -f \"$migration/migration.sql\" ]; then migration_name=$(basename \"$migration\"); pnpm prisma migrate resolve --applied \"$migration_name\" 2>/dev/null || true; fi; done; fi; set -e; exec node server.js'"]

0
data/README.md Normal file → Executable file
View File

View File

@@ -8,7 +8,7 @@ services:
- '${PORT:-3007}:3000'
environment:
NODE_ENV: ${NODE_ENV:-production}
DATABASE_URL: ${DATABASE_URL:-file:../data/dev.db}
DATABASE_URL: ${DATABASE_URL:-file:/app/data/dev.db}
BACKUP_DATABASE_PATH: ${BACKUP_DATABASE_PATH:-./data/dev.db}
BACKUP_STORAGE_PATH: ${BACKUP_STORAGE_PATH:-./data/backups}
TZ: ${TZ:-Europe/Paris}
@@ -40,7 +40,7 @@ services:
- '${PORT_DEV:-3005}:3000'
environment:
NODE_ENV: ${NODE_ENV:-development}
DATABASE_URL: ${DATABASE_URL:-file:../data/dev.db}
DATABASE_URL: ${DATABASE_URL:-file:/app/data/dev.db}
BACKUP_DATABASE_PATH: ${BACKUP_DATABASE_PATH:-./data/dev.db}
BACKUP_STORAGE_PATH: ${BACKUP_STORAGE_PATH:-./data/backups}
TZ: ${TZ:-Europe/Paris}