You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 10:22:46