<p>First of all, thanks for the drawing so I understand what you are trying.</p>
<p>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.</p>
<p>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.</p>
<p>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.</p>
<p>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.</p>
<p>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</p>
<p>So the following up on my description here is you need the following.</p>
<ul>
<li>HTTP / HTTPS Proxy to route traffic into a custom port eg. 9000 for your Docker Swarm Managers</li>
<li>Expose a port eg. 9000 to your service port eg. 80</li>
<li>Setup connection inside the service to use the external Redis and SQL</li>
</ul>
<p>Thats it, hope i have explain it good enough else you can just ask :)</p>
<p><strong>Expose sample - Docker</strong></p>
<p><code>docker run -p 9000:6379 –name redis_cont -d redis</code></p>
<p>allow port 9000 on your host to route into the redis_cont on port 6379</p>
<p><strong>Expose sample - docker-compose.yml</strong></p>
<pre class="lang-yml prettyprint-override"><code>version: &quot;3.7&quot;
services:

 redis:
    image: &quot;redis:alpine&quot;
    ports:
     - &quot;9000:6379&quot;
</code></pre>
<p>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</p>

CC BY-SA 4.0
2021-09-09T19:33:24.527Z
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
CC BY-SA 4.0
2021-09-09T20:15:34.353Z
I have updated with 2 samples one for docker and secound for docker-compose.yml, hopes what you need.
ParisNakitaKejser
CC BY-SA 4.0
2021-09-10T00:02:05.833Z
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
CC BY-SA 4.0
2021-10-04T23:17:11.103Z
I am not using the normal docker setup. I am using `docker stack deploy`
Soerendip
CC BY-SA 4.0
2021-09-04T22:50:01.690Z
2021-09-09T20:15:07.007Z
2021-09-09T20:15:07.007Z
ParisNakitaKejser
<p>I am struggling to set this up correctly. I am trying to run an app with <code>docker stack deploy</code> on a remote machine. The following app works, when I deploy it with <code>docker swarm deploy</code> 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 <code>curl localhost:8000</code>, however, not at the remote machine. There I get <code>curl: (7) Failed to connect to localhost port 8000: Connection refused</code>. 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.</p>
<p>I tried pruning all the networks and start again from scratch.</p>
<p>First, I connect to a remote server via ssh:</p>
<pre><code>ssh -qy -L 8000:localhost:8000
</code></pre>
<p>Here, I start a web application with <code>docker stack</code>, with this simple <code>yaml</code> file <code>testapp.yml</code>:</p>
<pre class="lang-yaml prettyprint-override"><code>    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:
           - &quot;8000:80&quot;
         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: {}
</code></pre>
<p>And deploy this with</p>
<pre><code>docker swarm deploy -c testapp.yml testapp
</code></pre>
<p>Now, I get the following output with <code>sudo docker stack services testapp</code>:</p>
<pre><code>ID             NAME                MODE         REPLICAS   IMAGE              PORTS
93rj1ialspj2   testapp_db          replicated   1/1        mysql:5.7          
nl18a7mr4auk   testapp_wordpress   replicated   1/1        wordpress:latest   *:8000-&gt;80/tcp
</code></pre>
<p>However, when I navigate to <code>localhost:8000</code>there is</p>
<blockquote>
<p>This site can’t be reached. localhost refused to connect.</p>
</blockquote>
<p>And <code>curl localhost:8000</code> (on the both systems local and remote) returns</p>
<blockquote>
<p>curl: (7) Failed to connect to localhost port 8000: Connection refused</p>
</blockquote>
<p>Running docker 20.10.9 remotely and 20.10.8 locally.</p>

CC BY-SA 4.0
2021-09-09T20:22:19.417Z
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
CC BY-SA 4.0
2021-09-12T17:50:56.493Z
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
CC BY-SA 4.0
2021-09-15T22:48:31.477Z
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
CC BY-SA 4.0
2021-09-30T17:30:30.883Z
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
CC BY-SA 4.0
2021-10-06T13:35:51.137Z
Does `curl -4 localhost:8000` work?
BMitch
CC BY-SA 4.0
2021-10-06T18:10:11.340Z
No, that gives me the same message. Failed to connect.
Soerendip
CC BY-SA 4.0
2021-10-06T19:01:21.263Z
And you're running that on the same node where the wordpress container is running?
BMitch
<docker><docker-compose>
How to create network connection into remote docker stack?
Soerendip
# How to create network connection into remote docker stack?

(Asked by: username_0 on 2021-09-03)

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.

**Tags:** docker, docker-compose

> **Comments:**
> **username_1**: 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?  
> **username_0**: 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?  
> **username_1**: 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.  
> **username_0**: Maybe, I am just trying the wrong IP address. I am trying localhost:8000 on the node that, I know, runs the web frontend.  
> **username_1**: Does `curl -4 localhost:8000` work?  
> **username_0**: No, that gives me the same message. Failed to connect.  
> **username_1**: And you're running that on the same node where the wordpress container is running?  

---

## 1 answers

### Answer by username_2 on 2021-09-04. Score: 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

> **Comments:**
> **username_0**: 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.  
> **username_2**: I have updated with 2 samples one for docker and secound for docker-compose.yml, hopes what you need.  
> **username_0**: 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.  
> **username_0**: I am not using the normal docker setup. I am using `docker stack deploy`  

---

