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