diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..643dd63 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,24 @@ +# 依赖目录 +node_modules/ + +# 编译产出目录 +.output/ +.nuxt/ + +# 环境变量文件(通常不应该被打包进去) +.env +.env.* + +# Git 相关 +.git/ +.gitignore + +# 系统相关 +.DS_Store +Thumbs.db + +# 日志 +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..021bc15 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,44 @@ +# ========================================== +# 1. Build Stage +# ========================================== +FROM node:22-alpine AS builder + +# 启用 corepack 并安装 pnpm +RUN corepack enable && corepack prepare pnpm@9 --activate + +# 设置工作目录 +WORKDIR /app + +# 复制依赖相关文件 +COPY package.json pnpm-lock.yaml ./ + +# 安装依赖项 +RUN pnpm install --frozen-lockfile + +# 复制项目源代码 +COPY . . + +# 执行打包构建(将生成 .output 目录) +RUN pnpm run build + +# ========================================== +# 2. Production Stage +# ========================================== +FROM node:22-alpine AS runner + +# 设置工作目录 +WORKDIR /app + +# 设置生产环境变量(Nuxt/Nitro 默认会读取这些变量) +ENV NODE_ENV=production +ENV HOST=0.0.0.0 +ENV PORT=3000 + +# 仅复制构建产物(.output 目录包含了运行所需的所有内容) +COPY --from=builder /app/.output ./.output + +# 暴露端口 +EXPOSE 3000 + +# 启动 Node.js 服务器 +CMD ["node", ".output/server/index.mjs"]