From 08f3fb6e85da0031387381b9dae34165787bdeda Mon Sep 17 00:00:00 2001 From: Julien Froidefond Date: Fri, 31 Oct 2025 14:00:50 +0100 Subject: [PATCH] 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. --- Dockerfile | 16 ++++++++++------ data/README.md | 0 docker-compose.yml | 4 ++-- 3 files changed, 12 insertions(+), 8 deletions(-) mode change 100644 => 100755 data/README.md diff --git a/Dockerfile b/Dockerfile index e69c0d9..a5dd7e5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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'"] diff --git a/data/README.md b/data/README.md old mode 100644 new mode 100755 diff --git a/docker-compose.yml b/docker-compose.yml index 990d646..997fb44 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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}