使用docker compose在2个docker容器之间建立连接

时间:2017-06-08 10:13:28

标签: docker docker-compose docker-networking

我在同一个网桥“restorecms_default”上运行了2个docker容器“restorecms_facade_1”(外观服务)和“restorecms_identity_1”(身份服务)。

我已在docker compose文件中为身份服务图像指定主机名为'hostname:identity-srv'

外观服务暴露了5000端口,它接受我的graphQL请求。 此Facite服务将委托请求到端口50051上运行的身份服务(dns名称'identity-srv'),但请求当前正在服务外观服务。

我的码头工人容器和新娘网络详情如下。

我甚至无法从门面服务ping身份服务(但反过来也可以)。

我在这里遗漏了什么,或者我是否需要在外观服务上添加任何内容以便连接进入身份服务?

Docker ps:

akumar@client3 /restore $ docker ps
CONTAINER ID        IMAGE                               COMMAND                  CREATED             STATUS                    PORTS                                                NAMES
4a8854ce4876        xxx/facade-srv          "node lib/index.js"      41 minutes ago      Up 41 minutes             0.0.0.0:5000->5000/tcp                               restorecms_facade_1
00f1f00ae2a6        xxx/identity-srv        "node service.js"        41 minutes ago      Up 41 minutes (healthy)   0.0.0.0:50051->50051/tcp                             restorecms_identity_1

Docker检查:

akumar@client3 /restore/identity-srv-TypeScript $ docker inspect restorecms_default 
[
    {
        "Name": "restorecms_default",
        "Id": "102358eab67884f7d39b78fd0bcf1050499d3dc667eddab5e15086633185837d",
        "Created": "2017-06-08T10:40:59.672964582+02:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "Containers": {
            "00f1f00ae2a64f489a530a63a9fb57711618d5e67769b739db514b5f20b73d36": {
                "Name": "restorecms_identity_1",
                "EndpointID": "d005cdff65479817bf769e4b60a18769c40d9d9cae396f3c735c2e497d6e08a4",
                "MacAddress": "02:42:ac:12:00:09",
                "IPv4Address": "172.18.0.9/16",
                "IPv6Address": ""
            },
            "4a8854ce487698149072c224378f697e9309e32649f6010d2d8c4cc4f0bb3f42": {
                "Name": "restorecms_facade_1",
                "EndpointID": "2509c8e1444d27cbe8a3188412fcbfb1aab103ec2366c22f3ad614c684ef87ab",
                "MacAddress": "02:42:ac:12:00:0a",
                "IPv4Address": "172.18.0.10/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "default",
            "com.docker.compose.project": "restorecms"
        }
    }
]

主机名和ping:

root@identity-srv:/# cat /etc/hostname 
identity-srv

root@identity-srv:/# ping 4a8854ce4876
PING 4a8854ce4876 (172.18.0.10): 48 data bytes
56 bytes from 172.18.0.10: icmp_seq=0 ttl=64 time=0.241 ms
56 bytes from 172.18.0.10: icmp_seq=1 ttl=64 time=0.149 ms
56 bytes from 172.18.0.10: icmp_seq=2 ttl=64 time=0.165 ms
^C--- 4a8854ce4876 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.149/0.185/0.241/0.040 ms


root@4a8854ce4876:/# cat /etc/hostname 
4a8854ce4876

root@4a8854ce4876:/# ping identity-srv
ping: unknown host

我当前的docker-compose.yml(用于身份和外观服务)配置如下:

identity:
    hostname: identity-srv
    image: xxx/identity-srv
    ports:
      - "50051:50051"
    depends_on:
      arangodb:
        condition: service_healthy
    links:
      - arangodb
      - kafka
    healthcheck:
      test: "exit 0"

  # Facade service
  facade:
    image: xxx/facade-srv
    ports:
      - "5000:5000"
    depends_on:
      identity:
        condition: service_healthy
      #resource:
      #  condition: service_healthy
    links:
      - identity
      - kafka
      - elasticsearch
      - arangodb
      - redis

2 个答案:

答案 0 :(得分:2)

links are legacy。多年来现代的方法是确保你的compose文件顶部有版本2(最新的架构版本是3.6但是我将它保持在2这个例子)然后自定义桥接网络上的所有容器都保持不变在DNS中,可以在该网络上相互访问。 Docker为每个虚拟网络提供一个私有DNS服务器,不再使用hosts文件来解析同一个docker网络上容器的名称。

另请注意,服务名称是它们的默认DNS名称,因此最简单的方法就是保持它们不变,这样您就不会有明确的主机名键/值。

这应该有效。您可以从identity ping facade,反之亦然:

version: '2'
services:
  identity:
    image: xxx/identity-srv
    ports:
      - "50051:50051"
    depends_on:
      arangodb:
        condition: service_healthy
    healthcheck:
      test: "exit 0"

  facade:
    image: xxx/facade-srv
    ports:
      - "5000:5000"
    depends_on:
      identity:
        condition: service_healthy

答案 1 :(得分:0)

主机名仅在容器内“设置”主机名。如果您想从另一个ping它,则需要使用您在links部分中使用的别名(默认为服务名称)。

ping identity 如果你想使用identity-srv,你可以这样使用链接: ... links: - identity:identity-srv ...