7

I have a docker swarm configuration and in there I deployed two stacks (main and mon) I can expose a port in one stack and I can refer to it from another. However, I was trying to find a way of not doing that but instead have it access it using the 127.0.0.11 DNS.

In mon I have a service called grafana

Using https://docs.docker.com/docker-cloud/apps/service-links/#discovering-services-or-containers-on-another-stack which indicated that I can use the stack name to refer to it didn't work when I tried to ping grafana.mon it just returned invalid host name.

7

To do this, all you need is to make it use the same network, then they will be visible. In my case I defined a network called public, that is referred to externally by all my stacks

docker network create -d overlay --attachable public

From there in my docker-compose.yml file I have

networks:
  public:
    external:
      name: public

To access it, just use the service name.

5
  • 1
    Thanks for your answer, but could you detail a little bit your explanation ? Should the service be reached on <SERVICE>.<STACK> as proposed in your question or only service name is needed ? I'm struggling to make a traefik service talk to a consul service in other stack and I do not want to use published ports – gcharbon Dec 21 '18 at 10:24
  • 1
    not sure about <service>.<stack> I just keep the service names unique per network – Archimedes Trajano Dec 21 '18 at 19:18
  • 1
    Why do you the public name mentioned twice in your docker-compose.yml file ? – Stephane Jan 25 '19 at 10:28
  • Older version of Docker compose used that notation to specify the name. The newer docker uses a slightly different style. – Archimedes Trajano Jan 25 '19 at 10:32
  • More information on how to use an external network https://docs.docker.com/compose/networking/#use-a-pre-existing-network – Stephane Jan 28 '19 at 12:33
1

You need to enable both stacks to connect through an overlay network type, and then allow both stacks to use (at least on the service required) the overlay network that was created externally to both stacks.

The overlay network should be created before the stacks go up, so the services that needs to connect through can 'attach' to it.

Create the network like this

docker network create --driver overlay --attachable 

Then reference the service name on your env file, you can check what name does your services has calling

docker service ls

ID              NAME                     MODE          REPLICAS      IMAGE                  PORTS
vkz5vccbmce7    foo-stack_por-service    replicated    1/1           por-service:1.0.0      *:33065->3306/tcp
sjpnrkm1iiha    foo-stack_vic-service    replicated    1/1           vic-service:1.0.0      *:8081->80/tcp
0capavl31oab    bar-stack_tar-service    replicated    1/1           tar-service:1.0.0      *:33066->3306/tcp
9vohh24jt6hy    bar-stack_zen-service    replicated    1/1           zen-service:1.0.0      *:8082->80/tcp

Now get the name the service from the NAME column. Ex: bar-stack_tar-service

Notice your services are prefixed by the stack name you give when deploy

docker stack ls

NAME                SERVICES            ORCHESTRATOR
foo-stack           2                   Swarm
bar-stack           2                   Swarm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.