Bogdan Boamfa
Bogdan Boamfa

Reputation: 405

docker stack communicate between containers

I'm trying to setup a swarm using docker but I'm having issues with communicating between containers. I have cluster with 5 nodes. 1 manager and 4 workers.

3 apps: redis, splash, myapp

myapp has to be on the 4 workers

redis, splash just on the manager

myapp has to be able to communicate with redis and splash

I tried using the container name but its not working. It resolves the container name to different IPs.

ping splash # return a different ip than the container actually has

I am deploying running the swarm using docker stack

docker stack deploy -c docker-stack.yml myapp

Linking container between them also doesn't work. Any ideas ? Am I missing something ?

root@swarm-manager:~# docker version
Client:
 Version:      17.09.0-ce
 API version:  1.32
 Go version:   go1.8.3
 Git commit:   afdb6d4
 Built:        Tue Sep 26 22:42:18 2017
 OS/Arch:      linux/amd64

Server:
 Version:      17.09.0-ce
 API version:  1.32 (minimum version 1.12)
 Go version:   go1.8.3
 Git commit:   afdb6d4
 Built:        Tue Sep 26 22:40:56 2017
 OS/Arch:      linux/amd64
 Experimental: false

docker-stack.yml contains:

version: "3"
services:
    splash:
        container_name: splash
        image: scrapinghub/splash
        ports:
            - 8050:8050
            - 5023:5023
        deploy:
            mode: global
            placement:
                constraints:
                    - node.role == manager

    redis:
        container_name: redis
        image: redis
        ports:
            - 6379:6379
        deploy:
            mode: global
            placement:
                constraints:
                    - node.role == manager

    myapp:
        container_name: myapp
        image: myapp_image:latest
        environment:
            REDIS_ENDPOINT: redis:6379
            SPLASH_ENDPOINT: splash:8050
        deploy:
            mode: global
            placement:
                constraints:
                    - node.role == worker
        entrypoint:
            - ping google.com

---- EDIT ----

I tried with curl also. Didn't work. docker stack deploy -c docker-stack.yml myapp

Creating network myapp_default
Creating service myapp_splash
Creating service myapp_redis
Creating service myapp_myapp

curl http://myapp_splash:8050

curl: (7) Failed to connect to myapp_splash port 8050: No route to host

curl http://splash:8050

curl: (7) Failed to connect to splash port 8050: No route to host

What worked is getting the actual container name of splash, which is some random generated string.

curl http://myapp_splash.d7bn0dpei9ijpba4q41vpl4zz.tuk1cimht99at9g0au8vj9lkz:8050

But this doesn't really help me.

Upvotes: 2

Views: 6106

Answers (3)

Fyodor Yemelyanenko
Fyodor Yemelyanenko

Reputation: 11848

It seems that containers in stack named tasks.<service name> so the command ping tasks.myservice works for me!

Itersting point to note that names like <stackname>_<service name> will also resolve and ping'able but IP address is incorrect. This is frustarating.

(For exmple if you do docker stack deploy -c my.yml AA you'll get name like AA_myservice which will resolve to incorrect addreses)

To add to above answer. From network point of view curl and ping do the same things. Both will try to resolve name passed to them and then curl will try to connect using specified protocol (http is the example above) and ping will send ICMP echo requests.

Upvotes: 0

Bogdan Boamfa
Bogdan Boamfa

Reputation: 405

I manage to get it working using curl http://tasks.splash:8050 or http://tasks.myapp_splash:8050.

I don't know whats is causing this issue though. Feel free to comment with an answer.

Upvotes: 2

herm
herm

Reputation: 16305

Ping is not the proper tool to try and connect services. For some reason it doesn't work with docker networks. Try curl http://serviceName instead.

Other than that: Containers can't be named when using stack deploy, instead your service name is used (which coincidentally is the same) to access another service.

Upvotes: 4

Related Questions