无法连接到Docker网络中的其他容器

无法连接到Docker网络中的其他容器

问题描述:

我有几个容器化的项目,我想互相交流成一个名为'dev_network'的网络.

I have a few containerized projects I would like to talk to each other into a network called 'dev_network'.

我希望能够启动两个容器,并能够从容器内部ping它们..

I would expect to be able to start up both containers and be able to ping them from inside the containers..

例如我应该能够从酒吧ping foo ,但不能.

E.g. I should be able to ping foo from bar, but I cannot.

root@75cba11f489c:/# ping bar
ping: bar: Name or service not known

这些项目位于单独的文件夹中,但我希望它们能够连接到同一网络并能够彼此通话.

These projects live in separate folders, but I would like them to connect to the same network and be able to talk to each other.

这是显示问题的简单配置

Here's a simple configuration that shows the problem

有一个 foo 服务和一个 bar 服务.

There's a foo service and a bar service.

这是 foo 服务的 Dockerfile :

FROM debian:stretch-slim

RUN set -x  && apt-get update && apt-get install -y apt-utils
RUN set -x  && apt-get install -y iputils-ping

ENTRYPOINT ["/bin/bash"]

bar 服务的 Dockerfile 是相同的:

FROM debian:stretch-slim

RUN set -x  && apt-get update && apt-get install -y apt-utils
RUN set -x  && apt-get install -y iputils-ping

ENTRYPOINT ["/bin/bash"]

这是 foo 服务的 docker-compose.yml :

version: '3.5'
services:
  foo:
    image: foo
    build:
      context: .
      dockerfile: Dockerfile

networks:
  default:
    name: dev_network
    driver: bridge

bar 服务的唯一略有不同的 docker-compose.yml :

And the only slightly different docker-compose.yml for the bar service:

version: '3.5'
services:
  bar:
    image: bar
    build:
      context: .
      dockerfile: Dockerfile

networks:
  default:
    name: dev_network
    driver: bridge

启动foo服务:

docker-compose build; docker-compose run foo
Building foo
Step 1/4 : FROM debian:stretch-slim
 ---> bd04d03c4529
Step 2/4 : RUN set -x  && apt-get update && apt-get install -y apt-utils
 ---> Using cache
 ---> e53a746ba26b
Step 3/4 : RUN set -x  && apt-get install -y iputils-ping
 ---> Using cache
 ---> 63e2377e85d7
Step 4/4 : ENTRYPOINT ["/bin/bash"]
 ---> Using cache
 ---> c304ddfa1035

Successfully built c304ddfa1035
Successfully tagged foo:latest
root@680d2ef987d2:/# 

启动酒吧服务看起来是一样的:

Starting the bar service looks identical:

docker-compose build; docker-compose run bar
... same as above mostly

启动两项服务后,网络如下所示:

After starting the two services, the networks look like this:

docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
95c4f247d652        bridge              bridge              local
13e26f1434ba        dev_network         bridge              local
5fc09d998133        host                host                local
23acbaf51f4e        none                null                local

检查dev_network显示:

And inspecting the dev_network shows:

docker network inspect dev_network

docker network inspect dev_network

docker network inspect dev_network
[
    {
        "Name": "dev_network",
        "Id": "13e26f1434ba74c16964d29ceaa73eafd9df09622a1801c9fc9e49d2552273de",
        "Created": "2019-01-07T22:08:25.3589996Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.28.0.0/16",
                    "Gateway": "172.28.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "75cba11f489c8ad01ece0c5348d150fa6e64cdb5da31fe791e4f1c17284326c8": {
                "Name": "foo_foo_run_7faa90f193d0",
                "EndpointID": "f133df00bec7849c700669ef4395c5c45e82f6b6ab462f4f988508ac78d87d4b",
                "MacAddress": "02:42:ac:1c:00:02",
                "IPv4Address": "172.28.0.2/16",
                "IPv6Address": ""
            },
            "807fbb8527aacc5d5ac7ef35091ae589aa85a18401f99eb558bfbf07fa5826ef": {
                "Name": "bar_bar_run_45964b29fb9a",
                "EndpointID": "99001203fdb6f3f1e33b95cad53648c2a029dcda269a687f81ba443656d3d3c2",
                "MacAddress": "02:42:ac:1c:00:03",
                "IPv4Address": "172.28.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "dev_network",
            "com.docker.compose.project": "bar",
            "com.docker.compose.version": "1.23.2"
        }
    }
]

我找到了一种方法来实现自己想要的功能,但似乎无法通过docker-compose.yml进行配置

I found a way to do what I want, but it doesn't seem to be configurable via docker-compose.yml

我必须以这种方式启动服务:

I have to start the services in this way:

docker-compose build; docker-compose run --name foo --rm foo

docker-compose build; docker-compose run --name bar --rm bar

然后我可以在它们和网络之间ping通:

Then I can ping between them and the network looks like this:

docker network inspect dev_network

docker network inspect dev_network

...
    "Containers": {
        "94ae3d8789c2bbf77b1fd2b19a11a00c4fb32462cd20e47b36f0d8c910901c54": {
            "Name": "foo",
            "EndpointID": "c024a3ff8bf8d98454b05c45e95267508589868a20269552c8bae393d8bd5392",
            "MacAddress": "02:42:ac:1c:00:02",
            "IPv4Address": "172.28.0.2/16",
            "IPv6Address": ""
        },
        "d310999e759d000b9330177cf4ca483eafd66bcced333b12fe4ff5d0cebdefbc": {
            "Name": "bar",
            "EndpointID": "6cf92bd9f463a0c1d3e2fd749a8d9e8ae3f13219e1f29a01061a05e040cd485c",
            "MacAddress": "02:42:ac:1c:00:03",
            "IPv4Address": "172.28.0.3/16",
            "IPv6Address": ""
        }
    },