From d308a4c52c7113b581e3ffe7c813eb5135755143 Mon Sep 17 00:00:00 2001 From: pradeepkumar Date: Wed, 24 Dec 2025 06:39:10 +0530 Subject: [PATCH] feat: Dockerize web application by adding Dockerfile, .dockerignore, and configuring next.config for standalone output and image optimization. --- .dockerignore | 53 ++++++++++++++++++++++++++++++++++++++++ Dockerfile | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ next.config.ts | 13 +++++++++- 3 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..32466b9 --- /dev/null +++ b/.dockerignore @@ -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 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2898d89 --- /dev/null +++ b/Dockerfile @@ -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"] diff --git a/next.config.ts b/next.config.ts index e9ffa30..dc2d4fe 100644 --- a/next.config.ts +++ b/next.config.ts @@ -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;