Docker启用host网络且VPN开启时gethostid返回0的异常问题
Docker容器连接SonicWall VPN时gethostid异常问题
环境信息
- 主机操作系统:Ubuntu 20.04
- 容器操作系统:Ubuntu 20.04
- VPN软件:SonicWall NetExtender 10.2.845(8.6.801版本也可复现该问题)
- Docker运行命令:
docker run -it --rm --network="host" test_gethostid_vpn:latest
问题现象
连接办公室VPN后,以--network="host"模式运行的Docker容器中,调用gethostid()函数会耗时5秒并返回0。移除--network="host"参数或关闭NetExtender VPN后,该问题消失。
测试代码及配置文件
main.cpp
#include <iostream> #include <cstring> #include <unistd.h> #include <chrono> int main(int* argc, char** argv) { auto startPoint = std::chrono::steady_clock::now(); long hostid = gethostid(); auto endPoint = std::chrono::steady_clock::now(); auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(endPoint - startPoint).count(); std::cout << "duration = " << duration << " [ms]" << std::endl; std::cout << "hostid = " << hostid << std::endl; std::cout << "errno = " << std::strerror(errno) << std::endl; return 0; }
CMakeLists.txt
cmake_minimum_required(VERSION 3.16) project(test_gethostid) set(CMAKE_CXX_STANDARD 17) add_executable(test_gethostid main.cpp)
Dockerfile
FROM ubuntu:20.04 # Non interactive apt ENV TZ=Europe/Paris ARG DEBIAN_FRONTEND=noninteractive RUN apt-get update && apt-get upgrade -y && apt-get install -y cmake build-essential COPY . /testcpp WORKDIR /testcpp/build RUN cmake .. RUN make -j $(nproc) ENTRYPOINT [ "./test_gethostid" ]
内容的提问来源于stack exchange,提问作者Mimi
相关产品推荐
相关产品推荐

