docker 网络使用主机模式不起作用(无端口暴露)

问题描述 投票:0回答:2

这是我的信息:

➜  ~ docker -v
Docker version 19.03.5, build 633a0ea
➜  ~ uname -ar
Darwin Felixs-iMac.local 19.2.0 Darwin Kernel Version 19.2.0: Sat Nov  9 03:47:04 PST 2019; root:xnu-6153.61.1~20/RELEASE_X86_64 x86_64

当我创建一个具有这样的主机网络模式的docker时(复制自https://docs.docker.com/network/network-tutorial-host/

docker run --rm -d --network host --name my_nginx nginx

运行良好,我可以看到 docker 正在运行。 但问题是端口没有暴露。 当我检查

lsof -i:80
时,什么也没有。

➜  ~ docker run --rm -d --network host --name my_nginx nginx
282fe15a16b70d7bd38b770fd2b67f0023b8fce1f9852a91af4047ef8f6a3f0d
➜  ~ lsof -i:80
➜  ~ docker ps
CONTAINER ID        IMAGE                   COMMAND                  CREATED              STATUS              PORTS                               NAMES
282fe15a16b7        nginx                   "nginx -g 'daemon of…"   About a minute ago   Up About a minute

我检查了容器,一切看起来都很好:

➜  ~ docker inspect my_nginx
[
    {
        "Id": "282fe15a16b70d7bd38b770fd2b67f0023b8fce1f9852a91af4047ef8f6a3f0d",
        "Created": "2020-02-26T15:35:58.243963941Z",
        "Path": "nginx",
        "Args": [
            "-g",
            "daemon off;"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 2238,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-02-26T15:35:58.39452216Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:2073e0bcb60ee98548d313ead5eacbfe16d9054f8800a32bedd859922a99a6e1",
        "ResolvConfPath": "/var/lib/docker/containers/282fe15a16b70d7bd38b770fd2b67f0023b8fce1f9852a91af4047ef8f6a3f0d/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/282fe15a16b70d7bd38b770fd2b67f0023b8fce1f9852a91af4047ef8f6a3f0d/hostname",
        "HostsPath": "/var/lib/docker/containers/282fe15a16b70d7bd38b770fd2b67f0023b8fce1f9852a91af4047ef8f6a3f0d/hosts",
        "LogPath": "/var/lib/docker/containers/282fe15a16b70d7bd38b770fd2b67f0023b8fce1f9852a91af4047ef8f6a3f0d/282fe15a16b70d7bd38b770fd2b67f0023b8fce1f9852a91af4047ef8f6a3f0d-json.log",
        "Name": "/my_nginx",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "host",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": true,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Capabilities": null,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "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": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/062cb6ccb41fb22ba624fb8f93e99e79bcf02cd240fbb4998e47aab1380e015f-init/diff:/var/lib/docker/overlay2/587f9f458f8f0de65434971354636364ff685701fa1f8ba0ba7da4459e22540c/diff:/var/lib/docker/overlay2/1a94586d0949b8305ac14f0934a46149b31db3eb6e7125d7b2965deef318695d/diff:/var/lib/docker/overlay2/ac900d4637ed15f3337e999928ec2a9de2ef4a998f4ce53700a1dffc955d5f0e/diff",
                "MergedDir": "/var/lib/docker/overlay2/062cb6ccb41fb22ba624fb8f93e99e79bcf02cd240fbb4998e47aab1380e015f/merged",
                "UpperDir": "/var/lib/docker/overlay2/062cb6ccb41fb22ba624fb8f93e99e79bcf02cd240fbb4998e47aab1380e015f/diff",
                "WorkDir": "/var/lib/docker/overlay2/062cb6ccb41fb22ba624fb8f93e99e79bcf02cd240fbb4998e47aab1380e015f/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "docker-desktop",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.17.8",
                "NJS_VERSION=0.3.8",
                "PKG_RELEASE=1~buster"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "Image": "nginx",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <[email protected]>"
            },
            "StopSignal": "SIGTERM"
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "be4dc476e331bb8ac857ec069d0aa8383d7700ba5b917c0fed44c59d0b042c99",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/default",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "host": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "56456bd40ca98647fed01509408599accfadc7cb0f0077ea884b18fc009801b5",
                    "EndpointID": "e3e870245e2995fbfbae8706fa2daa19f3bf3d59d11d2f8ce2300f2b84b211e0",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": null
                }
            }
        }
    }
]

有人可以帮助我吗?

docker networking port host
2个回答
0
投票

如果您使用的是 MacOS,则主机网络模式将无法使用,原因是 docker-desktop 使用底层 Linux 虚拟机来运行 docker 容器,因此该主机模式仅对该 VM 的网络有效不适用于主机(MacOS 机器)。

如果您想从 docker 容器访问主机(MacOS)上运行的服务,那么您需要使用以下主机名才能连接到该服务:

host.docker.internal

您将使用上述主机名代替

localhost
127.0.0.1

有关此问题的进一步讨论,请查看此 github 问题


0
投票

Docker Desktop 在 4.34 及更高版本中添加了对“主机网络”的一般支持。要启用您必须登录的功能,请转到设置,在资源中选择网络,然后选中启用主机网络。必须重新启动 Docker Desktop 才能使更改生效

有关详细信息,请参阅文档

© www.soinside.com 2019 - 2024. All rights reserved.