# SvelteKit Web 前端 Dockerfile # 多阶段构建:构建阶段 + 运行阶段 # ===== 构建阶段 ===== FROM node:20-alpine AS builder WORKDIR /app # 配置 yarn 镜像源(国内) RUN yarn config set registry https://registry.npmmirror.com # 先复制依赖文件,利用 Docker 缓存 COPY package.json yarn.lock* ./ # 安装依赖 RUN yarn install --frozen-lockfile || yarn install # 复制源代码 COPY . . # 构建生产版本 RUN yarn build # ===== 运行阶段 ===== FROM node:20-alpine WORKDIR /app # 配置 Alpine 镜像源(国内) RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories # 安装必要工具 RUN apk --no-cache add curl # 设置时区 ENV TZ=Asia/Shanghai # 设置为生产环境 ENV NODE_ENV=production ENV HOST=0.0.0.0 ENV PORT=3000 # 从构建阶段复制构建产物和依赖 COPY --from=builder /app/build ./build COPY --from=builder /app/package.json ./ COPY --from=builder /app/node_modules ./node_modules # 暴露端口 EXPOSE 3000 # 健康检查 HEALTHCHECK --interval=10s --timeout=5s --retries=5 \ CMD curl -f http://localhost:3000 || exit 1 # 启动服务 CMD ["node", "build"]