feat: docker 构建
This commit is contained in:
15
.env.example
15
.env.example
@@ -4,12 +4,11 @@ HOST=0.0.0.0
|
|||||||
|
|
||||||
# 服务器读取、写入和空闲连接的超时时间(秒)
|
# 服务器读取、写入和空闲连接的超时时间(秒)
|
||||||
SERVER_READ_TIMEOUT=120
|
SERVER_READ_TIMEOUT=120
|
||||||
SERVER_WRITE_TIMEOUT=1800
|
SERVER_WRITE_TIMEOUT=600
|
||||||
SERVER_GRACEFUL_SHUTDOWN_TIMEOUT=60
|
SERVER_GRACEFUL_SHUTDOWN_TIMEOUT=30
|
||||||
SERVER_IDLE_TIMEOUT=120
|
SERVER_IDLE_TIMEOUT=120
|
||||||
|
|
||||||
# 认证配置
|
# 认证配置 是必需的,用于保护管理 API 和 UI 界面
|
||||||
# AUTH_KEY 是必需的,用于保护管理 API 和 UI 界面
|
|
||||||
AUTH_KEY=sk-123456
|
AUTH_KEY=sk-123456
|
||||||
|
|
||||||
# CORS配置
|
# CORS配置
|
||||||
@@ -25,14 +24,14 @@ KEY_VALIDATION_POOL_SIZE=50
|
|||||||
|
|
||||||
# 数据库配置
|
# 数据库配置
|
||||||
# 示例 DSN: user:password@tcp(localhost:3306)/gpt_load?charset=utf8mb4&parseTime=True&loc=Local
|
# 示例 DSN: user:password@tcp(localhost:3306)/gpt_load?charset=utf8mb4&parseTime=True&loc=Local
|
||||||
DATABASE_DSN=
|
DATABASE_DSN=root:123456@tcp(mysql:3306)/gpt_load?charset=utf8mb4&parseTime=True&loc=Local
|
||||||
|
|
||||||
# Redis配置
|
# Redis配置
|
||||||
# 示例 DSN: redis://:password@localhost:6379/1
|
# 示例 DSN: redis://:password@localhost:6379/0
|
||||||
REDIS_DSN=
|
REDIS_DSN=redis://redis:6379/0
|
||||||
|
|
||||||
# 日志配置
|
# 日志配置
|
||||||
LOG_LEVEL=info
|
LOG_LEVEL=info
|
||||||
LOG_FORMAT=text
|
LOG_FORMAT=text
|
||||||
LOG_ENABLE_FILE=false
|
LOG_ENABLE_FILE=false
|
||||||
LOG_FILE_PATH=logs/app.log
|
LOG_FILE_PATH=
|
||||||
|
71
Dockerfile
71
Dockerfile
@@ -1,81 +1,30 @@
|
|||||||
# --- Stage 1: Frontend Builder ---
|
|
||||||
FROM node:20-alpine AS frontend-builder
|
FROM node:20-alpine AS frontend-builder
|
||||||
|
|
||||||
WORKDIR /app/web
|
WORKDIR /build
|
||||||
|
COPY ./web .
|
||||||
# Copy web project files
|
|
||||||
COPY web/package.json web/package-lock.json ./
|
|
||||||
COPY web/tsconfig.json web/tsconfig.node.json web/tsconfig.app.json ./
|
|
||||||
COPY web/vite.config.ts ./
|
|
||||||
|
|
||||||
# Install dependencies
|
|
||||||
RUN npm install
|
RUN npm install
|
||||||
|
|
||||||
# Copy the rest of the web source code
|
|
||||||
COPY web/ ./
|
|
||||||
|
|
||||||
# Build the frontend application
|
|
||||||
RUN npm run build
|
RUN npm run build
|
||||||
|
|
||||||
# --- Stage 2: Backend Builder ---
|
|
||||||
FROM golang:1.22-alpine AS backend-builder
|
|
||||||
|
|
||||||
WORKDIR /app
|
FROM golang:1.24-alpine AS backend-builder
|
||||||
|
|
||||||
# Install build tools
|
WORKDIR /build
|
||||||
RUN apk add --no-cache git build-base
|
RUN apk add --no-cache git build-base
|
||||||
|
|
||||||
# Copy Go module files and download dependencies
|
|
||||||
COPY go.mod go.sum ./
|
COPY go.mod go.sum ./
|
||||||
RUN go mod download
|
RUN go mod download
|
||||||
|
|
||||||
# Copy the entire Go project source code
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
COPY --from=frontend-builder /build/dist ./web/dist
|
||||||
|
|
||||||
# Copy the built frontend from the previous stage
|
|
||||||
COPY --from=frontend-builder /app/web/dist ./cmd/gpt-load/dist
|
|
||||||
|
|
||||||
# Build the Go application
|
|
||||||
# We use CGO_ENABLED=0 to create a static binary
|
|
||||||
# -ldflags="-w -s" strips debug information and symbols to reduce binary size
|
|
||||||
RUN CGO_ENABLED=0 GOOS=linux go build \
|
RUN CGO_ENABLED=0 GOOS=linux go build \
|
||||||
-ldflags="-w -s" \
|
-ldflags="-w -s" \
|
||||||
-o /gpt-load \
|
-o gpt-load
|
||||||
./cmd/gpt-load
|
|
||||||
|
|
||||||
# --- Stage 3: Final Image ---
|
|
||||||
FROM alpine:latest
|
FROM alpine:latest
|
||||||
|
|
||||||
# Install necessary runtime dependencies
|
|
||||||
# ca-certificates for HTTPS connections
|
|
||||||
# tzdata for time zone information
|
|
||||||
RUN apk --no-cache add ca-certificates tzdata
|
|
||||||
|
|
||||||
# Create a non-root user and group for security
|
|
||||||
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
|
|
||||||
|
|
||||||
# Set the working directory
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
RUN apk --no-cache add ca-certificates tzdata
|
||||||
|
COPY --from=backend-builder /build/gpt-load .
|
||||||
|
|
||||||
# Copy the compiled binary from the backend-builder stage
|
EXPOSE 3000
|
||||||
COPY --from=backend-builder /gpt-load .
|
|
||||||
|
|
||||||
# Copy the configuration file example
|
|
||||||
COPY .env.example .
|
|
||||||
|
|
||||||
# Set ownership of the app directory to the non-root user
|
|
||||||
RUN chown -R appuser:appgroup /app
|
|
||||||
|
|
||||||
# Switch to the non-root user
|
|
||||||
USER appuser
|
|
||||||
|
|
||||||
# Expose the application port
|
|
||||||
# This should match the port defined in the configuration
|
|
||||||
EXPOSE 8080
|
|
||||||
|
|
||||||
# Healthcheck to ensure the application is running
|
|
||||||
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
|
|
||||||
CMD [ "wget", "-q", "--spider", "http://localhost:8080/health" ] || exit 1
|
|
||||||
|
|
||||||
# Set the entrypoint for the container
|
|
||||||
ENTRYPOINT ["/app/gpt-load"]
|
ENTRYPOINT ["/app/gpt-load"]
|
||||||
|
@@ -1,70 +1,54 @@
|
|||||||
version: '3.8'
|
|
||||||
|
|
||||||
services:
|
services:
|
||||||
app:
|
gpt-load:
|
||||||
build:
|
image: ghcr.io/tbphp/gpt-load:latest
|
||||||
context: .
|
# build:
|
||||||
dockerfile: Dockerfile
|
# context: .
|
||||||
container_name: gpt-load-app
|
# dockerfile: Dockerfile
|
||||||
|
container_name: gpt-load
|
||||||
ports:
|
ports:
|
||||||
- "8080:8080"
|
- "3000:3000"
|
||||||
env_file:
|
env_file:
|
||||||
- .env
|
- .env
|
||||||
environment:
|
restart: always
|
||||||
# Override or set environment variables here
|
|
||||||
- DB_HOST=db
|
|
||||||
- DB_PORT=3306
|
|
||||||
- DB_USER=${DB_USER}
|
|
||||||
- DB_PASSWORD=${DB_PASSWORD}
|
|
||||||
- DB_NAME=${DB_NAME}
|
|
||||||
- GIN_MODE=release
|
|
||||||
restart: unless-stopped
|
|
||||||
depends_on:
|
depends_on:
|
||||||
db:
|
mysql:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
redis:
|
redis:
|
||||||
condition: service_started
|
condition: service_healthy
|
||||||
networks:
|
|
||||||
- gpt-load-net
|
|
||||||
|
|
||||||
db:
|
|
||||||
image: mysql:8.0
|
|
||||||
container_name: gpt-load-db
|
|
||||||
restart: unless-stopped
|
|
||||||
environment:
|
|
||||||
MYSQL_DATABASE: ${DB_NAME}
|
|
||||||
MYSQL_USER: ${DB_USER}
|
|
||||||
MYSQL_PASSWORD: ${DB_PASSWORD}
|
|
||||||
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
|
|
||||||
volumes:
|
|
||||||
- db-data:/var/lib/mysql
|
|
||||||
ports:
|
|
||||||
- "3306:3306"
|
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "${DB_USER}", "-p${DB_PASSWORD}"]
|
test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
|
||||||
interval: 10s
|
interval: 30s
|
||||||
timeout: 5s
|
timeout: 10s
|
||||||
|
retries: 3
|
||||||
|
start_period: 40s
|
||||||
|
|
||||||
|
mysql:
|
||||||
|
image: mysql:8.2
|
||||||
|
container_name: gpt-load-mysql
|
||||||
|
restart: always
|
||||||
|
# ports:
|
||||||
|
# - "3306:3306"
|
||||||
|
environment:
|
||||||
|
MYSQL_ROOT_PASSWORD: 123456
|
||||||
|
MYSQL_DATABASE: gpt_load
|
||||||
|
volumes:
|
||||||
|
- mysql_data:/var/lib/mysql
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
|
||||||
|
timeout: 10s
|
||||||
retries: 5
|
retries: 5
|
||||||
networks:
|
|
||||||
- gpt-load-net
|
|
||||||
|
|
||||||
redis:
|
redis:
|
||||||
image: redis:7-alpine
|
image: redis:latest
|
||||||
container_name: gpt-load-redis
|
container_name: gpt-load-redis
|
||||||
restart: unless-stopped
|
restart: always
|
||||||
ports:
|
# ports:
|
||||||
- "6379:6379"
|
# - "6379:6379"
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD", "redis-cli", "ping"]
|
test: ["CMD", "redis-cli", "ping"]
|
||||||
interval: 10s
|
interval: 10s
|
||||||
timeout: 5s
|
timeout: 5s
|
||||||
retries: 5
|
retries: 3
|
||||||
networks:
|
|
||||||
- gpt-load-net
|
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
db-data:
|
mysql_data:
|
||||||
|
|
||||||
networks:
|
|
||||||
gpt-load-net:
|
|
||||||
driver: bridge
|
|
||||||
|
Reference in New Issue
Block a user