Files
playground/app/frontend/Dockerfile

109 lines
3.7 KiB
Docker

#
# FRONTEND DOCKERFILE
#
# This Dockerfile builds the container for the Next.js frontend application.
# It uses a multi-stage build process to create lean, optimized images for
# production while providing a flexible environment for development.
#
# Stages:
# - `base`: Installs Node.js and sets up a non-root user.
# - `deps`: Installs npm dependencies.
# - `builder`: Builds the Next.js application for production.
# - `runner`: A minimal production-ready image that serves the built app.
# - `dev`: A development-ready image with hot-reloading enabled.
#
# For more details, see: ./docs/architecture.md
#
# ------------------------------------------------------------------------------
# 1. Base Stage
# - Installs Node.js and sets up a non-root user for security.
# ------------------------------------------------------------------------------
FROM node:20-slim AS base
# Set environment variables for non-interactive installation.
ENV NPM_CONFIG_LOGLEVEL=warn
# Create a non-root user and group for running the application.
# This is a critical security measure to avoid running as root.
RUN addgroup --system --gid 1001 nextjs
RUN adduser --system --uid 1001 nextjs
# ------------------------------------------------------------------------------
# 2. Dependencies Stage
# - Installs npm dependencies. This layer is cached to speed up builds
# when only source code changes.
# ------------------------------------------------------------------------------
FROM base AS deps
WORKDIR /app
# Copy the package manager files.
COPY package.json package-lock.json* ./
# Install dependencies.
RUN npm ci
# ------------------------------------------------------------------------------
# 3. Builder Stage
# - Builds the Next.js application for production.
# ------------------------------------------------------------------------------
FROM base AS builder
WORKDIR /app
# Copy dependencies from the `deps` stage.
COPY --from=deps /app/node_modules ./node_modules
# Copy the application source code.
COPY . .
# Build the Next.js application. This creates an optimized production build
# in the .next/ directory.
RUN npm run build
# ------------------------------------------------------------------------------
# 4. Runner Stage (Production)
# - Creates a minimal, secure image for serving the production application.
# ------------------------------------------------------------------------------
FROM base AS runner
WORKDIR /app
# Set the environment to "production". This tells Next.js to use the
# optimized build and enables other production-specific behaviors.
ENV NODE_ENV=production
# Switch to the non-root user.
USER nextjs
# Copy the built application from the `builder` stage.
# We copy only the necessary files to keep the image small.
COPY --from=builder --chown=nextjs:nextjs /app/public ./public
COPY --from=builder --chown=nextjs:nextjs /app/.next ./.next
COPY --from=builder --chown=nextjs:nextjs /app/node_modules ./node_modules
COPY --from=builder --chown=nextjs:nextjs /app/package.json ./package.json
# Expose the port the Next.js server will run on.
EXPOSE 3000
# The command to start the Next.js server in production mode.
CMD ["npm", "start"]
# ------------------------------------------------------------------------------
# 5. Dev Stage (Development)
# - Creates an image for local development with hot-reloading.
# ------------------------------------------------------------------------------
FROM base AS dev
WORKDIR /app
# Copy dependencies from the `deps` stage.
COPY --from=deps /app/node_modules ./node_modules
# Expose the development port.
EXPOSE 3000
# The command to start the Next.js development server.
# This will be overridden by the docker-compose file for bind mounting.
CMD ["npm", "run", "dev"]