Files
drone-cicd/Dockerfile
2026-07-01 16:20:26 +08:00

72 lines
2.6 KiB
Docker
Raw 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.
# ============================================================
# Stage 1: 依赖安装 + 构建
# ============================================================
FROM node:20-alpine AS builder
RUN corepack enable && corepack prepare pnpm@9.15.4 --activate
WORKDIR /monorepo
# 先复制 workspace 配置文件(利用 Docker 层缓存)
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml .npmrc ./
COPY turbo.json* ./
# 复制所有 package.json供 pnpm 解析 workspace 依赖)
COPY packages/components/package.json packages/components/
COPY packages/data/package.json packages/data/
COPY packages/graphql/package.json packages/graphql/
COPY packages/i18n/package.json packages/i18n/
COPY packages/lib/package.json packages/lib/
COPY packages/store/package.json packages/store/
COPY packages/styles/package.json packages/styles/
COPY packages/types/package.json packages/types/
COPY apps/shopify-europe/package.json apps/shopify-europe/
# 安装全部依赖BuildKit 缓存加速 + secret 注入 .npmrctoken 不写入镜像层)
RUN --mount=type=cache,id=pnpm-store,target=/root/.local/share/pnpm/store \
--mount=type=secret,id=npmrc,target=/root/.npmrc \
pnpm install --frozen-lockfile
# 复制源代码
COPY packages/ packages/
COPY apps/shopify-europe/ apps/shopify-europe/
# 构建阶段设置生产模式React Router 会跳过 dev 分支做优化
ENV NODE_ENV=production
# 1. 构建所有 @repo/* 共享包
RUN pnpm --filter '@repo/*' build
# 2. 构建 React Router 应用client + server bundle
RUN NODE_OPTIONS="--max-old-space-size=8192" \
pnpm --filter shopify-europe exec react-router build
# 3. 使用 pnpm deploy 创建自包含的生产部署目录(解析 workspace 依赖)
RUN pnpm deploy --filter=shopify-europe --prod /deploy
# ============================================================
# Stage 2: 生产镜像(最小化)
# ============================================================
FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
# 非 root 用户(在 COPY 之前创建,利用层缓存)
RUN addgroup --system --gid 1001 nodejs && \
adduser --system --uid 1001 hydrogen
# 直接在 COPY 时设置归属,避免额外的 chown 层
COPY --from=builder --chown=hydrogen:nodejs /deploy .
COPY --from=builder --chown=hydrogen:nodejs /monorepo/apps/shopify-europe/build ./build
COPY --from=builder --chown=hydrogen:nodejs /monorepo/apps/shopify-europe/server.js ./server.js
USER hydrogen
EXPOSE 3005
# HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \
# CMD wget -qO- http://localhost:3005/ | head -c 1 || exit 1
CMD ["node", "server.js"]