Postgres Test Containers在GitHub Actions中突然失效求助
GitHub Actions中Postgres Test Container集成测试失败排查
问题背景
我正在使用Postgres Test Container模块为Go语言API编写数据库集成测试,通过GitHub Actions执行CI环节。该配置正常运行一周后,今早CI突然无征兆失败,但本地macOS机器上测试仍可正常执行。
测试代码
pgContainer, err := postgres.RunContainer(ctx, testcontainers.WithImage("postgres:14-alpine"), postgres.WithDatabase("db"), postgres.WithUsername("user"), postgres.WithPassword("secret"), testcontainers.WithWaitStrategy( wait.ForLog("database system is ready to accept connections"). WithOccurrence(2).WithStartupTimeout(5*time.Second)), ) if err != nil { t.Fatal(err) } ... // 通过连接字符串获取上述容器的连接池 // 在测试中使用连接池
CI报错日志
2023/07/12 13:59:56 github.com/testcontainers/testcontainers-go - Connected to docker: Server Version: 20.10.25+azure-2 API Version: 1.41 Operating System: Ubuntu 22.04.2 LTS Total Memory: 6931 MB 2023/07/12 13:59:57 Failed to get image auth for docker.io. Setting empty credentials for the image: docker.io/testcontainers/ryuk:0.5.1. Error is:credentials not found in native keychain 2023/07/12 13:59:59 🐳 Creating container for image docker.io/testcontainers/ryuk:0.5.1 2023/07/12 13:59:59 ✅ Container created: 85df4c97985a 2023/07/12 13:59:59 🐳 Starting container: 85df4c97985a 2023/07/12 14:00:00 ✅ Container started: 85df4c97985a 2023/07/12 14:00:00 🚧 Waiting for container id 85df4c97985a image: docker.io/testcontainers/ryuk:0.5.1. Waiting for: &{Port:8080/tcp timeout:<nil> PollInterval:100ms} 2023/07/12 14:00:00 container logs: 2023/07/12 14:00:00 Pinging Docker... 2023/07/12 14:00:00 Docker daemon is available! 2023/07/12 14:00:00 Starting on port 8080... 2023/07/12 14:00:00 Started! 2023/07/12 14:00:00 New client connected: 172.17.0.1:35910 2023/07/12 14:00:00 EOF 2023/07/12 14:00:00 Client disconnected: 172.17.0.1:35910
可能原因
- GitHub Actions Runner资源不足:CI环境的内存/CPU资源有限,Ryuk容器启动后因资源不足异常终止,导致与testcontainers-go的连接断开(日志中出现
EOF和客户端断开提示)。 - Docker版本兼容性问题:CI中使用的Docker版本为
20.10.25+azure-2,可能与当前testcontainers-go版本存在兼容性冲突,而本地Docker版本较新无此问题。 - Ryuk通信异常:虽然Ryuk容器启动成功,但网络波动或runner的网络配置限制导致testcontainers-go与Ryuk的连接中断。
- 等待超时设置过短:Postgres的5秒启动超时在CI环境下可能不足以完成初始化,间接引发后续容器管理流程异常。
调试建议
- 升级testcontainers-go版本:检查是否有新版本修复了Ryuk与旧Docker版本的兼容性问题,更新依赖后重新测试。
- 禁用Ryuk容器:在GitHub Actions workflow中添加环境变量
TESTCONTAINERS_RYUK_DISABLED=true,跳过Ryuk的启动(CI runner为一次性环境,无需担心容器残留问题)。 - 调整资源配置:在workflow中指定大内存的runner(如
ubuntu-latest的高配置实例),或为Docker容器分配更多资源。 - 延长等待超时:将Postgres的启动超时从5秒调整为10秒以上,例如:
wait.ForLog("database system is ready to accept connections"). WithOccurrence(2).WithStartupTimeout(10*time.Second) - 开启debug日志:在CI中设置环境变量
TESTCONTAINERS_LOG_LEVEL=debug,获取testcontainers-go与Docker、Ryuk交互的详细日志,定位具体异常点。 - 验证Ryuk单独运行:在CI环节中手动启动Ryuk容器,测试是否能正常建立连接,排除容器本身的问题。
内容的提问来源于stack exchange,提问作者Kshitij Saraogi
相关产品推荐
相关产品推荐

