Docker config

This commit is contained in:
mukesh13
2025-06-16 20:15:06 +05:30
parent 4c8ab08a06
commit 4fb2a3d5cd
3 changed files with 182 additions and 61 deletions

View File

@ -1,49 +1,129 @@
# Ignore node_modules (already installed in the container) # Dependencies
node_modules/ node_modules
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# Ignore Next.js build output # Build outputs
.next/ .next/
out/ out/
dist/
build/
# Ignore Git-related files # Environment variables
.git/
.gitignore
# Ignore environment files (sensitive data)
.env .env
.env.local .env.local
.env.development .env.development.local
.env.production .env.test.local
.env*.local .env.production.local
# Ignore build artifacts and caches # IDE and editor files
.cache/
# Ignore logs and temporary files
logs/
*.log
*.tmp
*.temp
# Ignore editor-specific files
.vscode/ .vscode/
.idea/ .idea/
*.suo
*.ntvs*
*.njsproj
*.sln
*.swp *.swp
*.swo
*~
# Ignore OS-specific files # OS generated files
.DS_Store .DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db Thumbs.db
# Ignore TypeScript build info # Git
*.tsbuildinfo .git
.gitignore
# Ignore coverage reports # Docker
Dockerfile
.dockerignore
docker-compose*.yml
# Testing
coverage/ coverage/
.nyc_output/
*.lcov
# Ignore Docker-related files (optional, since they're small) # Logs
# Dockerfile logs
# docker-compose.yml *.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

View File

@ -1,32 +1,69 @@
# Use the official Node.js 20 image as the base # Use the official Node.js runtime as the base image
FROM node:20-alpine 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 WORKDIR /app
# Install pnpm # Install dependencies based on the preferred package manager
RUN corepack enable && corepack prepare pnpm@latest --activate 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 # Rebuild the source code only when needed
COPY package.json pnpm-lock.yaml* ./ FROM base AS builder
WORKDIR /app
# Install dependencies with pnpm COPY --from=deps /app/node_modules ./node_modules
RUN pnpm install --frozen-lockfile
# Copy the rest of the application code
COPY . . COPY . .
# Create a cache directory for Next.js # Next.js collects completely anonymous telemetry data about general usage.
RUN mkdir -p /app/.next/cache # 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 RUN \
ENV NEXT_TELEMETRY_DISABLED=1 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 # Production image, copy all the files and run next
RUN pnpm build 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 EXPOSE 3000
# Start the Next.js application ENV PORT 3000
CMD ["pnpm", "start", "-p", "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"]

View File

@ -1,5 +1,7 @@
version: '3.8'
services: services:
web: nextjs-app:
build: build:
context: . context: .
dockerfile: Dockerfile dockerfile: Dockerfile
@ -7,11 +9,13 @@ services:
- "8091:3000" - "8091:3000"
environment: environment:
- NODE_ENV=production - NODE_ENV=production
- NEXT_TELEMETRY_DISABLED=1 - PORT=3000
volumes: - HOSTNAME=0.0.0.0
- .:/app
- /app/node_modules
- next-cache:/app/.next/cache
restart: unless-stopped restart: unless-stopped
volumes: container_name: nextjs-app
next-cache: # 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