Files
operify/Dockerfile
2025-06-18 20:34:48 +05:30

75 lines
2.1 KiB
Docker

# Base image for all stages
FROM node:20-alpine AS base
### Dependencies Stage ###
FROM base AS deps
# Set a fast and reliable Alpine mirror (mirrors.tuna.tsinghua.edu.cn is globally accessible)
# Check if 'git' is needed (only required if package.json has Git-based dependencies)
RUN echo "https://mirrors.tuna.tsinghua.edu.cn/alpine/v3.22/main" > /etc/apk/repositories && \
echo "https://mirrors.tuna.tsinghua.edu.cn/alpine/v3.22/community" >> /etc/apk/repositories && \
apk update --verbose && \
apk add --no-cache --verbose libc6-compat git
# Setup pnpm environment
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable
RUN corepack prepare pnpm@latest --activate
WORKDIR /app
# Install dependencies with pnpm
COPY package.json pnpm-lock.yaml ./
RUN pnpm install --frozen-lockfile --prefer-frozen-lockfile
### Builder Stage ###
FROM base AS builder
# Enable pnpm
RUN corepack enable
RUN corepack prepare pnpm@latest --activate
WORKDIR /app
# Copy node_modules from deps stage
COPY --from=deps /app/node_modules ./node_modules
# Copy all source files
COPY . .
# Build the Next.js application
RUN pnpm build
### Production Runner Stage ###
FROM base AS runner
# Set production environment
ENV NODE_ENV production
# Disable Next.js telemetry
# Learn more: https://nextjs.org/telemetry
ENV NEXT_TELEMETRY_DISABLED 1
# Create non-root user and set permissions
RUN addgroup nodejs
RUN adduser -SDH nextjs
RUN mkdir .next
RUN chown nextjs:nodejs .next
# Copy built artifacts with correct ownership
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
COPY --from=builder --chown=nextjs:nodejs /app/public ./public
# Switch to non-root user
USER nextjs
# Expose port for the application
EXPOSE 3000
ENV PORT 3000
ENV HOSTNAME "0.0.0.0"
# Healthcheck using curl (available in node:20-alpine, unlike wget)
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
CMD curl -f http://localhost:3000/health || exit 1
# Start the Next.js application
CMD ["node", "server.js"]