user2608576
user2608576

Reputation: 807

connection between 2 docker containers using docker compose

I have a 2 docker containers "restorecms_facade_1" (facade service) and "restorecms_identity_1" (identity service) running on same bridge "restorecms_default".

I have specified the host name as 'hostname: identity-srv' for identity service image in my docker compose file

The facade service has 5000 port exposed and it accepts my graphQL request. This facade service will delegate the request to identity service ( dns name 'identity-srv') running on port 50051 but the request is currently timing out on facade service.

My docker container and bride network details are below.

I cannot even ping from facade service to identity service (but the other way around is possible).

Am I missing something here or do I need to add anything on facade service so that the connection goes through to identity service ?

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 inspect:

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"
        }
    }
]

Hostnames and 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

My current docker-compose.yml (for identity and facade service) config are below:

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

Upvotes: 1

Views: 849

Answers (2)

Bret Fisher
Bret Fisher

Reputation: 8596

links are legacy. The modern way for years now has been to ensure you have version 2 at top of your compose file (latest schema version is 3.6 but I'll keep it at 2 for this example) and then all containers on a custom bridge network are always kept in DNS and accessible from each other on that network. Docker provides each virtual network a private DNS server and no longer uses hosts file for name resolution of containers on the same docker network.

Also note that service names are their default DNS name, so it's easiest just to keep them the same so you won't an explicit hostname key/value.

This should work. You'll be able to ping identity from facade and vice versa:

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

Upvotes: 2

Mateusz Moneta
Mateusz Moneta

Reputation: 1640

Hostname only sets hostname "inside" the container. If you want to ping it from another one you need to use alias (default to service name) which you used in your links section.

ping identity if you want use identity-srv you can use links this way: ... links: - identity:identity-srv ...

Upvotes: 0

Related Questions