如何从localhost连接到docker容器

时间:2018-02-22 15:59:44

标签: linux docker selenium-grid docker-machine

我创建了一个运行2个java进程的docker容器 - 其中一个进程正在侦听端口4444,并且应该可以从浏览器访问以下URL:

的http:// {主机}:4444 /网格/控制台

如何从主机上的浏览器中点击此URL - 连接到端口4444上的容器?

这是docker inspect的回应:

[
    {
        "Id": "3bef855324d8c78fcd3a7ac4e52d1641437f221ebe64af5651641d776cfa1bde",
        "Created": "2018-02-22T14:54:37.025854258Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 12320,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2018-02-22T14:54:37.487535484Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:422dc563ca3260ad9ef5c47a1c246f5065d7f177ce51f4dd208efd82967ff182",
        "ResolvConfPath": "/var/lib/docker/containers/3bef855324d8c78fcd3a7ac4e52d1641437f221ebe64af5651641d776cfa1bde/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/3bef855324d8c78fcd3a7ac4e52d1641437f221ebe64af5651641d776cfa1bde/hostname",
        "HostsPath": "/var/lib/docker/containers/3bef855324d8c78fcd3a7ac4e52d1641437f221ebe64af5651641d776cfa1bde/hosts",
        "LogPath": "/var/lib/docker/containers/3bef855324d8c78fcd3a7ac4e52d1641437f221ebe64af5651641d776cfa1bde/3bef855324d8c78fcd3a7ac4e52d1641437f221ebe64af5651641d776cfa1bde-json.log",
        "Name": "/hint-env",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "shareable",
            "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,
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/aa6ee2cab028cc33b0af19c43a7da3977ef0cef29f3aa02536844955abff3cba-init/diff:/var/lib/docker/overlay2/3eb4156ba1e3e5ad6b066c1cb82c2547a7dee07afb3e0e64a41689f3c02f66fd/diff",
                "MergedDir": "/var/lib/docker/overlay2/aa6ee2cab028cc33b0af19c43a7da3977ef0cef29f3aa02536844955abff3cba/merged",
                "UpperDir": "/var/lib/docker/overlay2/aa6ee2cab028cc33b0af19c43a7da3977ef0cef29f3aa02536844955abff3cba/diff",
                "WorkDir": "/var/lib/docker/overlay2/aa6ee2cab028cc33b0af19c43a7da3977ef0cef29f3aa02536844955abff3cba/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "3bef855324d8",
            "Domainname": "",
            "User": "",
            "AttachStdin": true,
            "AttachStdout": true,
            "AttachStderr": true,
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": true,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "DISTTAG=f27container",
                "FGC=f27",
                "FBR=f27"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "Image": "422dc563ca32",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "28bb071b33999759e2f523bb7e99e5de0ef23922f32b17cf0135ac8a389a40bc",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/28bb071b3399",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "12a5101b68c18ec32bc0b76ab95bb0960993390f7c420e040b39b1cad45eeb4b",
            "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": "f385270bd9603551c9eca046222ff0b9c7147a761f06c2095343b7960515eafb",
                    "EndpointID": "12a5101b68c18ec32bc0b76ab95bb0960993390f7c420e040b39b1cad45eeb4b",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

3 个答案:

答案 0 :(得分:2)

您必须将4444端口绑定到主机的端口。您可以使用-p [hostport]:[containerport]

在运行命令中执行此操作

例如:docker run -p 8081:80 php:5.6-apache

答案 1 :(得分:2)

从问题的输出中可以看出,容器的IP地址是172.17.0.2。只需将浏览器指向http://172.17.0.2:4444

即可

或者,您可以将其转发到主机端口,如:

docker run -p 4444:4444

然后在您的主机上,您可以访问localhost:4444

答案 2 :(得分:0)

有同样的问题。将--host“ string”添加到我的泊坞窗-编写。

command: uvicorn main:app --reload --port 8000 --host 0.0.0.0

您可以阅读有关主机和Docker容器here之间的连接的更多信息。