diff --git a/.dockerignore b/.dockerignore index bac5bdb..e470723 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,49 +1,129 @@ -# Ignore node_modules (already installed in the container) -node_modules/ +# Dependencies +node_modules +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* -# Ignore Next.js build output +# Build outputs .next/ out/ +dist/ +build/ -# Ignore Git-related files -.git/ -.gitignore - -# Ignore environment files (sensitive data) +# Environment variables .env .env.local -.env.development -.env.production -.env*.local +.env.development.local +.env.test.local +.env.production.local -# Ignore build artifacts and caches -.cache/ - -# Ignore logs and temporary files -logs/ -*.log -*.tmp -*.temp - -# Ignore editor-specific files +# IDE and editor files .vscode/ .idea/ -*.suo -*.ntvs* -*.njsproj -*.sln *.swp +*.swo +*~ -# Ignore OS-specific files +# OS generated files .DS_Store +.DS_Store? +._* +.Spotlight-V100 +.Trashes +ehthumbs.db Thumbs.db -# Ignore TypeScript build info -*.tsbuildinfo +# Git +.git +.gitignore -# Ignore coverage reports +# Docker +Dockerfile +.dockerignore +docker-compose*.yml + +# Testing coverage/ +.nyc_output/ +*.lcov -# Ignore Docker-related files (optional, since they're small) -# Dockerfile -# docker-compose.yml \ No newline at end of file +# Logs +logs +*.log +lerna-debug.log* +.pnpm-debug.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# 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/ + +# Editor directories and files +.vscode/* +!.vscode/extensions.json +.idea +*.suo +.ntvs +*.njsproj +*.sln +*.sw? + +# MacOS +.DS_Store + +# Windows +Thumbs.db +ehthumbs.db +Desktop.ini + +# README and docs (optional - remove if you want to include them) +README.md +*.md \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 29a38d1..95b7b3e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,32 +1,69 @@ -# Use the official Node.js 20 image as the base -FROM node:20-alpine +# Use the official Node.js runtime as the base image +FROM node:18-alpine AS base -# Set working directory +# Install dependencies only when needed +FROM base AS deps +# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. +RUN apk add --no-cache libc6-compat WORKDIR /app -# Install pnpm -RUN corepack enable && corepack prepare pnpm@latest --activate +# Install dependencies based on the preferred package manager +COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ +RUN \ + if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ + elif [ -f package-lock.json ]; then npm ci; \ + elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \ + else echo "Lockfile not found." && exit 1; \ + fi -# Copy only dependency files first -COPY package.json pnpm-lock.yaml* ./ - -# Install dependencies with pnpm -RUN pnpm install --frozen-lockfile - -# Copy the rest of the application code +# Rebuild the source code only when needed +FROM base AS builder +WORKDIR /app +COPY --from=deps /app/node_modules ./node_modules COPY . . -# Create a cache directory for Next.js -RUN mkdir -p /app/.next/cache +# Next.js collects completely anonymous telemetry data about general usage. +# Learn more here: https://nextjs.org/telemetry +# Uncomment the following line in case you want to disable telemetry during the build. +# ENV NEXT_TELEMETRY_DISABLED 1 -# Disable telemetry -ENV NEXT_TELEMETRY_DISABLED=1 +RUN \ + if [ -f yarn.lock ]; then yarn run build; \ + elif [ -f package-lock.json ]; then npm run build; \ + elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \ + else echo "Lockfile not found." && exit 1; \ + fi -# Build the Next.js application -RUN pnpm build +# Production image, copy all the files and run next +FROM base AS runner +WORKDIR /app + +ENV NODE_ENV production +# Uncomment the following line in case you want to disable telemetry during runtime. +# ENV NEXT_TELEMETRY_DISABLED 1 + +RUN addgroup --system --gid 1001 nodejs +RUN adduser --system --uid 1001 nextjs + +COPY --from=builder /app/public ./public + +# Set the correct permission for prerender cache +RUN mkdir .next +RUN chown nextjs:nodejs .next + +# Automatically leverage output traces to reduce image size +# https://nextjs.org/docs/advanced-features/output-file-tracing +COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ +COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static + +USER nextjs -# Expose port 3000 EXPOSE 3000 -# Start the Next.js application -CMD ["pnpm", "start", "-p", "3000"] \ No newline at end of file +ENV PORT 3000 +# set hostname to localhost +ENV HOSTNAME "0.0.0.0" + +# server.js is created by next build from the standalone output +# https://nextjs.org/docs/pages/api-reference/next-config-js/output +CMD ["node", "server.js"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index eee7c53..6c76778 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,7 @@ +version: '3.8' + services: - web: + nextjs-app: build: context: . dockerfile: Dockerfile @@ -7,11 +9,13 @@ services: - "8091:3000" environment: - NODE_ENV=production - - NEXT_TELEMETRY_DISABLED=1 - volumes: - - .:/app - - /app/node_modules - - next-cache:/app/.next/cache + - PORT=3000 + - HOSTNAME=0.0.0.0 restart: unless-stopped -volumes: - next-cache: \ No newline at end of file + container_name: nextjs-app + # Uncomment the following lines if you need to persist data or add volumes + # volumes: + # - ./data:/app/data + # If you need environment variables from a file + # env_file: + # - .env.local \ No newline at end of file