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

效果

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


相关推荐
好好沉淀6 小时前
Docker开发笔记(详解)
运维·docker·容器
禅口魔心7 小时前
Win10 + WSL2 + Docker:K510(DongshanPI-Vision)开发环境从踩坑到跑通全记录(交叉编译 + 上板运行)
docker·嵌入式开发·wsl2·k510
Ankie Wan8 小时前
cgroup(Control Group)是 Linux 内核提供的一种机制,用来“控制、限制、隔离、统计”进程对系统资源的使用。
linux·容器·cgroup·lxc
我的xiaodoujiao8 小时前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 44--将自动化测试结果自动推送至钉钉工作群聊
前端·python·测试工具·ui·pytest
Free Tester9 小时前
基于已有容器生成Dockerfile
docker
lcx_defender9 小时前
【Docker】Docker部署运行nacos
运维·docker·容器
啦啦啦小石头10 小时前
docker添加用户权限不使用sudo
运维·docker·容器
cuber膜拜10 小时前
Weaviate 简介与基本使用
数据库·python·docker·向量数据库·weaviate
xuefuhe10 小时前
Docker常用命令
docker
云草桑10 小时前
15分钟快速了解 Odoo
数据库·python·docker·postgresql·.net·odoo