添加docker镜像等服务

This commit is contained in:
小贺
2025-09-06 20:17:11 +08:00
parent 307dd031eb
commit 8e7790a75c
6 changed files with 224 additions and 0 deletions

14
.dockerignore Normal file
View File

@@ -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

74
Dockerfile Normal file
View File

@@ -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"]

23
Dockerfile.simple Normal file
View File

@@ -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"]

30
build-docker.bat Normal file
View File

@@ -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

42
build-docker.sh Normal file
View File

@@ -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

41
docker-compose.yml Normal file
View File

@@ -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