diff --git a/.dockerignore b/.dockerignore index 5621cea..2c8fede 100644 --- a/.dockerignore +++ b/.dockerignore @@ -5,34 +5,29 @@ yarn-debug.log* yarn-error.log* pnpm-debug.log* -# Next.js build output -.next -out +# Next.js +.next/ +out/ -# Production build +# Production +build dist -# Environment variables +# Environment files .env .env.local .env.development.local .env.test.local .env.production.local -# IDE and editor files +# IDE .vscode .idea *.swp *.swo -*~ -# OS generated files +# OS .DS_Store -.DS_Store? -._* -.Spotlight-V100 -.Trashes -ehthumbs.db Thumbs.db # Git @@ -40,14 +35,23 @@ Thumbs.db .gitignore # Docker -Dockerfile +Dockerfile* +docker-compose* .dockerignore -docker-compose.yml # Documentation README.md +docs/ *.md +# Tests +coverage/ +.nyc_output +*.test.js +*.test.ts +*.spec.js +*.spec.ts + # Logs logs *.log @@ -59,10 +63,60 @@ pids *.pid.lock # Coverage directory used by tools like istanbul -coverage +coverage/ + +# Dependency directories +jspm_packages/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# parcel-bundler cache (https://parceljs.org/) +.cache +.parcel-cache + +# Next.js build output +.next + +# Nuxt.js build / generate output +.nuxt + +# Storybook build outputs +.out +.storybook-out # Temporary folders -tmp -temp +tmp/ +temp/ -public/thumbnails \ No newline at end of file +# Media files (these will be mounted as volumes) +data/ +media/ +public/thumbnails/ + +# Database files +*.db +*.sqlite +*.sqlite3 + +# Backup files +*.bak +*.backup \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 1946fc2..ec8cb86 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,75 +1,67 @@ -# Use official Node.js runtime as the base image -FROM node:22.18.0 AS base +# Use a smaller base image for the final runtime +FROM node:22.18.0-alpine AS base -# Rebuild the source code only when needed +# Build stage FROM base AS builder WORKDIR /app # Install build dependencies for native modules -RUN apt-get update && apt-get install -y \ +RUN apk add --no-cache \ python3 \ make \ g++ \ - libsqlite3-dev \ - && rm -rf /var/lib/apt/lists/* + sqlite-dev \ + ffmpeg-dev # Install pnpm globally RUN npm install -g pnpm -# Copy package files and install all dependencies (including dev dependencies) +# Copy package files and install all dependencies COPY package.json package-lock.json ./ -RUN pnpm install +RUN pnpm install # Copy source code COPY . . # Rebuild better-sqlite3 to ensure native bindings are compiled correctly -RUN pnpm rebuild better-sqlite3 || npm rebuild better-sqlite3 - -# Verify native bindings are compiled -RUN find /app/node_modules -name "better_sqlite3.node" -type f - -# Database file will be created at runtime via docker-compose - -# Create directories for media storage -RUN mkdir -p /app/data /app/media +RUN pnpm rebuild better-sqlite3 # Build the application RUN pnpm buildprod -# Production image, copy all the files and run next +# Production stage FROM base AS runner WORKDIR /app ENV NODE_ENV=production ENV NEXT_TELEMETRY_DISABLED=1 -# Install FFmpeg for media file analysis -RUN apt-get update && apt-get install -y \ +# Install only runtime dependencies +RUN apk add --no-cache \ ffmpeg \ - && rm -rf /var/lib/apt/lists/* + sqlite \ + && rm -rf /var/cache/apk/* -RUN groupadd --system --gid 1001 nodejs -RUN useradd --system --uid 1001 --gid nodejs nextjs +# Create user +RUN addgroup --system --gid 1001 nodejs +RUN adduser --system --uid 1001 --ingroup nodejs nextjs -# Create media directories -RUN mkdir -p /app/data /app/media +# Create necessary directories +RUN mkdir -p /app/data /app/media /app/public/thumbnails -# Ensure directories have correct permissions -RUN chown -R nextjs:nodejs /app/data /app/media - -# Copy built application +# Copy only the necessary files from builder COPY --from=builder --chown=nextjs:nodejs /app/public ./public COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static -# Copy node_modules to ensure native bindings are available + +# Copy the entire node_modules to ensure all native bindings are available COPY --from=builder --chown=nextjs:nodejs /app/node_modules ./node_modules # Rebuild native bindings for the production environment RUN npm rebuild better-sqlite3 -# Set up volume for persistent data -VOLUME ["/app/data", "/app/media"] +# Set correct permissions +RUN chown -R nextjs:nodejs /app/data /app/media /app/public # Switch to non-root user USER nextjs diff --git a/docker/data/media.db b/docker/data/media.db index 45e8f66..85eb35e 100644 Binary files a/docker/data/media.db and b/docker/data/media.db differ diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 4c0324f..7d86377 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -2,7 +2,7 @@ version: '3.8' services: nextav: - image: ${REGISTRY_URL:-192.168.2.212:3000}/${IMAGE_NAME:-tigeren/nextav}:${IMAGE_TAG:-latest} + image: nextav:optimized container_name: nextav-app restart: unless-stopped ports: