From 29e2ba9bd7e755f7dc6289300a7e99575ea73fd8 Mon Sep 17 00:00:00 2001 From: hamedhosseini Date: Mon, 30 Jun 2025 17:13:29 +0330 Subject: [PATCH] set dockerfile --- docker-compose.yaml | 22 +++++++++++++----- dockerfile | 54 +++++++++++++++++++++++++++++---------------- 2 files changed, 52 insertions(+), 24 deletions(-) diff --git a/docker-compose.yaml b/docker-compose.yaml index f5a7eed..99d32bb 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,11 +1,23 @@ # docker-compose.yml -version: '3.8' +version: "3.8" services: - react-app: - build: . + frontend: + build: + context: . + dockerfile: Dockerfile + args: + - NODE_ENV=production + - NEXT_TELEMETRY_DISABLED=1 + - NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL} + - NEXT_PUBLIC_CLIENT_ID=${NEXT_PUBLIC_CLIENT_ID} + - NEXT_PUBLIC_CLIENT_SECRET=${NEXT_PUBLIC_CLIENT_SECRET} + restart: unless-stopped + container_name: nobat724-frontend ports: - - "3000:80" # Changed host port + - "3001:3000" environment: - NODE_ENV=production - restart: unless-stopped \ No newline at end of file + - NEXT_TELEMETRY_DISABLED=1 + - NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL} + - NEXT_PUBLIC_CLIENT_ID=${NEXT_PUBLIC_CLIENT_ID} \ No newline at end of file diff --git a/dockerfile b/dockerfile index abbe1f5..5b94891 100644 --- a/dockerfile +++ b/dockerfile @@ -1,32 +1,48 @@ -# Build stage -FROM node:22-alpine AS builder +# Dockerfile for Next.js 14 - Optimized for Production +# Use the official Node.js image with Alpine Linux for a smaller footprint +FROM node:22-alpine AS base + +# Set the working directory in the container WORKDIR /app -# Install system dependencies (if needed) -RUN apk add --no-cache python3 make g++ - -# Copy package files +# Copy package.json and package-lock.json (or yarn.lock) COPY package*.json ./ -# Install dependencies -RUN npm npm i --force +# Install dependencies - leveraging Docker cache +RUN npm i --force -# Copy source code +# Copy the rest of the application code COPY . . -# Build React app +# Build the Next.js application RUN npm run build -# Production stage -FROM nginx:1.25-alpine +# Stage 2: Production image - smaller and leaner +FROM node:22-alpine AS runner -# Copy nginx config -COPY docker/nginx/nginx.conf /etc/nginx/conf.d/default.conf +# Set the working directory +WORKDIR /app -# Copy build artifacts -COPY --from=builder /app/build /usr/share/nginx/html +# Set environment variables +ENV NODE_ENV production +ENV NEXT_TELEMETRY_DISABLED 1 -# Expose and run -EXPOSE 80 -CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file +# Add a non-root user for security +RUN addgroup -g 1001 -S nodejs +RUN adduser -S nextjs -u 1001 + +# Copy only the necessary files from the builder stage +COPY --from=base /app/next.config.js ./ +COPY --from=base /app/public ./public +COPY --from=base --chown=nextjs:nodejs /app/.next/standalone ./ +COPY --from=base --chown=nextjs:nodejs /app/.next/static ./.next/static + +# Change ownership of all copied files to the non-root user +USER nextjs + +# Expose the port Next.js listens on +EXPOSE 3000 + +# Command to start the Next.js server in production mode +CMD ["node", "server.js"] \ No newline at end of file