linux 如何从localhost连接到docker容器

r7xajy2e  于 2023-08-03  发布在  Linux
关注(0)|答案(4)|浏览(132)

我创建了一个运行2个java进程的docker容器,其中一个进程正在监听端口4444,应该可以通过浏览器访问以下URL:
http://{host}:4444/grid/console
如何从主机上的浏览器中点击此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
                }
            }
        }
    }
]

字符串

myzjeezk

myzjeezk1#

从问题的输出中可以看到,容器的IP地址是172.17.0.2。将您的浏览器指向http://172.17.0.2:4444
或者,您可以将其转发到主机端口,如:

docker run -p 4444:4444 ...

字符串
然后在您的主机上运行,您可以访问localhost:4444

kyks70gy

kyks70gy2#

您必须将4444端口绑定到主机的端口。您可以在run命令中使用-p [hostport]:[containerport]
示例:docker run -p 8081:80 php:5.6-apache

ekqde3dh

ekqde3dh3#

也有同样的问题。将--host“string”添加到我的docker - compose。

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

字符串
您可以阅读更多关于主机和Docker容器here之间的连接。

oprakyz7

oprakyz74#

根据this question,我想将运行在本地主机(而不是浏览器)上的Java应用程序连接到运行在docker中的另一个应用程序。我使用相关端口将本地应用程序目标URL设置为host.docker.internal,它可以很好地连接到docker应用程序。

相关问题