Docker:一个网络中容器之间的请求

时间:2019-05-15 14:33:51

标签: docker

我有2个容器-后端和前端。我使用以下命令在远程服务器上运行它们:

docker run -p 3000:3000 xpendence/api-checker:0.0.1
docker run -p 8099:8099 --name rebounder-backend-0017a xpendence/rebounder-chain-backend:0.0.17

如文档所述,默认情况下,容器连接到“桥接”网络。我在里面看到这个容器:

# docker network inspect bridge
[
    {
        "Name": "bridge",
        "Id": "27f9d6240b4022b6ccbfff93daeff32d2639aa22f7f2a19c9cbc21ce77b435",
        "Created": "2019-05-12T12:26:35.903309613Z",
        "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": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "82446be7a9254c79264d921059129711f150a43ac412700cdc21eb5312522ea4": {
                "Name": "rebounder-backend-0017a",
                "EndpointID": "41fb5be38cff7f052ebbbb9d31ee7b877f664bb620b3063e57cd87cc6c7ef5c9",
                "MacAddress": "03:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            },
            "da82a03c5d3bfe26cbd750da7f8872cf22dc9d43117123b9069e9ab4e17dbce6": {
                "Name": "elastic_galileo",
                "EndpointID": "13878a6db60ef854dcbdf6b7e729817a1d96fbec6364d0c18d7845fcbc040222",
                "MacAddress": "03:42:ac:11:00:03",
                "IPv4Address": "172.17.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {}
    }

我将请求从前端发送到后端,但没有到达:

GET http://localhost:8099/log net::ERR_CONNECTION_REFUSED
GET http://172.17.0.2:8099/log net::ERR_ADDRESS_UNREACHABLE
GET http://172.17.0.2/16:8099/log net::ERR_ADDRESS_UNREACHABLE
GET http://0.0.0.0:8099/log net::ERR_CONNECTION_REFUSED

请给我建议,如何解决问题?

从外部请求后端是可以的。

1 个答案:

答案 0 :(得分:2)

尽管您的两个容器链接到同一默认桥,但这并不意味着它们可以互相访问。

过去,我们建议使用--link来使容器直接相互交谈,而无需主持人参与,但是现在不建议使用。

相反,您需要使用user-defined bridge

  • 连接到同一用户定义网桥网络的容器会自动将所有端口彼此公开。
  • 用户定义的网桥可在容器之间提供自动DNS解析。

步骤如下:

docker network create my-net
docker run --network my-net -p 3000:3000 xpendence/api-checker:0.0.1
docker run --network my-net -p 8099:8099 --name rebounder-backend-0017a xpendence/rebounder-chain-backend:0.0.17

official guide

的详细引用