[RESOLVED] Can't access container by service name in swarm mode

I’m using docker stack deploy to deploy two simple service on two servers. But when I inside a container to ping others by service name, it says ping: bad address 'worker'.

And I’m sure the port that swarm mode needed is all available (Port 2377 for manager node, Port 7946 TCP/UDP, Port 4789 UDP), but I still can’t access service by service name.

Here is my docker-compose files:

# manager.yml
# Deploy on 'manager' server 
version: "3.5"
services:
  manager:
    image: alpine:3.8
    command: 
      top
# worker.yml
# Deploy on 'worker' server 
version: "3.5"
services:
  worker:
    image: alpine:3.8
    deploy:
      placement:
        constraints:
          - node.labels.role==lbworker1
    command: 
      top

Here is my docker node ls output:

ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
qbhyc7tgtg6d5v6so5z1kjvqh     change-lbworker1    Ready               Active                                  18.09.0
od44fnagwh9h1bqvolp9wg28d *   change-manager      Ready               Active              Leader              18.09.0

Here is my docker stack ps output:

[root@change-manager project]# docker stack ps worker
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
bdj1j0xrewon        worker_worker.1     alpine:3.8          change-lbworker1    Running             Running 23 seconds ago                       

[root@change-manager project]# docker stack ps manager
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
lge7x4ol17sr        manager_manager.1   alpine:3.8          change-manager      Running             Running 39 seconds ago 

I found out I have to use the same stack name (service stack deploy -c docker-compose.yml same-stack-name), so that containers can access to each other.:tired_face: OMG, I was tried to figure this issue out for a whole WEEK. Silly me.

thanks to you I found out about it after an hour of search. Bless you!

Actually, the solution is much simpler: they just have to be in at least one shared overlay network.
If both services are in the same stack, they share the stack’s default overlay network - that is if no other network is defined and attached to the services.