From 605cbc3a8bc72a45252eac50d9bcb199527602c6 Mon Sep 17 00:00:00 2001 From: "n.tolstov" Date: Sun, 30 Nov 2025 22:19:45 +0300 Subject: [PATCH] build: Implement multi-stage Dockerfile for frontend production build, passing environment variables via build arguments. --- docker-compose.yml | 14 ++++---------- frontend/Dockerfile | 37 ++++++++++++++++++++++++++++--------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index c425ce5..28d3ad0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -53,19 +53,15 @@ services: condition: service_started restart: unless-stopped - # Next.js frontend + # Next.js frontend (production build) frontend: build: context: ./frontend dockerfile: Dockerfile + args: + - NEXT_PUBLIC_API_URL=http://192.168.50.130:8091 + - NEXT_PUBLIC_WS_URL=ws://192.168.50.130:8091 container_name: sp-frontend - environment: - - NEXT_PUBLIC_API_URL=http://192.168.50.130:8091 - - NEXT_PUBLIC_WS_URL=ws://192.168.50.130:8091 - volumes: - - ./frontend:/app - - frontend_node_modules:/app/node_modules - - frontend_next:/app/.next ports: - "3091:3000" depends_on: @@ -75,5 +71,3 @@ services: volumes: postgres_data: piston_packages: - frontend_node_modules: - frontend_next: diff --git a/frontend/Dockerfile b/frontend/Dockerfile index f99b2c5..9cde049 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -1,4 +1,4 @@ -FROM node:22-alpine +FROM node:22-alpine AS builder WORKDIR /app @@ -6,19 +6,38 @@ WORKDIR /app COPY package*.json ./ # Install dependencies -RUN npm install +RUN npm ci # Copy application code COPY . . -# Make entrypoint executable -RUN chmod +x /app/entrypoint.sh +# Build arguments for environment variables +ARG NEXT_PUBLIC_API_URL +ARG NEXT_PUBLIC_WS_URL + +ENV NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL +ENV NEXT_PUBLIC_WS_URL=$NEXT_PUBLIC_WS_URL + +# Build the application +RUN npm run build + +# Production image +FROM node:22-alpine AS runner + +WORKDIR /app + +ENV NODE_ENV=production + +# Copy necessary files from builder +COPY --from=builder /app/package*.json ./ +COPY --from=builder /app/.next ./.next +COPY --from=builder /app/node_modules ./node_modules + +# Copy public folder if exists +COPY --from=builder /app/public* ./public/ # Expose port EXPOSE 3000 -# Use entrypoint for dependency sync -ENTRYPOINT ["/app/entrypoint.sh"] - -# Run the development server -CMD ["npm", "run", "dev"] +# Run the production server +CMD ["npm", "run", "start"]