'Node.js x React x Auth0 - getting error 500 with docker-compose deployment

I'm creating a WebApp that has a React front end whose build is served by a node.js back end. There is some security implementation using Auth0 token validation using JWT & JWKS for secured API access (which I'm suspecting to be the cause of this) Everything worked fine on my local machine (Windows 10.0.19043 pro x86) using node to do some local testing. Everything worked perfectly fine when I built the Docker image using this Dockerfile:

FROM node:16

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

COPY client ./client

RUN npm run build
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 8080
CMD [ "npm", "run", "start" ]

My node back end has a connexion to some redis instance, which is working fine, no connection problems here.

So, the main problem is that when I'm deploying my stack using docker-compose, my back end throws 500s for secured API requests. Error example:

Error: connect ECONNREFUSED 127.0.0.1:80
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1157:16)

What I'm struggling to understand is that I'm not calling 127.0.0.1:80 (or any of its equivalents) anywhere in the back or front end! The app is running and is exposed on port 8080, and even the front end's requests are properly formed

GET http://localhost:8080/api/secure/v1/test

and Authorization is a valid Auth0 token. I have been searching everywhere to get a solution for this but everything so far was talking about some external connection problems (i.e. mongodb, mysql or redis instances are not atteignable and this error is thrown when trying to connect to those), which isn't my case.

My back end code used to check Auth0's token is the following (I'm using express and I followed the documentation's official guide).

var jwtCheck = jwt({ // Creating authentication validator as described by Auth0's Doc.
    secret: jwks.expressJwtSecret({
        cache: true,
        rateLimit: true,
        jwksRequestsPerMinute: 1000,
        jwksUri: process.env.JWKSURI
  }),
  audience: process.env.AUDIENCE,
  issuer: process.env.ISSUER,
  algorithms: [process.env.ALGORITHMS]
});
app.route("/api/secure/v1/test")
.get(jwtCheck, (req,res) =>{
    res.status(200).send({"status":"working lol"})
})

When trying to access http://localhost:8080/api/secure/v1/test I get the error mentioned above but only with docker-compose deployment. docker run works fine but I need to use docker-compose for convenience and security reasons.

example of docker-compose file:

version: "3.9"
services:
  web:
    restart: unless-stopped
    image: some.private.container.registry/myWebImage
    ports:
      - "8080:8080"
    expose:
      - 8080
    environment:
      - PORT=8080
      - DOMAIN="as described by Auth0 documentation"
      - CLIENTID="as described by Auth0 documentation"
      - JWKSURI="as described by Auth0 documentation"
      - AUDIENCE="as described by Auth0 documentation"
      - ISSUER="as described by Auth0 documentation"
      - ALGORITHMS="as described by Auth0 documentation"
      - REDISHOST=someIp
      - REDISPORT=6379
    networks:
      - default

Logs of the web container:

> [email protected] start
> node server.js

API and front are running on http://localhost:8080/
refreshed redis 2022-03-28T06:57:50.906Z 2022-03-28T06:48:27.044Z 2022-03-26T21:45:00.000Z 2022-03-27T00:45:00.000Z
Error: connect ECONNREFUSED 127.0.0.1:80
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1157:16)
Error: connect ECONNREFUSED 127.0.0.1:80
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1157:16)
refreshed redis 2022-03-28T06:58:50.870Z 2022-03-28T06:48:27.044Z 2022-03-26T21:45:00.000Z 2022-03-27T00:45:00.000Z

"refreshed redis" lines are here to log every time the webapp requests data from redis with some meaningful timestamps in the use case of my app.
The "error" appears only when a request is made to a secure endpoint.

Environment informations:
Env 1:

kernel: 5.4.0-99-generic
OS: Ubuntu 20.04.3 LTS x86_64
docker version: Docker version 20.10.12, build e91ed5707e
docker-compose version: docker-compose version 1.29.2, build unknown

Env 2:

kernel: 5.15.25-1-MANJARO
OS: Manjaro Linux x86_64
docker version: Docker version 20.10.12, build e91ed5707e
docker-compose version: Docker Compose version 2.2.3

The symptoms are the same on those 2 environments.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source