build: 添加 Docker 配置文件和忽略文件

添加 Dockerfile 用于构建和运行 Node.js 应用,同时添加 .dockerignore 文件排除不必要的文件和目录
This commit is contained in:
2026-04-28 13:10:04 +08:00
parent 8845c8293d
commit 6aa61cc78f
2 changed files with 68 additions and 0 deletions
+24
View File
@@ -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*
+44
View File
@@ -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"]