From 8e7790a75ca1c08150c342fe5860a67d7856ae1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=B4=BA?= Date: Sat, 6 Sep 2025 20:17:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0docker=E9=95=9C=E5=83=8F?= =?UTF-8?q?=E7=AD=89=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 14 +++++++++ Dockerfile | 74 ++++++++++++++++++++++++++++++++++++++++++++++ Dockerfile.simple | 23 ++++++++++++++ build-docker.bat | 30 +++++++++++++++++++ build-docker.sh | 42 ++++++++++++++++++++++++++ docker-compose.yml | 41 +++++++++++++++++++++++++ 6 files changed, 224 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 Dockerfile.simple create mode 100644 build-docker.bat create mode 100644 build-docker.sh create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..5709f92 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,14 @@ +node_modules +npm-debug.log +dist +*.log +.env +.env.local +.env.production +.git +.gitignore +README.md +.vscode +.frontend/node_modules +.frontend/dist +.frontend/.env \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..00a2ef1 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,74 @@ +# 使用节点官方镜像作为构建阶段 +FROM node:18-alpine AS backend-builder + +# 设置工作目录 +WORKDIR /app + +# 复制后端依赖文件 +COPY package*.json ./ +COPY prisma ./prisma/ + +# 安装依赖 +RUN npm ci --only=production + +# 复制源代码 +COPY . . + +# 构建后端 +RUN npm run build:server + +# 前端构建阶段 +FROM node:18-alpine AS frontend-builder + +WORKDIR /app/frontend + +# 复制前端依赖文件 +COPY frontend/package*.json ./ + +# 安装前端依赖 +RUN npm ci + +# 复制前端源代码 +COPY frontend ./ + +# 构建前端 +RUN npm run build + +# 生产阶段 +FROM node:18-alpine AS production + +# 安装必要的系统依赖 +RUN apk add --no-cache dumb-init + +# 创建非root用户 +RUN addgroup -g 1001 -S nodejs +RUN adduser -S nodejs -u 1001 + +# 设置工作目录 +WORKDIR /app + +# 复制后端生产依赖和构建文件 +COPY --from=backend-builder --chown=nodejs:nodejs /app/package*.json ./ +COPY --from=backend-builder --chown=nodejs:nodejs /app/node_modules ./node_modules +COPY --from=backend-builder --chown=nodejs:nodejs /app/dist ./dist +COPY --from=backend-builder --chown=nodejs:nodejs /app/prisma ./prisma + +# 复制前端构建文件 +COPY --from=frontend-builder --chown=nodejs:nodejs /app/frontend/dist ./frontend/dist + +# 设置环境变量 +ENV NODE_ENV=production +ENV PORT=3000 + +# 切换到非root用户 +USER nodejs + +# 暴露端口 +EXPOSE 3000 + +# 健康检查 +HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \ + CMD wget --no-verbose --tries=1 --spider http://localhost:3000/health || exit 1 + +# 启动应用 +CMD ["dumb-init", "node", "dist/server.js"] \ No newline at end of file diff --git a/Dockerfile.simple b/Dockerfile.simple new file mode 100644 index 0000000..e59dbf4 --- /dev/null +++ b/Dockerfile.simple @@ -0,0 +1,23 @@ +# 简化的单阶段构建(适用于开发环境) +FROM node:18-alpine + +# 设置工作目录 +WORKDIR /app + +# 复制所有文件 +COPY . . + +# 安装后端依赖 +RUN npm ci + +# 安装前端依赖并构建 +RUN cd frontend && npm ci && npm run build + +# 构建后端 +RUN npm run build:server + +# 暴露端口 +EXPOSE 3000 + +# 启动应用 +CMD ["npm", "start"] \ No newline at end of file diff --git a/build-docker.bat b/build-docker.bat new file mode 100644 index 0000000..c1d5aab --- /dev/null +++ b/build-docker.bat @@ -0,0 +1,30 @@ +@echo off +echo Building SiliconFlow API Key Validator Docker image... + +echo. +echo Step 1: Installing backend dependencies... +if not exist node_modules ( + npm install +) + +echo. +echo Step 2: Installing frontend dependencies... +if not exist frontend\node_modules ( + cd frontend + npm install + cd .. +) + +echo. +echo Step 3: Building Docker image... +docker build -t siliconflow-validator . + +echo. +echo Build completed! +echo To run the container: +echo docker run -p 3000:3000 siliconflow-validator +echo. +echo To run with Docker Compose: +echo docker-compose up +echo. +pause \ No newline at end of file diff --git a/build-docker.sh b/build-docker.sh new file mode 100644 index 0000000..7b307ca --- /dev/null +++ b/build-docker.sh @@ -0,0 +1,42 @@ +#!/bin/bash + +echo "🐳 Building SiliconFlow API Key Validator Docker image..." + +# 检查Docker是否已安装 +if ! command -v docker &> /dev/null; then + echo "❌ Docker is not installed. Please install Docker first." + exit 1 +fi + +# 安装依赖 +echo "📦 Installing dependencies..." +if [ ! -d "node_modules" ]; then + npm install +fi + +if [ ! -d "frontend/node_modules" ]; then + cd frontend + npm install + cd .. +fi + +# 构建Docker镜像 +echo "🔨 Building Docker image..." +docker build -t siliconflow-validator . + +# 检查构建结果 +if [ $? -eq 0 ]; then + echo "✅ Docker image built successfully!" + echo + echo "🏃‍♂️ To run the container:" + echo " docker run -p 3000:3000 siliconflow-validator" + echo + echo "🐳 To run with Docker Compose:" + echo " docker-compose up" + echo + echo "🧪 To run development container:" + echo " docker-compose --profile dev up" +else + echo "❌ Docker build failed!" + exit 1 +fi \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..063f5ce --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,41 @@ +version: '3.8' + +services: + app: + build: + context: . + dockerfile: Dockerfile + ports: + - "3000:3000" + environment: + - NODE_ENV=production + - DATABASE_URL=file:/app/data/database.db + volumes: + - ./data:/app/data + restart: unless-stopped + healthcheck: + test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:3000/health"] + interval: 30s + timeout: 10s + retries: 3 + start_period: 40s + + # 开发环境(可选的开发容器) + app-dev: + build: + context: . + dockerfile: Dockerfile.simple + ports: + - "3000:3000" + - "5173:5173" # Vite开发服务器端口 + environment: + - NODE_ENV=development + - DATABASE_URL=file:/app/data/database.db + volumes: + - ./data:/app/data + - .:/app + - /app/node_modules + - /app/frontend/node_modules + command: npm run dev + profiles: + - dev \ No newline at end of file