This commit is contained in:
hailin 2025-04-05 18:33:09 +08:00
parent 19ae1f877c
commit 3924916e22
3 changed files with 70 additions and 0 deletions

View File

@ -0,0 +1,9 @@
node_modules
.next
Dockerfile
docker-compose.yml
.git
.gitignore
*.md
test/
tmp/

56
apps/blogai/Dockerfile Normal file
View File

@ -0,0 +1,56 @@
# --- 第一阶段:构建阶段 ---
FROM node:18-bullseye-slim AS builder
# 设置构建环境变量
ENV NODE_ENV=development
# 设置工作目录
WORKDIR /app
# 安装 pnpm
RUN npm install -g pnpm
# 复制依赖声明文件
COPY package.json pnpm-lock.yaml ./
# 安装所有依赖(包括开发依赖)
RUN pnpm install
# 复制项目源码(注意 .dockerignore 应该屏蔽 node_modules 等无关内容)
COPY . .
# 编译 Next.js 应用
RUN pnpm run build
# --- 第二阶段:生产环境运行阶段 ---
FROM node:18-slim AS runner
# 设置运行环境变量
ENV NODE_ENV=production
ENV PORT=3008
# 安装 pm2
RUN npm install -g pm2
# 设置运行目录
WORKDIR /app
# 复制生产所需文件
COPY --from=builder /app/package.json ./
COPY --from=builder /app/pnpm-lock.yaml ./
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/public ./public
COPY --from=builder /app/next.config.js ./next.config.js
COPY --from=builder /app/vercel.json ./vercel.json # 如果有
COPY --from=builder /app/locales ./locales # 如果有多语言
# 清理无用缓存,减小镜像体积
RUN rm -rf /root/.npm /root/.pnpm-store /tmp/*
# 暴露端口
EXPOSE 3008
# 容器启动命令
CMD ["pm2-runtime", "npm", "--", "start"]

5
apps/blogai/build.sh Normal file
View File

@ -0,0 +1,5 @@
# 1. 构建镜像(名字直接叫 cradle
docker build -t cradle:latest .
# 2. 运行容器(容器名叫 Cradle
docker run -d --name Cradle -p 3008:3008 --restart always cradle:latest