chore(docker): refactor Docker configuration for environment variables and database initialization

- Updated docker-compose.yml to use environment variable fallbacks for configuration.
- Modified Dockerfile to streamline database initialization using Prisma migrations directly.
- Removed init-db.js script as its functionality is now integrated into the Docker CMD.
This commit is contained in:
Julien Froidefond
2025-10-31 12:00:01 +01:00
parent 48e649cf75
commit 5d1239c4de
3 changed files with 38 additions and 93 deletions

View File

@@ -28,12 +28,9 @@ COPY . .
# Set a dummy DATABASE_URL for build time (Prisma needs it to generate client) # Set a dummy DATABASE_URL for build time (Prisma needs it to generate client)
ENV DATABASE_URL="file:/tmp/build.db" ENV DATABASE_URL="file:/tmp/build.db"
# Generate Prisma client # Generate Prisma client (no DB needed at build time)
RUN pnpm prisma generate RUN pnpm prisma generate
# Initialize the database schema for build time
RUN pnpm prisma migrate deploy || pnpm prisma db push
# Build the application # Build the application
RUN pnpm run build RUN pnpm run build
@@ -65,12 +62,9 @@ RUN chown nextjs:nodejs .next
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
# Copy Prisma schema # Copy Prisma schema and migrations
COPY --from=builder /app/prisma ./prisma COPY --from=builder /app/prisma ./prisma
# Copy scripts (including init-db.js)
COPY --from=builder /app/scripts ./scripts
# Copy pnpm node_modules (includes .pnpm store with Prisma client) # Copy pnpm node_modules (includes .pnpm store with Prisma client)
COPY --from=builder /app/node_modules ./node_modules COPY --from=builder /app/node_modules ./node_modules
@@ -86,5 +80,7 @@ USER nextjs
EXPOSE 3000 EXPOSE 3000
# Start the application with smart database initialization # Start the application with Prisma migrations
CMD ["sh", "-c", "node scripts/init-db.js && node server.js"] # 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"]

View File

@@ -5,16 +5,22 @@ services:
dockerfile: Dockerfile dockerfile: Dockerfile
target: runner target: runner
ports: ports:
- '3006:3000' - '${PORT:-3007}:3000'
environment: environment:
NODE_ENV: production NODE_ENV: ${NODE_ENV:-production}
DATABASE_URL: 'file:../data/dev.db' # Prisma DATABASE_URL: ${DATABASE_URL:-file:../data/dev.db}
BACKUP_DATABASE_PATH: './data/dev.db' # Base de données à sauvegarder BACKUP_DATABASE_PATH: ${BACKUP_DATABASE_PATH:-./data/dev.db}
BACKUP_STORAGE_PATH: './data/backups' # Dossier des sauvegardes BACKUP_STORAGE_PATH: ${BACKUP_STORAGE_PATH:-./data/backups}
TZ: Europe/Paris TZ: ${TZ:-Europe/Paris}
# NextAuth.js # NextAuth.js
NEXTAUTH_SECRET: 'TbwIWAmQgBcOlg7jRZrhkeEUDTpSr8Cj/Cc7W58fAyw=' NEXTAUTH_SECRET: ${NEXTAUTH_SECRET:-TbwIWAmQgBcOlg7jRZrhkeEUDTpSr8Cj/Cc7W58fAyw=}
NEXTAUTH_URL: 'http://localhost:3006' NEXTAUTH_URL: ${NEXTAUTH_URL:-http://localhost:3006}
# Jira (optionnel)
JIRA_BASE_URL: ${JIRA_BASE_URL:-}
JIRA_EMAIL: ${JIRA_EMAIL:-}
JIRA_API_TOKEN: ${JIRA_API_TOKEN:-}
# Debug
VERBOSE_LOGGING: ${VERBOSE_LOGGING:-false}
volumes: volumes:
- ./data:/app/data # Dossier local data/ vers /app/data - ./data:/app/data # Dossier local data/ vers /app/data
restart: unless-stopped restart: unless-stopped
@@ -31,16 +37,22 @@ services:
dockerfile: Dockerfile dockerfile: Dockerfile
target: base target: base
ports: ports:
- '3005:3000' - '${PORT_DEV:-3005}:3000'
environment: environment:
NODE_ENV: development NODE_ENV: ${NODE_ENV:-development}
DATABASE_URL: 'file:../data/dev.db' # Prisma DATABASE_URL: ${DATABASE_URL:-file:../data/dev.db}
BACKUP_DATABASE_PATH: './data/dev.db' # Base de données à sauvegarder BACKUP_DATABASE_PATH: ${BACKUP_DATABASE_PATH:-./data/dev.db}
BACKUP_STORAGE_PATH: './data/backups' # Dossier des sauvegardes BACKUP_STORAGE_PATH: ${BACKUP_STORAGE_PATH:-./data/backups}
TZ: Europe/Paris TZ: ${TZ:-Europe/Paris}
# NextAuth.js # NextAuth.js
NEXTAUTH_SECRET: 'TbwIWAmQgBcOlg7jRZrhkeEUDTpSr8Cj/Cc7W58fAyw=' NEXTAUTH_SECRET: ${NEXTAUTH_SECRET:-TbwIWAmQgBcOlg7jRZrhkeEUDTpSr8Cj/Cc7W58fAyw=}
NEXTAUTH_URL: 'http://localhost:3005' NEXTAUTH_URL: ${NEXTAUTH_URL:-http://localhost:3005}
# Jira (optionnel)
JIRA_BASE_URL: ${JIRA_BASE_URL:-}
JIRA_EMAIL: ${JIRA_EMAIL:-}
JIRA_API_TOKEN: ${JIRA_API_TOKEN:-}
# Debug
VERBOSE_LOGGING: ${VERBOSE_LOGGING:-false}
volumes: volumes:
- .:/app # code en live - .:/app # code en live
- /app/node_modules # vol anonyme pour ne pas écraser ceux du conteneur - /app/node_modules # vol anonyme pour ne pas écraser ceux du conteneur
@@ -49,7 +61,7 @@ services:
command: > command: >
sh -c "pnpm install && sh -c "pnpm install &&
pnpm prisma generate && pnpm prisma generate &&
node scripts/init-db.js && (pnpm prisma migrate deploy || (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)) &&
pnpm run dev" pnpm run dev"
profiles: profiles:
- dev - dev
@@ -59,5 +71,6 @@ services:
# ├── dev.db -> Base de données développement # ├── dev.db -> Base de données développement
# └── backups/ -> Sauvegardes automatiques # └── backups/ -> Sauvegardes automatiques
# #
# 🔧 Configuration via .env.docker # 🔧 Configuration via variables d'environnement (.env ou .env.local)
# 📚 Documentation : ./data/README.md # Les variables utilisent la syntaxe ${VAR:-default} pour les fallbacks
# 📚 Documentation : ./data/README.md et env.example

View File

@@ -1,64 +0,0 @@
#!/usr/bin/env node
/**
* Smart database initialization script
* - Fresh DB (no tables) -> prisma db push (fast, applies full schema)
* - Existing DB with data -> prisma migrate deploy (safe, incremental)
*/
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
function exec(command) {
try {
return execSync(command, { encoding: 'utf-8', stdio: 'pipe' });
} catch (error) {
return null;
}
}
function getDatabasePath() {
const dbUrl = process.env.DATABASE_URL || 'file:./data/prod.db';
// Extract path from SQLite URL (file:./path or file:/absolute/path)
return dbUrl.replace(/^file:/, '');
}
function isDatabaseEmpty() {
const dbPath = getDatabasePath();
const absolutePath = path.resolve(dbPath);
console.log(`🔍 Checking database at: ${absolutePath}`);
// Si le fichier DB n'existe pas, c'est vide
if (!fs.existsSync(absolutePath)) {
console.log('📦 Database file does not exist - fresh install');
return true;
}
// Vérifier si des tables existent
const result = exec('pnpm prisma db execute --stdin <<< ".tables"');
if (!result || result.trim() === '') {
console.log('📦 Database exists but has no tables - fresh install');
return true;
}
console.log('🔄 Database has existing tables');
return false;
}
function main() {
console.log('🚀 Starting database initialization...\n');
if (isDatabaseEmpty()) {
console.log('✨ Applying full schema with prisma db push...');
execSync('pnpm prisma db push --skip-generate', { stdio: 'inherit' });
} else {
console.log('📝 Applying migrations with prisma migrate deploy...');
execSync('pnpm prisma migrate deploy', { stdio: 'inherit' });
}
console.log('\n✅ Database ready!');
}
main();