build: isolate go module proxy setting

This commit is contained in:
dela
2026-05-24 21:50:22 +08:00
parent 6f4e7f04cc
commit 622ff1d317
5 changed files with 15 additions and 9 deletions

View File

@@ -18,11 +18,12 @@ APP_ENV=production
BINANCE_BASE_URL=https://fapi.binance.com
# ---- Go module build ----
# 国内构建推荐 goproxy.cn海外可改为 https://proxy.golang.org,direct
GOPROXY=https://goproxy.cn,direct
# 国内构建推荐 goproxy.cn海外可改为 https://proxy.golang.org,direct
# 注意:这不是 HTTP 代理,不要填 http://127.0.0.1:7890。
GO_MODULE_PROXY=https://goproxy.cn,direct
# ---- 国内网络代理(如不需要可留空)----
# 例如 http://host.docker.internal:7890
# 例如 http://host.docker.internal:7890;不要填 127.0.0.1,容器里会指向容器自身。
HTTP_PROXY=
HTTPS_PROXY=
NO_PROXY=localhost,127.0.0.1,postgres,crypto-hermes-postgres

View File

@@ -39,7 +39,7 @@ guard:
@echo "G13.2 (compose fixed app port)..." && grep -q 'APP_HOST_PORT' docker-compose.yml && grep -q 'APP_PORT: "8080"' docker-compose.yml && ! grep -q '$${APP_PORT' docker-compose.yml
@echo "G13.3 (compose migrations gated)..." && grep -q 'service_healthy' docker-compose.yml && grep -q 'service_completed_successfully' docker-compose.yml
@echo "G13.4 (dockerignore protects env)..." && grep -qx '.env' .dockerignore && grep -qx '.env.\*' .dockerignore
@echo "G13.5 (docker build proxy explicit)..." && grep -q 'GOPROXY:' docker-compose.yml && grep -q 'ARG GOPROXY' Dockerfile && ! grep -q 'go mod download || true' Dockerfile
@echo "G13.5 (docker build proxy explicit)..." && grep -q 'GO_MODULE_PROXY' docker-compose.yml && grep -q 'ARG GOPROXY' Dockerfile && ! grep -q 'go mod download || true' Dockerfile
@echo "all guards green"
docker-up:

View File

@@ -108,8 +108,9 @@ curl -s 'localhost:8080/v1/market/context?symbol=BTCUSDT' | jq 'keys'
# 1. 准备环境变量
cp .env.example .env
# 编辑 .env必须填写 POSTGRES_PASSWORD留空时 compose 直接拒绝启动)
# 国内机器还要确认 GOPROXY并按需填写 HTTP_PROXY / HTTPS_PROXY
# 如果 .env 是旧文件补一行GOPROXY=https://goproxy.cn,direct
# 国内机器还要确认 GO_MODULE_PROXY并按需填写 HTTP_PROXY / HTTPS_PROXY
# 如果 .env 是旧文件补一行GO_MODULE_PROXY=https://goproxy.cn,direct
# HTTP_PROXY / HTTPS_PROXY 不要填 127.0.0.1Docker 容器访问宿主机代理用 host.docker.internal
# 2. 启动(自动建表)
docker compose up -d
@@ -234,7 +235,7 @@ collector:
| `POSTGRES_DSN` | 覆盖 yaml 中的 DSNDocker Compose 里就是这么干的) |
| `APP_PORT` | HTTP 端口compose 部署中容器内固定为 8080 |
| `APP_HOST_PORT` | docker compose 暴露到宿主机的 HTTP 端口 |
| `GOPROXY` | Docker build 阶段下载 Go module 使用的代理 |
| `GO_MODULE_PROXY` | Docker build 阶段下载 Go module 使用的 Go proxy不是 HTTP 代理 |
| `HTTPS_PROXY` | 走代理访问 Binance |
---

View File

@@ -230,7 +230,7 @@ grep -rn --include="*.go" "float64\|float32" internal/entity
- `.dockerignore` 必须排除 `.env``.env.*`,防止 `Dockerfile``COPY . .` 把 secrets 送进 build context / builder layer。
- app 容器内端口固定为 8080宿主机暴露端口只通过 `APP_HOST_PORT` 调整。不要在 compose 中用 `${APP_PORT}` 同时控制容器内监听和宿主机映射。
- app 必须等待 Postgres healthcheck 通过、migration init container 成功退出后再启动。
- Docker build 阶段必须显式传入 `GOPROXY` / HTTP(S) proxy build args`go mod download` 失败必须立刻失败,禁止 `go mod download || true`
- Docker build 阶段必须显式传入 `GO_MODULE_PROXY`(映射为 Dockerfile 内的 `GOPROXY`/ HTTP(S) proxy build args`go mod download` 失败必须立刻失败,禁止 `go mod download || true``GO_MODULE_PROXY` 只能填 Go module proxy`https://goproxy.cn,direct`),不能填本机 HTTP 代理地址。
**为什么**:这个服务虽然不接 Binance 私钥,但 DB 密码和代理凭据仍可能出现在 `.env`。同时 app 启动前若表不存在,会导致 collector/API 失败;端口变量混用会让健康检查和端口映射分叉。

View File

@@ -45,13 +45,15 @@ services:
context: .
dockerfile: Dockerfile
args:
GOPROXY: ${GOPROXY:-https://goproxy.cn,direct}
GOPROXY: ${GO_MODULE_PROXY:-https://goproxy.cn,direct}
HTTP_PROXY: ${HTTP_PROXY:-}
HTTPS_PROXY: ${HTTPS_PROXY:-}
NO_PROXY: ${NO_PROXY:-}
http_proxy: ${HTTP_PROXY:-}
https_proxy: ${HTTPS_PROXY:-}
no_proxy: ${NO_PROXY:-}
extra_hosts:
- "host.docker.internal:host-gateway"
container_name: crypto-hermes-market-gateway
depends_on:
postgres:
@@ -73,6 +75,8 @@ services:
volumes:
- ./config:/app/config:ro
- ./logs:/app/logs
extra_hosts:
- "host.docker.internal:host-gateway"
# SIGTERM → 等 app 内置 10s graceful shutdownfiber.ShutdownWithContext
# + cron 任务结束的缓冲 = 30s。超时后 docker 发 SIGKILL。
stop_grace_period: 30s