next-windfonts/Dockerfile
bo.yu 1b70025aac feat: 添加国内镜像配置并优化Docker构建
- 添加.yarnrc.yml、.yarnrc和.npmrc配置文件使用国内镜像源
- 更新Dockerfile使用阿里云镜像源并优化构建流程
- 添加sharp依赖用于图片处理
- 创建DOCKER_NETWORK_SOLUTIONS.md文档记录构建问题解决方案
- 简化logger.ts和useCssLoader.ts的配置逻辑
- 设置生产环境构建时明确指定NODE_ENV
2025-08-19 11:21:16 +08:00

90 lines
2.7 KiB
Docker
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

FROM node:20-alpine AS base
# 设置国内镜像源
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN apk update
# 设置 yarn 国内镜像源
RUN yarn config set registry https://registry.npmmirror.com
RUN yarn config set disturl https://npmmirror.com/dist
RUN yarn config set electron_mirror https://npmmirror.com/mirrors/electron/
RUN yarn config set sass_binary_site https://npmmirror.com/mirrors/node-sass/
RUN yarn config set phantomjs_cdnurl https://npmmirror.com/mirrors/phantomjs/
# Install dependencies only when needed
FROM base AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
# Install dependencies based on the preferred package manager
# 复制依赖文件和配置文件
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
COPY .npmrc .yarnrc* .yarnrc.yml* ./
COPY .yarn .yarn
RUN if [ -f yarn.lock ]; then yarn install; \
elif [ -f package-lock.json ]; then npm ci --legacy-peer-deps; \
elif [ -f pnpm-lock.yaml ]; then pnpm i; \
else echo "Lockfile not found." && exit 1; \
fi
# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
# 复制依赖
COPY --from=deps /app/node_modules ./node_modules
# 复制源代码
COPY . .
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
ENV NEXT_TELEMETRY_DISABLED=1
# 构建应用(使用 yarn
RUN yarn install && \
echo "Building with yarn..." && \
yarn run build
# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app
ENV NODE_ENV=production
# Uncomment the following line in case you want to disable telemetry during runtime.
# ENV NEXT_TELEMETRY_DISABLED=1
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
COPY --from=builder /app/public ./public
# Set the correct permission for prerender cache
RUN mkdir .next
RUN chown nextjs:nodejs .next
# Create logs directory with correct permissions
RUN mkdir logs
RUN chown nextjs:nodejs logs
# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
EXPOSE 3000
ENV PORT=3000
# server.js is created by next build from the standalone output
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
ENV HOSTNAME="0.0.0.0"
CMD ["node", "server.js"]