Python 无法从 Windows 容器内访问互联网

时间:2020-12-31 08:59:12

标签: python docker docker-for-windows windows-networking

TL;DR

Docker for windows 容器在ping 时可以访问互联网,但在容器内运行的python 不能。 这怎么可能?

详情

我已拉取图像:python:3.10.0a3-windowsservercore-1809 我可以使用以下命令启动一个基于它的容器:docker run -it --rm --dns 41.212.0.100 python:3.10.0a3-windowsservercore-1809 cmd 启动后,我可以 ping.com 以 0% 丢包

但是,当我尝试进行 pip install 或使用 urlib 模块或 sockets 模块访问 Internet 时,出现超时错误:

WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.HTTPSConnection object at 0x000001C7EA3C03D0>, 'Connection to pypi.org timed out. (connect timeout=15)')

我也尝试增加 pip 和 irllib 的超时参数,但它们仍然超时。

Docker 信息

Server:
 Images: 1
 Server Version: 20.10.0
 Storage Driver: windowsfilter
  Windows:
 Logging Driver: json-file
 Plugins:
  Volume: local
  Network: ics internal l2bridge l2tunnel nat null overlay private transparent
  Log: awslogs etwlogs fluentd gcplogs gelf json-file local logentries splunk syslog
 Swarm: inactive
 Default Isolation: hyperv
 Kernel Version: 10.0 17763 (17763.1.amd64fre.rs5_release.180914-1434)
 Operating System: Windows 10 Enterprise Version 1809 (OS Build 17763.1577)
 OSType: windows
 Architecture: x86_64
 CPUs: 4
 Total Memory: 7.885GiB
 Docker Root Dir: C:\ProgramData\Docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false
 Product License: Community Engine

Docker 检查容器

[
    {
     
       
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "ExitCode": 0,
            "Error": "",
        },
        "ResolvConfPath": "",
        "HostnamePath": "",
        "HostsPath": ""
      
        "Driver": "windowsfilter",
        "Platform": "windows",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": true,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Capabilities": null,
            "Dns": [
                "41.212.0.100"
            ],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 0,
            "ConsoleSize": [
                47,
                189
            ],
            "Isolation": "hyperv",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": null,
            "ReadonlyPaths": null
        },
        "GraphDriver": {
            "Data": {
                
            },
            "Name": "windowsfilter"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "32cfd271955e",
            "Domainname": "",
            "User": "",
            "AttachStdin": true,
            "AttachStdout": true,
            "AttachStderr": true,
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": true,
            "Env": [
                "PYTHONIOENCODING=UTF-8",
                "PYTHON_VERSION=3.10.0a3",
                "PYTHON_RELEASE=3.10.0",
                "PYTHON_PIP_VERSION=20.3.3",
                "PYTHON_GET_PIP_URL=https://github.com/pypa/get-pip/raw/5f38681f7f5872e4032860b54e9cc11cf0374932/get-pip.py",
                "PYTHON_GET_PIP_SHA256=6a0b13826862f33c13b614a921d36253bfa1ae779c5fbf569876f3585057e9d2"
            ],
            "Cmd": [
                "cmd"
            ],
            "Image": "python:3.10.0a3-windowsservercore-1809",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "32cfd271955eaa769cafbf8f22f34c6f95446ec4e3fb3da523b99ca334a67a4d",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "32cfd271955eaa769cafbf8f22f34c6f95446ec4e3fb3da523b99ca334a67a4d",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "nat": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "3983bd268d5212c421ad26a5d935bc3802f9ea6d351f7643a1481fa458f1a545",
                    "EndpointID": "4456e1ddf2de9646ee3324dd001394700f334f04fcbb24a772ed9156cbd54a52",
                    "Gateway": "172.29.96.1",
                    "IPAddress": "172.29.111.58",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "00:15:5d:b9:49:2a",
                    "DriverOpts": null
                }
            }
        }
    }
]

0 个答案:

没有答案