From 2a5876fa831e1df58c3b495248de9f4e0acb774c Mon Sep 17 00:00:00 2001 From: lei Date: Tue, 12 Nov 2024 20:15:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0dockerfile=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0pm2=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 5 ++++- Dockerfile | 38 ++++++++++++-------------------------- ecosystem.config.cjs | 25 +++++++++++++++++++++++++ start.sh | 16 ++++++++++++++++ 4 files changed, 57 insertions(+), 27 deletions(-) create mode 100644 ecosystem.config.cjs create mode 100644 start.sh diff --git a/.gitignore b/.gitignore index 7fdda33..0e0ad1b 100644 --- a/.gitignore +++ b/.gitignore @@ -23,4 +23,7 @@ logs !.env.example # package -package-lock.json \ No newline at end of file +package-lock.json + +#pm2 +ecosystem.config.cjs.me \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index bcb57e6..3a33b9a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,35 +11,14 @@ RUN npm install -g pnpm # 复制 package.json 和 pnpm-lock.yaml(或 package-lock.json,如果有) COPY package*.json ./ -# 安装仅用于构建的依赖 +# 安装构建依赖 RUN pnpm install # 复制所有项目文件 COPY . . -ENV NODE_ENV=production -# 定义构建时的变量 -ARG ADMIN_USER -ARG ADMIN_PASSWORD -ARG ADMIN_EMAIL -ARG JWT_SECRET -ARG DATABASE_URL -ARG DATABASE_SCHEMA -ARG SHADOW_DATABASE_URL - -# 设置环境变量 -ENV ADMIN_USER=${ADMIN_USER} -ENV ADMIN_PASSWORD=${ADMIN_PASSWORD} -ENV ADMIN_EMAIL=${ADMIN_EMAIL} -ENV JWT_SECRET=${JWT_SECRET} -ENV DATABASE_URL=${DATABASE_URL} -ENV DATABASE_SCHEMA=${DATABASE_SCHEMA} -ENV SHADOW_DATABASE_URL=${SHADOW_DATABASE_URL} -ENV NODE_OPTIONS="--max-old-space-size=4096" - -# 生成 Prisma 客户端和数据库迁移 +# 生成 Prisma 客户端(不执行数据库迁移) RUN npx prisma generate -RUN npx prisma migrate deploy # 构建 Nuxt.js 项目 RUN npm run build @@ -56,12 +35,19 @@ COPY --from=builder /app/.output .output COPY --from=builder /app/node_modules node_modules COPY --from=builder /app/package.json package.json -# 设置环境变量 +# 复制 prisma schema 文件 +COPY --from=builder /app/prisma /app/prisma + +# 复制启动脚本 +COPY start.sh /app/start.sh +RUN chmod +x /app/start.sh + +# 设置公共的环境变量 ENV NUXT_HOST=0.0.0.0 ENV NUXT_PORT=3000 # 暴露端口 EXPOSE 3000 -# 启动 Nuxt.js 应用 -CMD ["node", ".output/server/index.mjs"] \ No newline at end of file +# 使用启动脚本作为启动命令 +CMD ["/app/start.sh"] \ No newline at end of file diff --git a/ecosystem.config.cjs b/ecosystem.config.cjs new file mode 100644 index 0000000..0b6d962 --- /dev/null +++ b/ecosystem.config.cjs @@ -0,0 +1,25 @@ +module.exports = { + apps: [ + { + name: "aipan-netdisk-search", + port: "3000", + exec_mode: "cluster", + instances: "max", + script: "./.output/server/index.mjs", + watch: true, + env: { + NODE_ENV: "development", + }, + env_production: { + NODE_ENV: "production", + ADMIN_USER: "", + ADMIN_PASSWORD: "", + ADMIN_EMAIL: "", + JWT_SECRET: "", + DATABASE_SCHEMA: "", + DATABASE_URL: "", + SHADOW_DATABASE_URL: "", + }, + }, + ], +}; diff --git a/start.sh b/start.sh new file mode 100644 index 0000000..3414e44 --- /dev/null +++ b/start.sh @@ -0,0 +1,16 @@ +#!/bin/sh + +# 检查必要的环境变量是否已设置 +: "${ADMIN_PASSWORD:?Need to set ADMIN_PASSWORD}" +: "${ADMIN_EMAIL:?Need to set ADMIN_EMAIL}" +: "${JWT_SECRET:?Need to set JWT_SECRET}" +: "${DATABASE_URL:?Need to set DATABASE_URL}" +: "${DATABASE_SCHEMA:?Need to set DATABASE_SCHEMA}" + +echo "Running database migrations..." +# 执行数据库迁移,并指定 schema 文件的位置 +npx prisma migrate deploy --schema=/app/prisma/schema.prisma + +echo "Starting application..." +# 启动 Nuxt.js 应用 +exec node .output/server/index.mjs \ No newline at end of file