mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2025-12-06 13:54:41 +08:00
Feat: Upgrade Docker deployment with multi-stage build and Nginx integration
- Upgrade Dockerfile to Go 1.24 with multi-stage build (backend + frontend) - Add TA-Lib installation for technical analysis support - Integrate frontend build into main container image - Add Nginx reverse proxy configuration for API routing - Update docker-compose.yml to simplified single-container architecture - Update .dockerignore to include web source for build - Improve health checks and startup time handling Benefits: - One-click deployment with single Docker image - Better resource utilization with multi-stage build - Production-ready Nginx frontend serving - Easier maintenance and deployment 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: tinkle-community <tinklefund@gmail.com>
This commit is contained in:
@@ -40,8 +40,9 @@ coin_pool_cache/
|
||||
# Config files (should be mounted)
|
||||
config.json
|
||||
|
||||
# Web directory (has its own Dockerfile)
|
||||
web/
|
||||
# Web build artifacts (but include source for multi-stage build)
|
||||
web/node_modules/
|
||||
web/dist/
|
||||
|
||||
# Temporary files
|
||||
tmp/
|
||||
|
||||
110
Dockerfile
110
Dockerfile
@@ -1,59 +1,103 @@
|
||||
# 构建阶段
|
||||
FROM golang:1.21-alpine AS builder
|
||||
# Multi-stage build for NOFX AI Trading System
|
||||
FROM golang:1.24-alpine AS backend-builder
|
||||
|
||||
# 安装必要的构建工具
|
||||
RUN apk add --no-cache git gcc musl-dev
|
||||
# Install build dependencies including TA-Lib
|
||||
RUN apk add --no-cache \
|
||||
git \
|
||||
make \
|
||||
gcc \
|
||||
g++ \
|
||||
musl-dev \
|
||||
wget \
|
||||
tar
|
||||
|
||||
# 设置工作目录
|
||||
# Install TA-Lib
|
||||
RUN wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz && \
|
||||
tar -xzf ta-lib-0.4.0-src.tar.gz && \
|
||||
cd ta-lib && \
|
||||
./configure --prefix=/usr && \
|
||||
make && \
|
||||
make install && \
|
||||
cd .. && \
|
||||
rm -rf ta-lib ta-lib-0.4.0-src.tar.gz
|
||||
|
||||
# Set working directory
|
||||
WORKDIR /app
|
||||
|
||||
# 复制 go mod 文件
|
||||
# Copy go mod files
|
||||
COPY go.mod go.sum ./
|
||||
|
||||
# 下载依赖
|
||||
# Download dependencies
|
||||
RUN go mod download
|
||||
|
||||
# 复制源代码
|
||||
# Copy backend source code
|
||||
COPY . .
|
||||
|
||||
# 构建应用
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o nofx .
|
||||
# Build the application
|
||||
RUN CGO_ENABLED=1 GOOS=linux go build -a -installsuffix cgo -o nofx .
|
||||
|
||||
# 运行阶段
|
||||
# Frontend build stage
|
||||
FROM node:18-alpine AS frontend-builder
|
||||
|
||||
WORKDIR /app/web
|
||||
|
||||
# Copy package files
|
||||
COPY web/package*.json ./
|
||||
|
||||
# Install dependencies
|
||||
RUN npm ci
|
||||
|
||||
# Copy frontend source
|
||||
COPY web/ ./
|
||||
|
||||
# Build frontend
|
||||
RUN npm run build
|
||||
|
||||
# Final stage
|
||||
FROM alpine:latest
|
||||
|
||||
# 安装 ca-certificates(HTTPS 请求需要)
|
||||
RUN apk --no-cache add ca-certificates tzdata
|
||||
# Install runtime dependencies
|
||||
RUN apk add --no-cache \
|
||||
ca-certificates \
|
||||
tzdata \
|
||||
wget \
|
||||
tar \
|
||||
make \
|
||||
gcc \
|
||||
g++ \
|
||||
musl-dev
|
||||
|
||||
# 设置时区为上海
|
||||
ENV TZ=Asia/Shanghai
|
||||
# Install TA-Lib runtime
|
||||
RUN wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz && \
|
||||
tar -xzf ta-lib-0.4.0-src.tar.gz && \
|
||||
cd ta-lib && \
|
||||
./configure --prefix=/usr && \
|
||||
make && \
|
||||
make install && \
|
||||
cd .. && \
|
||||
rm -rf ta-lib ta-lib-0.4.0-src.tar.gz
|
||||
|
||||
# 创建非 root 用户
|
||||
RUN addgroup -g 1000 nofx && \
|
||||
adduser -D -u 1000 -G nofx nofx
|
||||
# Set timezone to UTC
|
||||
ENV TZ=UTC
|
||||
|
||||
# 设置工作目录
|
||||
WORKDIR /app
|
||||
|
||||
# 从构建阶段复制二进制文件
|
||||
COPY --from=builder /app/nofx .
|
||||
# Copy backend binary from builder
|
||||
COPY --from=backend-builder /app/nofx .
|
||||
|
||||
# 复制配置文件示例
|
||||
COPY config.json.example ./config.json.example
|
||||
# Copy frontend build from builder
|
||||
COPY --from=frontend-builder /app/web/dist ./web/dist
|
||||
|
||||
# 创建必要的目录
|
||||
RUN mkdir -p decision_logs coin_pool_cache && \
|
||||
chown -R nofx:nofx /app
|
||||
# Create directories for logs and data
|
||||
RUN mkdir -p /app/decision_logs
|
||||
|
||||
# 切换到非 root 用户
|
||||
USER nofx
|
||||
|
||||
# 暴露 API 端口
|
||||
# Expose ports
|
||||
# 8080 for backend API
|
||||
EXPOSE 8080
|
||||
|
||||
# 健康检查
|
||||
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
|
||||
# Health check
|
||||
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
|
||||
CMD wget --no-verbose --tries=1 --spider http://localhost:8080/health || exit 1
|
||||
|
||||
# 启动应用
|
||||
# Run the application
|
||||
CMD ["./nofx"]
|
||||
|
||||
@@ -1,24 +1,21 @@
|
||||
version: '3.8'
|
||||
|
||||
services:
|
||||
# 后端服务
|
||||
backend:
|
||||
# NOFX Trading Backend
|
||||
nofx:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
container_name: nofx-backend
|
||||
container_name: nofx-trading
|
||||
restart: unless-stopped
|
||||
ports:
|
||||
- "8080:8080"
|
||||
volumes:
|
||||
# 挂载配置文件(必须)
|
||||
- ./config.json:/app/config.json:ro
|
||||
# 持久化决策日志
|
||||
- ./decision_logs:/app/decision_logs
|
||||
# 持久化币种池缓存
|
||||
- ./coin_pool_cache:/app/coin_pool_cache
|
||||
- /etc/localtime:/etc/localtime:ro # 同步主机时间
|
||||
environment:
|
||||
- TZ=Asia/Shanghai
|
||||
- TZ=Asia/Shanghai # 使用中国时区
|
||||
networks:
|
||||
- nofx-network
|
||||
healthcheck:
|
||||
@@ -26,45 +23,23 @@ services:
|
||||
interval: 30s
|
||||
timeout: 10s
|
||||
retries: 3
|
||||
start_period: 10s
|
||||
logging:
|
||||
driver: "json-file"
|
||||
options:
|
||||
max-size: "10m"
|
||||
max-file: "3"
|
||||
start_period: 60s
|
||||
|
||||
# 前端服务
|
||||
frontend:
|
||||
build:
|
||||
context: ./web
|
||||
dockerfile: Dockerfile
|
||||
# Frontend (Nginx)
|
||||
nofx-frontend:
|
||||
image: nginx:alpine
|
||||
container_name: nofx-frontend
|
||||
restart: unless-stopped
|
||||
ports:
|
||||
- "3000:80"
|
||||
depends_on:
|
||||
backend:
|
||||
condition: service_healthy
|
||||
volumes:
|
||||
- ./web/dist:/usr/share/nginx/html:ro
|
||||
- ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
|
||||
networks:
|
||||
- nofx-network
|
||||
environment:
|
||||
- TZ=Asia/Shanghai
|
||||
healthcheck:
|
||||
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost/health"]
|
||||
interval: 30s
|
||||
timeout: 10s
|
||||
retries: 3
|
||||
start_period: 5s
|
||||
logging:
|
||||
driver: "json-file"
|
||||
options:
|
||||
max-size: "10m"
|
||||
max-file: "3"
|
||||
depends_on:
|
||||
- nofx
|
||||
|
||||
networks:
|
||||
nofx-network:
|
||||
driver: bridge
|
||||
|
||||
volumes:
|
||||
decision_logs:
|
||||
coin_pool_cache:
|
||||
|
||||
49
nginx.conf
Normal file
49
nginx.conf
Normal file
@@ -0,0 +1,49 @@
|
||||
server {
|
||||
listen 80;
|
||||
server_name localhost;
|
||||
|
||||
# Frontend root
|
||||
root /usr/share/nginx/html;
|
||||
index index.html;
|
||||
|
||||
# Gzip compression
|
||||
gzip on;
|
||||
gzip_vary on;
|
||||
gzip_min_length 1024;
|
||||
gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml+rss application/javascript application/json;
|
||||
|
||||
# Frontend routes (SPA)
|
||||
location / {
|
||||
try_files $uri $uri/ /index.html;
|
||||
|
||||
# Cache static assets
|
||||
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
|
||||
expires 1y;
|
||||
add_header Cache-Control "public, immutable";
|
||||
}
|
||||
}
|
||||
|
||||
# Proxy API requests to backend
|
||||
location /api/ {
|
||||
proxy_pass http://nofx-trading:8080;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection 'upgrade';
|
||||
proxy_set_header Host $host;
|
||||
proxy_cache_bypass $http_upgrade;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
|
||||
# Increase timeout for long-running API calls
|
||||
proxy_connect_timeout 300s;
|
||||
proxy_send_timeout 300s;
|
||||
proxy_read_timeout 300s;
|
||||
}
|
||||
|
||||
# Health check endpoint
|
||||
location /health {
|
||||
return 200 "OK\n";
|
||||
add_header Content-Type text/plain;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user