Deploy app on a cluster but cannot access it successfully

Viewed 1334

I'm now learning to use docker follow get-started documents, but in part 4--Swarms I've met some problem. That is when deployed my app on a cluster, I cannot access it successfully.

docker@myvm1:~$ docker stack ps getstartedlab
ID                  NAME                  IMAGE                     NODE                DESIRED STATE       CURRENT STATE              ERROR               PORTS
gsueb9ejeur5        getstartedlab_web.1   zhugw/get-started:first   myvm1               Running             Preparing 11 seconds ago
ku13wfrjp9wt        getstartedlab_web.2   zhugw/get-started:first   myvm2               Running             Preparing 11 seconds ago
vzof1ybvavj3        getstartedlab_web.3   zhugw/get-started:first   myvm1               Running             Preparing 11 seconds ago
lkr6rqtqbe6n        getstartedlab_web.4   zhugw/get-started:first   myvm2               Running             Preparing 11 seconds ago
cpg91o8lmslo        getstartedlab_web.5   zhugw/get-started:first   myvm2               Running             Preparing 11 seconds ago

docker@myvm1:~$ curl 'http://localhost'
curl: (7) Failed to connect to localhost port 80: Connection refused

āžœ  ~ docker-machine ls
NAME    ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER        ERRORS
myvm1   -        virtualbox   Running   tcp://192.168.99.101:2376           v17.06.0-ce
myvm2   -        virtualbox   Running   tcp://192.168.99.100:2376           v17.06.0-ce

āžœ  ~ curl 'http://192.168.99.101'
curl: (7) Failed to connect to 192.168.99.101 port 80: Connection refused

What's wrong?

In addition, very strange. After adding below content in docker-compose.yml I found above question resolved automatically

visualizer:
    image: dockersamples/visualizer:stable
    ports:
      - "8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
    deploy:
      placement:
        constraints: [node.role == manager]
    networks:
      - webnet

but this time the new added visualizer does not work

docker@myvm1:~$ docker stack ps getstartedlab
ID                  NAME                         IMAGE                             NODE                DESIRED STATE       CURRENT STATE             ERROR               PORTS
xomsv2l5nc8x        getstartedlab_web.1          zhugw/get-started:first           myvm1               Running             Running 7 minutes ago
ncp0rljod4rc        getstartedlab_visualizer.1   dockersamples/visualizer:stable   myvm1               Running             Preparing 7 minutes ago
hxddan48i1dt        getstartedlab_web.2          zhugw/get-started:first           myvm2               Running             Running 7 minutes ago
dzsianc8h7oz        getstartedlab_web.3          zhugw/get-started:first           myvm1               Running             Running 7 minutes ago
zpb6dc79anlz        getstartedlab_web.4          zhugw/get-started:first           myvm2               Running             Running 7 minutes ago
pg96ix9hbbfs        getstartedlab_web.5          zhugw/get-started:first           myvm2               Running             Running 7 minutes ago

from above you know it's always preparing.


My whole docker-compose.yml

version: "3"
services:
  web:
    # replace username/repo:tag with your name and image details
    image: zhugw/get-started:first
    deploy:
      replicas: 5
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      - "80:80"
    networks:
      - webnet
  visualizer:
    image: dockersamples/visualizer:stable
    ports:
      - "8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
    deploy:
      placement:
        constraints: [node.role == manager]
    networks:
      - webnet
networks:
  webnet:
6 Answers

Had this problem while learning too.

It's because your none clustered image is still running from step 2 and the clustered image you just deployed uses the same port mapping (4000:80) in the docker-compose.yml file.

You have two options:

  1. Go into your docker-compose.yml and change the port mapping to something else e.g 4010:80 and then redeploy your cluster with the update. Then try: http://localhost:4010

  2. Remove the container you created in step 2 of the guide that's still running and using port mapping 4000:80

Open port 7946 TCP/UDP and port 4789 UDP between the swarm nodes. Use the ingress network. Please let me know if it works, thanks.

If you are using Docker toolbox for mac, then you should check this out.

I had the same problem. As it says in the tutorial (see "Having connectivity trouble?") the following ports need to be open:

  • Port 7946 TCP/UDP for container network discovery.
  • Port 4789 UDP for the container ingress network.

So I executed the following before the swarm init (right after creation of myvm1 and myvm2) and then later could access the service e.g. in the browser with IP_node:4000

$ docker-machine ssh myvm1 "sudo iptables -I INPUT -p tcp --dport 7946 --syn -j ACCEPT"
$ docker-machine ssh myvm2 "sudo iptables -I INPUT -p tcp --dport 7946 --syn -j ACCEPT"
$ docker-machine ssh myvm1 "sudo iptables -I INPUT -p udp --dport 7946 -j ACCEPT"
$ docker-machine ssh myvm2 "sudo iptables -I INPUT -p udp --dport 7946 -j ACCEPT"
$ docker-machine ssh myvm1 "sudo iptables -I INPUT -p udp --dport 4789 -j ACCEPT"
$ docker-machine ssh myvm2 "sudo iptables -I INPUT -p udp --dport 4789 -j ACCEPT"

Hope it helps others.

Related