Docker swarm: second service connection refused

Hello, I have been experimenting with creating a single node swarms for our dev department. I have successfully fired up my first stack using the docker stack deploy command and pointing to a custom docker-compose.yml file.

I tried to do the same thing with another project. I have deployed a second stack to the swarm, and when I run the command ‘docker stack services ’ everything looks good. My replicas are filled out 1/1 and it shows the correct image and PORTS being listened to.

However when I try to connect to the second stack on the node, I am getting connection refused.

@bretfisher if you have any tips I would be greatful. Been following your udemy guide for docker recently (which is gold). Here is my compose file for the service that is refusing the connection.


version: “3.5”
services:
app_dev:
build:
context: ./
image: “rsidevs/inquiry”
restart: unless-stopped
command: bash -c “node index.js”
ports:
- “4051:4051”

Maybe a firewall in the way? Does that port work from another container on that docker network (test inside the network rather than outside the host IP). The Swarm channel on my Discord server may be able to help more in chat: https://discord.gg/devops

Thanks for the reply @bretfisher. I will definitely investigate these suggestions and check out the discord the channel.

I did a little digging and noticed that when I try to run this application locally using docker-compose build/up, the application loads in the browser with a blank page and response saying, Must enable javascript to run this app.

This is a react front end with an express proxy server. When I exec -it into the running container locally, and try to run node index.js file, I get the following error response in the container output…

node@34169c235169:/server$ node index.js
node:events:505
throw er; // Unhandled ‘error’ event
^

Error: listen EADDRINUSE: address already in use :::4051
at Server.setupListenHandle [as _listen2] (node:net:1380:16)
at listenInCluster (node:net:1428:12)
at Server.listen (node:net:1516:7)
at Function.listen (/server/node_modules/express/lib/application.js:635:24)
at Object. (/server/index.js:18:5)
at Module._compile (node:internal/modules/cjs/loader:1099:14)
at Object.Module._extensions…js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
Emitted ‘error’ event on Server instance at:
at emitErrorNT (node:net:1407:8)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: ‘EADDRINUSE’,
errno: -98,
syscall: ‘listen’,
address: ‘::’,
port: 4051
}

I don’t understand how the port is already in use because I am defining it in my docker-compose.yml file. Here is the docker-compose.yml file…


version: “3.5”

services:
app:
build:
context: ./
image: “inquiry_app”
container_name: inquiry_app
restart: unless-stopped
command: bash -c “node index.js”
ports:
- “4051:4051”

and the Dockerfile the image is built with…


FROM node:17-buster-slim

RUN rm /bin/sh && ln -s /bin/bash /bin/sh

COPY ./server /server

RUN cd /server && yarn install

WORKDIR /server

USER node

Furthermore, what also throws me for a loop is that our current architecture on a single node in production simply used docker-compose to deploy the application and it works fine ( I understand this is not ideal, hence the experimentation with swarm) but when I try to run the same compose file in a stack service it tells me connection refused.

Please use 3 backticks before and after to format you code.

The node application is using a port. It is automatically started when you run the container, so the port inside the container is used upon container startup.

When you go inside the container and manually start your node app again, then it can’t use the port because it is already used.

Thanks for the response @bluepuma77.

I realized your point once I started looking at the running container logs. :grimacing:

Still having a disconnect about why the application will not load in the browser when running locally with docker-compose.

Success!

Figured out the issue, in case anyone is interested. The Dockerfile/Compose file are working as expected, the issue was caused by the index.html file in the server build directory.

The src in the script importing the main.js files began with

./static/js/main...

While the build file path is actually

/build/static/js/main...

By replacing ./static/js/main... in the path with /static/js/main... to create the proper file path to the build files correctly loads the application in the browser when running docker-compose up locally.

Thanks for the tips and advice @bluepuma77 and @bretfisher.

The Docker journey continues!