feat: Dockerize web application by adding Dockerfile, .dockerignore, and configuring next.config for standalone output and image optimization.

This commit is contained in:
pradeepkumar
2025-12-24 06:39:10 +05:30
parent b5f18910ab
commit d308a4c52c
3 changed files with 131 additions and 1 deletions

53
.dockerignore Normal file
View File

@@ -0,0 +1,53 @@
# Dependencies
node_modules
npm-debug.log
# Next.js build output
.next
out
# Environment files
.env
.env.*
!.env.example
# IDE
.idea
.vscode
*.swp
*.swo
# OS
.DS_Store
Thumbs.db
# Git
.git
.gitignore
# Docker
Dockerfile
docker-compose*.yml
.dockerignore
# Testing
coverage
.nyc_output
__tests__
*.spec.ts
*.test.ts
*.spec.tsx
*.test.tsx
# Documentation
docs
*.md
!README.md
# Logs
logs
*.log
# Misc
.eslintcache
.prettierignore

66
Dockerfile Normal file
View File

@@ -0,0 +1,66 @@
# ===========================================
# Web Dockerfile (Next.js)
# ===========================================
# Stage 1: Dependencies
FROM node:20-alpine AS deps
WORKDIR /app
# Copy package files
COPY package*.json ./
# Install dependencies
RUN npm ci
# Stage 2: Build
FROM node:20-alpine AS builder
WORKDIR /app
# Copy dependencies from deps stage
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Set environment variables for build
ENV NEXT_TELEMETRY_DISABLED=1
ENV NODE_ENV=production
# Build the application
RUN npm run build
# Stage 3: Production
FROM node:20-alpine AS production
WORKDIR /app
# Create non-root user
RUN addgroup -g 1001 -S nodejs && \
adduser -S nextjs -u 1001
# Set environment variables
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
# Copy necessary files from builder
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
# Set ownership
RUN chown -R nextjs:nodejs /app
# Switch to non-root user
USER nextjs
# Expose port
EXPOSE 3000
# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:3000 || exit 1
# Start the application
CMD ["node", "server.js"]

View File

@@ -1,7 +1,18 @@
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
/* config options here */
// Enable standalone output for Docker
output: "standalone",
// Image optimization
images: {
remotePatterns: [
{
protocol: "https",
hostname: "**",
},
],
},
};
export default nextConfig;