tercul-frontend/Dockerfile
Damir Mukimov 40be6866b0 fix: optimize Dockerfile to use Yarn PnP instead of node-modules
- Use Yarn Plug'n'Play (PnP) for faster installs and smaller image size
- Keep .yarnrc.yml configuration instead of overriding it
- Copy PnP files (.pnp.cjs, .pnp.loader.mjs, .yarn cache) to production stage
- Use yarn workspaces focus --production for production dependencies
- Corepack manages Yarn version while PnP handles dependency resolution
2025-11-27 06:54:20 +01:00

58 lines
1.4 KiB
Docker

# Multi-stage build for Node.js frontend application
FROM node:iron-alpine3.22 AS builder
# Enable Corepack for Yarn 4.x
RUN corepack enable
# Set working directory
WORKDIR /app
# Copy Yarn configuration and package files
COPY .yarnrc.yml package.json yarn.lock ./
# Install dependencies (uses PnP by default from .yarnrc.yml)
RUN yarn install --immutable
# Copy source code
COPY . .
# Build the application
RUN yarn build
# Production stage
FROM node:iron-alpine3.22 AS production
# Enable Corepack for Yarn 4.x
RUN corepack enable
# Create app user
RUN addgroup -g 1001 -S nodejs && \
adduser -S nextjs -u 1001
# Set working directory
WORKDIR /app
# Copy Yarn configuration and package files
COPY .yarnrc.yml package.json yarn.lock ./
# Install production dependencies (uses PnP)
RUN yarn workspaces focus --production
# Copy PnP files and built application from builder stage
COPY --from=builder --chown=nextjs:nodejs /app/.pnp.cjs /app/.pnp.loader.mjs ./
COPY --from=builder --chown=nextjs:nodejs /app/.yarn ./.yarn
COPY --from=builder --chown=nextjs:nodejs /app/dist ./dist
# Switch to non-root user
USER nextjs
# Expose the application port
EXPOSE 3000
# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD node -e "require('http').get('http://localhost:3000/health', (res) => { process.exit(res.statusCode === 200 ? 0 : 1) }).on('error', () => process.exit(1))"
# Command to run the application
CMD ["yarn", "start"]