diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..77dbe5a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,39 @@ +# Build stage +FROM node:20-alpine AS builder + +# Set working directory +WORKDIR /app + +# Copy package files +COPY package*.json ./ +COPY tsconfig.json ./ + +# Install all dependencies (including dev dependencies for building) +RUN npm ci + +# Copy source code +COPY src/ ./src/ + +# Build the application +RUN npm run build + +# Production stage +FROM node:20-alpine AS production + +# Set working directory +WORKDIR /app + +# Copy package files +COPY package*.json ./ + +# Install production dependencies only +RUN npm ci --only=production + +# Copy built application from builder stage +COPY --from=builder /app/dist ./dist + +# Expose port +EXPOSE 3000 + +# Start the application +CMD ["npm", "start"] diff --git a/README.md b/README.md index 83dab5e..068eece 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ ## 运行步骤 +### 本地开发 1. **安装依赖:** ``` npm install @@ -32,6 +33,31 @@ npm start # 生产模式(需预先构建) ``` +### Docker Compose 部署(推荐) +1. **启动所有服务:** + ``` + docker-compose up --build -d + ``` + + 这将启动: + - PostgreSQL 数据库(在 `db` 服务中) + - 许可证服务器应用(在 `app` 服务中,端口 3000) + +2. **检查服务状态:** + ``` + docker-compose ps + ``` + +3. **查看日志:** + ``` + docker-compose logs -f + ``` + +4. **停止服务:** + ``` + docker-compose down + ``` + 5. **测试 API:** - 健康检查:`GET http://localhost:3000/health` - 许可证检查:`POST http://localhost:3000/api/license/check` diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ed21ca7 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,35 @@ +version: '3.8' + +services: + app: + build: . + ports: + - "3000:3000" + depends_on: + db: + condition: service_healthy + environment: + - NODE_ENV=production + - DB_HOST=db + restart: unless-stopped + + db: + image: postgres:15 + environment: + POSTGRES_DB: kintone_license_server + POSTGRES_USER: postgres + POSTGRES_PASSWORD: psadmin + ports: + - "5432:5432" + volumes: + - postgres_data:/var/lib/postgresql/data + - ./init-db.sql:/docker-entrypoint-initdb.d/init-db.sql + healthcheck: + test: ["CMD-SHELL", "pg_isready -U postgres"] + interval: 10s + timeout: 5s + retries: 5 + restart: unless-stopped + +volumes: + postgres_data: diff --git a/src/config/database.ts b/src/config/database.ts index 3981fee..2939d3c 100644 --- a/src/config/database.ts +++ b/src/config/database.ts @@ -2,7 +2,7 @@ import { Pool } from 'pg'; export const pool = new Pool({ user: 'postgres', - host: 'localhost', + host: process.env.DB_HOST || 'localhost', database: 'kintone_license_server', password: 'psadmin', port: 5432,