From a80dc1e69d7f6bd0cd1aad6048b9778b904d58a5 Mon Sep 17 00:00:00 2001 From: huang Date: Tue, 20 Jan 2026 11:50:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AE=B9=E5=99=A8=E5=81=A5?= =?UTF-8?q?=E5=BA=B7=E6=A3=80=E6=9F=A5=E5=A4=B1=E8=B4=A5=E7=9A=84=E6=A0=B8?= =?UTF-8?q?=E5=BF=83=E9=97=AE=E9=A2=98=EF=BC=9AIPv6=20vs=20IPv4=20?= =?UTF-8?q?=E5=92=8C=E6=9D=83=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题诊断(本地运行镜像验证): 1. 服务实际已启动在 3000 端口 2. 从宿主机访问健康检查 ✅ 成功 3. 容器内部 wget localhost:3000 ❌ 失败 (Connection refused) 4. 健康检查尝试连接 [::1]:3000 (IPv6),但 Fiber 只监听 IPv4 根本原因: - wget localhost 优先解析为 IPv6 地址 [::1] - Fiber 默认监听 0.0.0.0:3000 (仅 IPv4) - Docker 健康检查失败 → 容器标记 unhealthy → Worker 无法启动 修复内容: 1. Dockerfile.api 健康检查: localhost → 127.0.0.1 (强制 IPv4) 2. docker-compose.prod.yml 健康检查: 同步修改 3. Dockerfile.api: 创建 logs 目录并设置 appuser 权限 4. cmd/api/main.go: OpenAPI 文档路径改为 logs/openapi.yaml --- Dockerfile.api | 7 +++++-- cmd/api/main.go | 2 +- docker-compose.prod.yml | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Dockerfile.api b/Dockerfile.api index 814f30d..2813512 100644 --- a/Dockerfile.api +++ b/Dockerfile.api @@ -67,15 +67,18 @@ COPY migrations /app/migrations COPY docker/entrypoint-api.sh /app/entrypoint.sh RUN chmod +x /app/entrypoint.sh +# 创建日志目录并设置权限(在切换用户前) +RUN mkdir -p /app/logs && chown -R appuser:appuser /app/logs + # 切换到非 root 用户 USER appuser # 暴露端口 EXPOSE 3000 -# 健康检查(使用 Alpine 自带的 wget) +# 健康检查(使用 127.0.0.1 强制 IPv4,避免 IPv6 连接问题) HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \ - CMD wget --no-verbose --tries=1 --spider http://localhost:3000/health || exit 1 + CMD wget --no-verbose --tries=1 --spider http://127.0.0.1:3000/health || exit 1 # 启动命令 ENTRYPOINT ["/app/entrypoint.sh"] diff --git a/cmd/api/main.go b/cmd/api/main.go index 94fea28..aa181b9 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -81,7 +81,7 @@ func main() { initRoutes(app, cfg, result, queueClient, db, redisClient, appLogger) // 12. 生成 OpenAPI 文档 - generateOpenAPIDocs("./openapi.yaml", appLogger) + generateOpenAPIDocs("logs/openapi.yaml", appLogger) // 13. 启动服务器 startServer(app, cfg, appLogger, cancelWatch) diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index bc9ff17..914d75c 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -20,7 +20,7 @@ services: networks: - junhong-network healthcheck: - test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:3000/health"] + test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://127.0.0.1:3000/health"] interval: 30s timeout: 3s retries: 3