解决docker容器执行pytest命令报socket.gaierror: [Errno -2] Name or service not known 错误问题

问题背景

在docker容器中执行pytest测试的时候,测试容器的镜像,测试代码都完全下相同,唯一不同就是宿主机不一致,而其中一个宿主机报错提示:INTERNALERROR> socket.gaierror: [Errno -2] Name or service not known 完整报错如下:

bash 复制代码
2026-01-09 06:51:11,159 - root - INFO - test cmd: pytest  -v -s -l am/cases/scp/func/admin/users/user/test_ptadmin.py -m xtz_0109  --last-failed --last-failed-no-failures all -n 1 --reruns 0 --timeout 5400 --show-capture=stderr --dist=worksteal --durations=30 --alluredir ./allure-results
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/usr/local/lib/python3.10/site-packages/_pytest/main.py", line 285, in wrap_session
INTERNALERROR>     config._do_configure()
INTERNALERROR>   File "/usr/local/lib/python3.10/site-packages/_pytest/config/__init__.py", line 1119, in _do_configure
INTERNALERROR>     self.hook.pytest_configure.call_historic(kwargs=dict(config=self))
INTERNALERROR>   File "/usr/local/lib/python3.10/site-packages/pluggy/_hooks.py", line 535, in call_historic
INTERNALERROR>     res = self._hookexec(self.name, self._hookimpls.copy(), kwargs, False)
INTERNALERROR>   File "/usr/local/lib/python3.10/site-packages/pluggy/_manager.py", line 120, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR>   File "/usr/local/lib/python3.10/site-packages/pluggy/_callers.py", line 139, in _multicall
INTERNALERROR>     raise exception.with_traceback(exception.__traceback__)
INTERNALERROR>   File "/usr/local/lib/python3.10/site-packages/pluggy/_callers.py", line 103, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/usr/local/lib/python3.10/site-packages/pytest_rerunfailures.py", line 337, in pytest_configure
INTERNALERROR>     config.failures_db = ServerStatusDB()
INTERNALERROR>   File "/usr/local/lib/python3.10/site-packages/pytest_rerunfailures.py", line 426, in __init__
INTERNALERROR>     self.sock.bind(("localhost", 0))
INTERNALERROR> socket.gaierror: [Errno -2] Name or service not known

排查思路

通过报错位置知道,是网络套接字绑定loaclhost的时候出错,于是首先怀疑是域名解析的时候有问题,于是在出问题的宿主机与正常的宿主机中分别执行 ping localhost 命令,并且创建测试容器在测试容器中执行 ping localhost 命令看 localhost 被解析到的地址,结果发现宿主机解析的地址都是IPv4格式,而正常的容器内也是IPv4格式,异常的容器内解析到的地址缺少IPv6格式,效果如下

bash 复制代码
# 正常 IPv4格式
ping localhost
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.038 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.049 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.051 ms

# 异常IPv6格式
ping localhost
PING localhost(localhost (::1)) 56 data bytes
64 bytes from localhost (::1): icmp_seq=1 ttl=64 time=0.026 ms
64 bytes from localhost (::1): icmp_seq=2 ttl=64 time=0.047 ms
64 bytes from localhost (::1): icmp_seq=3 ttl=64 time=0.048 ms

问题原因

使用的pytest版本还不支持IPv6,所以pytest在尝试绑定 socket 时遇到问题,pytest期望使用 IPv4。

解决方案

方案一

由于在宿主机上执行ping localhost解析到的是IPv4,所以可以在创建容器命令的时候携带 --network host 参数,使用宿主机的网络解决该问题
docker run -it --rm --network host {images}:{tag} /bin/bash

方案二

在拉起测试容器,执行pytest命令之前,修改容器内/etc/hosts文件,指定localhost域名ip为127.0.0.1
echo "127.0.0.1 localhost" > /etc/hosts

效果

使用上述两种方案中的任意一种,成功解决该问题


相关推荐
Tronlong创龙5 小时前
SBC-TL3588 单板机 / EPC-TL3588 工控机 Docker 开发手册(二)
docker·开发板·嵌入式开发·硬件开发·工业控制
AI服务老曹9 小时前
视觉算法模型管理完整流程:多版本上线、灰度发布与回滚的落地实践
人工智能·docker·音视频
飞翔沫沫情11 小时前
K8s Alloy 采集 Pod 控制台日志
云原生·容器·kubernetes
辉的技术笔记11 小时前
拆解 Dify 的 Celery:15 个队列都在干什么
docker
潮起鲸落入海17 小时前
Kubernetes Metric Server, Quota and Limits
容器·kubernetes
梦想的颜色17 小时前
Docker 容器化本地部署 Claude Code 完整硬核教程|隔离沙盒 + 国产模型直连 + 持久化 Skill
运维·容器·ai 工程化·沙盒隔离·docker 本地部署·claude code硬核实战·国内国产模型接入
渣渣盟17 小时前
Docker 运维常用命令手册(含扩容与实战)
运维·docker·容器
小尘要自信18 小时前
小米摄像头怎么接入RTSP?Go2RTC转流、EasyNVR录像与公网访问教程
人工智能·docker·开源
我叫张小白。19 小时前
一个微服务电商+社区项目(瓷韵app)的技术深度复盘
docker·微服务·云原生·架构
咕噜咕噜的猪大侠19 小时前
《K8s 核心知识梳理:Deployment 滚动更新、JobCronJob、DaemonSet 和 Service》
云原生·容器·kubernetes