3

I am struggling to set this up correctly. I am trying to run an app with docker stack deploy on a remote machine. The following app works, when I deploy it with docker swarm deploy on my windows notebook. But when I run it on the linux cluster. I cannot connect to it. If can help me to identify the missing piece here? When I run the app locally, I get some output from curl localhost:8000, however, not at the remote machine. There I get curl: (7) Failed to connect to localhost port 8000: Connection refused. I also do not understand why I get a different result (app accessible, not accessible) on local and remote computer, i.e. what configuration is the disrupting element.

I tried pruning all the networks and start again from scratch.

First, I connect to a remote server via ssh:

ssh -qy -L 8000:localhost:8000

Here, I start a web application with docker stack, with this simple yaml file testapp.yml:

    version: '3'
    services:
       db:
         image: mysql:5.7
         volumes:
           - db_data:/var/lib/mysql
         restart: always
         environment:
           MYSQL_ROOT_PASSWORD: somewordpress
           MYSQL_DATABASE: wordpress
           MYSQL_USER: wordpress
           MYSQL_PASSWORD: wordpress
    
       wordpress:
         depends_on:
           - db
         image: wordpress:latest
         volumes:
           - web_data:/var/www/html
         ports:
           - "8000:80"
         restart: always
         environment:
           WORDPRESS_DB_HOST: db:3306
           WORDPRESS_DB_USER: wordpress
           WORDPRESS_DB_PASSWORD: wordpress
           WORDPRESS_DB_NAME: wordpress
    
    volumes:
        db_data: {}
        web_data: {}

And deploy this with

docker swarm deploy -c testapp.yml testapp

Now, I get the following output with sudo docker stack services testapp:

ID             NAME                MODE         REPLICAS   IMAGE              PORTS
93rj1ialspj2   testapp_db          replicated   1/1        mysql:5.7          
nl18a7mr4auk   testapp_wordpress   replicated   1/1        wordpress:latest   *:8000->80/tcp

However, when I navigate to localhost:8000there is

This site can’t be reached. localhost refused to connect.

And curl localhost:8000 (on the both systems local and remote) returns

curl: (7) Failed to connect to localhost port 8000: Connection refused

Running docker 20.10.9 remotely and 20.10.8 locally.

7
  • What makes you say the website is not accessible? Did you publish the port? What does the compose file look like and what errors do you see when attempting to connect? Does the error change if you try to connect from the node where the web server is deployed?
    – BMitch
    Sep 9, 2021 at 20:22
  • When I start the stack and navigate to localhost:8000 there is nothing. I added the compose file for a development server. Maybe it is accessible via a different IP address?
    – Soerendip
    Sep 12, 2021 at 17:50
  • A blank page indicates you are likely talking to the application and the application itself is having issues. If there's an error page, which error you see is an important detail.
    – BMitch
    Sep 15, 2021 at 22:48
  • Maybe, I am just trying the wrong IP address. I am trying localhost:8000 on the node that, I know, runs the web frontend.
    – Soerendip
    Sep 30, 2021 at 17:30
  • Does curl -4 localhost:8000 work?
    – BMitch
    Oct 6, 2021 at 13:35

1 Answer 1

2

First of all, thanks for the drawing so I understand what you are trying.

The next step is, when you are working with Docker Swarm all your stacks/services can hit the external networks like Redis, SQL and S3 storage so that is nothing you need to take care of.

When you want traffic inside your cluster you need a proxy in the front, I personally are using NGINX as proxy layer and then you need to create a upstream with a stack of IP's to your Docker Swarm Manager nodes, never sending it to your workers.

Docker Swarm working the way you are sending traffic to a single port number eg. 9000 and this port 9000 is binding to your stack/services on port 80 or what you are needed. Every single replicate will now running on port 9000 for externel traffic from the managers and into your cluster.

Besure you are deploying your stacks only for worker nodes except if its controller software you needed, else you can get a unstable cluster based on my own expires.

If your Redis and SQL running in the cluster (i hope not) but if you want it, you can setup a network for thoes stacks and the add the network internel into your orther stacks, but i will recommend to keep Redis, SQL and somthing like that on the own servers

So the following up on my description here is you need the following.

  • HTTP / HTTPS Proxy to route traffic into a custom port eg. 9000 for your Docker Swarm Managers
  • Expose a port eg. 9000 to your service port eg. 80
  • Setup connection inside the service to use the external Redis and SQL

Thats it, hope i have explain it good enough else you can just ask :)

Expose sample - Docker

docker run -p 9000:6379 –name redis_cont -d redis

allow port 9000 on your host to route into the redis_cont on port 6379

Expose sample - docker-compose.yml

version: "3.7"
services:

 redis:
    image: "redis:alpine"
    ports:
     - "9000:6379"

Deploy a redis stack into your Docker Swarm and allow port 9000 on your host(Docker Swarm Cluster) to route into the Redis stack service on port 6379

4
  • Thank you a lot I really appreciate your effort. I like to have Redis and SQL inside the stack. My primary objective is simplicity. And these things can be changed anytime for production settings. The answer gives some overview about what needs to be done, though it does not offer specific code examples for the docker-compose file. Or links to corresponding examples. And the essential part of the question is rephrased as Expose a port eg. 9000 to your service port eg. 80, The core of the question is how to do exactly that. If you want to elaborate on this, would be great.
    – Soerendip
    Sep 9, 2021 at 19:33
  • I have updated with 2 samples one for docker and secound for docker-compose.yml, hopes what you need. Sep 9, 2021 at 20:15
  • Are you sure that works for docker stack deploy? I know this works when I start docker compose on one machine. But when that is started with docker stack deploy I first need a connection into the sub-network. All the services see each other, that is not the question. The question is how to create this overlay or bridge network. The services can reach the outside, but the outside cannot reach the web server.
    – Soerendip
    Sep 10, 2021 at 0:02
  • I am not using the normal docker setup. I am using docker stack deploy
    – Soerendip
    Oct 4, 2021 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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