如何从本地主机连接到Docker容器

如何从本地主机连接到Docker容器

本文介绍了如何从本地主机连接到Docker容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created a docker container which is running 2 java processes - one of the processes is listening on port 4444 and should have access from browser hitting the following URL:

http:// {host}:4444 / grid / console

http://{host}:4444/grid/console

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

How do I hit this URL from my browser on the host - connect to the container on port 4444?

这是docker inspect的响应:

This is the response from 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
                }
            }
        }
    }
]


推荐答案

从问题输出中可以看到,容器的ip地址为172.17.0.2。只需将您的浏览器指向。

As you can see from the output in your question, your container's ip address is 172.17.0.2. Just point your browser at http://172.17.0.2:4444.

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

Alternately, you could forward this to a host port, as in:

docker run -p 4444:4444 ...

然后在主机运行时,您可以访问 localhost:4444

And then on your host run you could access localhost:4444.

这篇关于如何从本地主机连接到Docker容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 23:29