caidaohz commited on
Commit
a3ed433
·
1 Parent(s): 7d69ad4

✨ 更新Dockerfile,优化构建过程并添加健康检查;新增Docker部署说明和docker-compose配置文件

Browse files
Files changed (5) hide show
  1. .dockerignore +33 -0
  2. DOCKER.md +69 -0
  3. Dockerfile +34 -11
  4. docker-compose.yml +23 -0
  5. templates/styles.css +0 -0
.dockerignore ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Git 相关
2
+ .git
3
+ .gitignore
4
+ .gitattributes
5
+
6
+ # 文档
7
+ README.md
8
+ *.md
9
+
10
+ # 临时文件
11
+ .DS_Store
12
+ .env
13
+ .env.*
14
+
15
+ # 编译产物
16
+ proxy-server
17
+ api-proxy
18
+
19
+ # Node.js 相关(如果有的话)
20
+ node_modules
21
+ npm-debug.log*
22
+ yarn-debug.log*
23
+ yarn-error.log*
24
+
25
+ # IDE 配置
26
+ .vscode
27
+ .idea
28
+ *.swp
29
+ *.swo
30
+
31
+ # 其他不需要的文件
32
+ 1.html
33
+ templates/
DOCKER.md ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Docker 部署说明
2
+
3
+ ## 构建和运行
4
+
5
+ ### 使用 Docker
6
+
7
+ ```bash
8
+ # 构建镜像
9
+ docker build -t api-proxy:latest .
10
+
11
+ # 运行容器
12
+ docker run -d \
13
+ --name api-proxy-server \
14
+ -p 8000:8000 \
15
+ -e PORT=8000 \
16
+ --restart unless-stopped \
17
+ api-proxy:latest
18
+ ```
19
+
20
+ ### 使用 Docker Compose
21
+
22
+ ```bash
23
+ # 启动服务
24
+ docker-compose up -d
25
+
26
+ # 查看日志
27
+ docker-compose logs -f
28
+
29
+ # 停止服务
30
+ docker-compose down
31
+ ```
32
+
33
+ ## 健康检查
34
+
35
+ 容器包含内置的健康检查,会定期检查 `/stats` 端点:
36
+
37
+ ```bash
38
+ # 查看容器健康状态
39
+ docker ps
40
+
41
+ # 查看健康检查日志
42
+ docker inspect api-proxy-server | grep -A 10 Health
43
+ ```
44
+
45
+ ## 环境变量
46
+
47
+ - `PORT`: 服务器监听端口(默认:8000)
48
+
49
+ ## 数据持久化
50
+
51
+ 当前版本的统计数据存储在内存中,容器重启后会丢失。如需持久化,可以考虑:
52
+
53
+ 1. 添加数据库支持
54
+ 2. 挂载数据卷
55
+ 3. 使用外部存储服务
56
+
57
+ ## 访问服务
58
+
59
+ - 主页: http://localhost:8000
60
+ - 统计API: http://localhost:8000/stats
61
+ - API代理: http://localhost:8000/{endpoint}/*
62
+
63
+ ## 生产环境建议
64
+
65
+ 1. 使用反向代理(如 Nginx)
66
+ 2. 配置 HTTPS
67
+ 3. 设置适当的资源限制
68
+ 4. 配置日志收集
69
+ 5. 监控和告警
Dockerfile CHANGED
@@ -1,5 +1,10 @@
1
  # Go版本的高性能API代理服务器Dockerfile
2
- FROM golang:alpine AS builder
 
 
 
 
 
3
 
4
  # 设置工作目录
5
  WORKDIR /app
@@ -8,31 +13,44 @@ WORKDIR /app
8
  COPY go.mod go.sum ./
9
 
10
  # 下载依赖
11
- RUN go mod download
12
 
13
  # 复制源代码
14
  COPY *.go ./
15
 
16
- # 编译应用
17
- RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o api-proxy .
 
 
 
 
 
 
18
 
19
  # 运行阶段
20
- FROM alpine:latest
21
 
22
- # 安装CA证书
23
- RUN apk --no-cache add ca-certificates
 
24
 
25
  # 创建非root用户
26
  RUN addgroup -g 1001 -S apiproxy && \
27
- adduser -u 1001 -S apiproxy -G apiproxy
28
 
 
29
  WORKDIR /home/apiproxy/
30
 
31
  # 从构建阶段复制二进制文件
32
  COPY --from=builder /app/api-proxy .
33
 
 
 
 
 
34
  # 设置文件权限
35
- RUN chown apiproxy:apiproxy api-proxy
 
36
 
37
  # 切换到非root用户
38
  USER apiproxy
@@ -40,9 +58,14 @@ USER apiproxy
40
  # 暴露端口
41
  EXPOSE 8000
42
 
 
 
 
 
 
43
  # 健康检查
44
- HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
45
  CMD wget --no-verbose --tries=1 --spider http://localhost:8000/stats || exit 1
46
 
47
  # 启动应用
48
- CMD ["./api-proxy"]
 
1
  # Go版本的高性能API代理服务器Dockerfile
2
+
3
+ # 构建阶段
4
+ FROM golang:1.21-alpine AS builder
5
+
6
+ # 安装构建依赖
7
+ RUN apk add --no-cache git ca-certificates tzdata
8
 
9
  # 设置工作目录
10
  WORKDIR /app
 
13
  COPY go.mod go.sum ./
14
 
15
  # 下载依赖
16
+ RUN go mod download && go mod verify
17
 
18
  # 复制源代码
19
  COPY *.go ./
20
 
21
+ # 复制静态文件和HTML文件
22
+ COPY index.html ./
23
+ COPY static/ ./static/
24
+
25
+ # 编译应用(优化构建参数)
26
+ RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
27
+ go build -ldflags='-w -s -extldflags "-static"' \
28
+ -a -installsuffix cgo -o api-proxy .
29
 
30
  # 运行阶段
31
+ FROM alpine:3.18
32
 
33
+ # 安装运行时依赖
34
+ RUN apk --no-cache add ca-certificates wget tzdata && \
35
+ update-ca-certificates
36
 
37
  # 创建非root用户
38
  RUN addgroup -g 1001 -S apiproxy && \
39
+ adduser -u 1001 -S apiproxy -G apiproxy -s /bin/sh
40
 
41
+ # 设置工作目录
42
  WORKDIR /home/apiproxy/
43
 
44
  # 从构建阶段复制二进制文件
45
  COPY --from=builder /app/api-proxy .
46
 
47
+ # 从构建阶段复制静态文件和HTML文件
48
+ COPY --from=builder /app/index.html .
49
+ COPY --from=builder /app/static/ ./static/
50
+
51
  # 设置文件权限
52
+ RUN chown -R apiproxy:apiproxy . && \
53
+ chmod +x api-proxy
54
 
55
  # 切换到非root用户
56
  USER apiproxy
 
58
  # 暴露端口
59
  EXPOSE 8000
60
 
61
+ # 添加标签信息
62
+ LABEL maintainer="API Proxy Team" \
63
+ description="高性能API代理服务器" \
64
+ version="1.0.0"
65
+
66
  # 健康检查
67
+ HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
68
  CMD wget --no-verbose --tries=1 --spider http://localhost:8000/stats || exit 1
69
 
70
  # 启动应用
71
+ ENTRYPOINT ["./api-proxy"]
docker-compose.yml ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ version: '3.8'
2
+
3
+ services:
4
+ api-proxy:
5
+ build: .
6
+ container_name: api-proxy-server
7
+ ports:
8
+ - "8000:8000"
9
+ environment:
10
+ - PORT=8000
11
+ restart: unless-stopped
12
+ healthcheck:
13
+ test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:8000/stats"]
14
+ interval: 30s
15
+ timeout: 10s
16
+ retries: 3
17
+ start_period: 40s
18
+ networks:
19
+ - api-proxy-network
20
+
21
+ networks:
22
+ api-proxy-network:
23
+ driver: bridge
templates/styles.css ADDED
File without changes