【问题标题】:docker machine (virtualbox vm) export container port to host machine faildocker machine(virtualbox vm)将容器端口导出到主机失败
【发布时间】:2017-05-28 14:15:03
【问题描述】:

我正在使用 docker 工具箱进行设置:

  1. Windows 10 中的虚拟机默认虚拟机
  2. 在 virtualbox vm 中运行的容器

问题是我无法访问容器中的 Web 服务。

这是我使用的一些命令:

# start up the web service
docker run -p 8000:8000 -p 9000:9000 -t -i xxx/myweb /bin/bash -l -c '/root/myweb/demos/web/start-servers.sh'

# terminal print after above cmd
Starting the HTTP TLS server on port 8000
and the Secure WebSocket server on port 9000.

Access the demo through the HTTP server in your browser.
If you're running on the same computer outside of Docker, use https://localhost:8000
If you're running on the same computer with Docker, find the IP
address of the Docker container and use https://<docker-ip>:8000.
If you're running on a remote computer, find the IP address
and use https://<remote-ip>:8000.

WARNING: Chromium will warn on self-signed certificates. Please accept the certificate
and reload the app.

WebSocket Server: Logging to '/tmp/openface.websocket.log'

2017-05-27 15:10:13+0000 [-] Log opened.
2017-05-27 15:10:13+0000 [-] WebSocketServerFactory (TLS) starting on 9000
2017-05-27 15:10:13+0000 [-] Starting factory <autobahn.twisted.websocket.WebSocketServerFactory object at 0x7f00ef9f5c90>

现在我尝试使用以下地址访问,但都无法加载网页:

  1. 本地主机:8000
  2. 192.168.99.100:8000(virtualbox vm的ip)
  3. 172.17.0.2:8000(容器的ip)

以下是我使用的一些命令及其结果:

$ docker-machine config
--tlsverify
--tlscacert="C:\\Users\\AIT\\.docker\\machine\\machines\\default\\ca.pem"
--tlscert="C:\\Users\\AIT\\.docker\\machine\\machines\\default\\cert.pem"
--tlskey="C:\\Users\\AIT\\.docker\\machine\\machines\\default\\key.pem"
-H=tcp://192.168.99.100:2376

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                                          NAMES
5896815b991d        xxx/myweb  "/bin/bash -l -c /..."   12 minutes ago      Up 12 minutes       192.168.99.100:8000->8000/tcp, 192.168.99.100:9000->9000/tcp   thirsty_wiles

$ docker inspect thirsty_wiles
[
    {
        "Id": "5896815b991d394753b5eabdd4fe7967e2e4bd65791ffe7fa1d3e82486c9f050",
        "Created": "2017-05-27T15:10:12.172475859Z",
        "Path": "/bin/bash",
        "Args": [
            "-l",
            "-c",
            "/root/myweb/demos/web/start-servers.sh"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,                "Pid": 2753,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2017-05-27T15:10:12.294120929Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:3c9f00308ef150eef9d50576245df69234437a4360684f9b262a284f50dc15d7",
        "ResolvConfPath": "/mnt/sda1/var/lib/docker/containers/5896815b991d394753b5eabdd4fe7967e2e4bd65791ffe7fa1d3e82486c9f050/resolv.conf",
        "HostnamePath": "/mnt/sda1/var/lib/docker/containers/5896815b991d394753b5eabdd4fe7967e2e4bd65791ffe7fa1d3e82486c9f050/hostname",
        "HostsPath": "/mnt/sda1/var/lib/docker/containers/5896815b991d394753b5eabdd4fe7967e2e4bd65791ffe7fa1d3e82486c9f050/hosts",
        "LogPath": "/mnt/sda1/var/lib/docker/containers/5896815b991d394753b5eabdd4fe7967e2e4bd65791ffe7fa1d3e82486c9f050/5896815b991d394753b5eabdd4fe7967e2e4bd65791ffe7fa1d3e82486c9f050-json.log",
        "Name": "/thirsty_wiles",
        "RestartCount": 0,
        "Driver": "aufs",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "8000/tcp": [
                    {
                        "HostIp": "192.168.99.100",
                        "HostPort": "8000"
                    }
                ],
                "9000/tcp": [
                    {
                        "HostIp": "192.168.99.100",
                        "HostPort": "9000"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                30,
                120
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": -1,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0
        },
        "GraphDriver": {
            "Data": null,
            "Name": "aufs"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "5896815b991d",
            "Domainname": "",
            "User": "",
            "AttachStdin": true,
            "AttachStdout": true,
            "AttachStderr": true,
            "ExposedPorts": {
                "8000/tcp": {},
                "9000/tcp": {}
            },
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": true,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash",
                "-l",
                "-c",
                "/root/myweb/demos/web/start-servers.sh"
            ],
            "Image": "xxx/myweb",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "97f5c2894d9a6977723e3a54db77f7ff81150d8447722147a0d561a193d4caeb",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "8000/tcp": [
                    {
                        "HostIp": "192.168.99.100",
                        "HostPort": "8000"
                    }
                ],
                "9000/tcp": [
                    {
                        "HostIp": "192.168.99.100",
                        "HostPort": "9000"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/97f5c2894d9a",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "2f62e7e113d8ec9f0c6760f618f53f43821c469b3c461b7447011737ee768c65",
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,

            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "188e40f0cb38ff9be5f053cffdf5e97c22bfed8f7ee93c685178d0c9748a032c",
                    "EndpointID": "2f62e7e113d8ec9f0c6760f618f53f43821c469b3c461b7447011737ee768c65",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02"
                }
            }
        }
    }
]

docker 工具箱也有“默认”虚拟机网络设置的默认配置:

1:NAT

2:仅主机适配器(Virtualbox Host-Only Ethernet Adapter #2)。

我是 docker 工具箱的新手,我应该怎么做才能访问容器中的网页?你能帮我诊断一下我的问题吗?

【问题讨论】:

  • 你得到什么错误?检查是否有防火墙阻止该端口。此外,尝试通过 VM 的 IP 访问任何内容。在容器外运行的东西,例如 Python SimpleHTTPServer。
  • @vempo 当我使用 ip:port 访问网页时,我发现没有响应。并且在容器命令终端中也没有打印出任何连接。我应该如何检查防火墙? (谁的防火墙)。 VM的IP是指上面的192.168.99.100吗?我在这里使用 SimpleHTTPServer。你能指导我诊断问题吗?
  • 那么如果你在容器外的VM中运行SimpleHTTPServer,你能从宿主机访问它吗?例如,192.168.99.100:8080(您配置的端口)。您首先必须弄清楚问题出在哪里 - 主机/虚拟机或虚拟机/容器。

标签: web-services docker virtualbox boot2docker


【解决方案1】:

如果 VM(Docker 主机)正在运行 Linux,请尝试 iptables -Fiptables -t nat -F,然后从 Windows 的 Host-Only 或桥接模式网络重试,例如,如果 VM 的 Host Only 接口 IP 为 192.168.56.101,从 Windows 物理主机尝试 IP:8080。

【讨论】:

  • 我检查了 docker 工具箱是否在 VM 中运行 boot2docker 映像。默认情况下,docker toolbox 创建了一个名为“default”的虚拟机,并且网络配置为同时具有适配器 1:NAT 和适配器 2:仅主机适配器(Virtualbox Host-Only Ethernet Adapter #2)。请您帮忙诊断一下问题,我不知道下一步该去哪里。
  • 首先检查是否可以从 VM 访问 Host Only #2 网关。
【解决方案2】:

我假设您是在询问有关从运行 Virtualbox 的主机访问 Web 服务的问题。

如您所知,您可以使用 localhost: 或 : 从 boot2docker Linux 机器访问 web 服务。

要从您的主机访问 Web 服务,您可以使用 Virtualbox 提供的端口转发功能。您可以在boot2docker机器的NAT适配器下进入settings->Network->Port forwarding,在boot2docker主机端口和windows机器端口之间添加端口转发。 对于您的示例,您可以说 (hostip=127.0.0.1, host port=8000, guest port=8000)。之后,您可以使用 localhost:8000 或 127.0.0.1.8000 从您的主机访问网络服务器。

希望这会有所帮助。

问候 斯雷尼瓦斯

【讨论】:

猜你喜欢
  • 2013-07-20
  • 2016-07-17
  • 1970-01-01
  • 2017-06-20
  • 2014-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多