解决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

效果

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


相关推荐
努力努力再努力wz1 小时前
【边缘计算入门系列】从“在哪里算”到“怎么算”:一文建立边缘计算、算子、计算图与 Tensor 的底层心智模型
人工智能·docker·边缘计算
江湖有缘2 小时前
3款开源IT工具箱整理合集,可Docker一键部署!
docker·容器·开源
旋生万物2 小时前
用螺旋数重写 Transformer Attention:让大模型自带“相位记忆“的 PyTorch 实现
docker·云原生·kubernetes·螺旋生成论·螺旋相位
编码如写诗3 小时前
【k8s】全新Ubuntu 26.04 使用kt 超简单安装 k8s 最新1.37.1+KubeSphere4.1.3
ubuntu·容器·kubernetes
小匠石钧知3 小时前
05_在k8s集群中安装NFS实现ReadWriteMany存储
java·容器·kubernetes·nfs·readwritemany·rwx
余槐i3 小时前
-m 2g 反而更早 OOM:Docker 内存计数与宿主机空闲口径差异
java·linux·docker·性能优化·cgroup
风华同学3 小时前
Docker镜像换源
运维·docker·容器
Stark-C4 小时前
IDM的免费替代,老牌下载器时隔三年再次更新,NAS部署Motrix
docker
谢亮_vipxieliang4 小时前
容器日志收集与管理:从 stdout 规范到 ELK/Loki 落地
运维·网络·人工智能·elk·docker·容器
羑悻的小杀马特4 小时前
Docker高阶实战:从Redis集群到C++微服务,全面解析镜像优化与生产环境部署+镜像制作常见问题详解
c++·redis·docker·镜像制作·dockefile