问题背景
在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
效果
使用上述两种方案中的任意一种,成功解决该问题