build: Implement multi-stage Dockerfile for frontend production build, passing environment variables via build arguments.

This commit is contained in:
n.tolstov 2025-11-30 22:19:45 +03:00
parent 5e65dd84f6
commit 605cbc3a8b
2 changed files with 32 additions and 19 deletions

View File

@ -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
container_name: sp-frontend
environment:
args:
- 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
container_name: sp-frontend
ports:
- "3091:3000"
depends_on:
@ -75,5 +71,3 @@ services:
volumes:
postgres_data:
piston_packages:
frontend_node_modules:
frontend_next:

View File

@ -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"]