Fork me on GitHub

docker容器如何访问宿主机的服务

127.0.0.1 指向的是本地地址,如果容器内访问 127.0.0.1:端口 是访问不到宿主机的服务,而是访问容器内的本地服务。

docker网关默认 172.17.0.1 指向宿主机。通过172.17.0.1:端口可访问到宿主机指定端口的服务。

通过命令docker network inspect bridge可以查看到详细网关信息。

[
    {
        "Name": "bridge",
        "Id": "b6d0297324427956faa344e88035e8f3eb574b129bc9656bec4e65e80e370be4",
        "Created": "2018-11-12T17:05:15.750987573+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        }
        ...
     }
]