I've the following problem:
I created a simple NodeJS app, where there is a listening server on port number 3000
. I definded some simple operations, like post, put and read. Anyway, this operation are performed into a database executed as Docker Container, on port number 5432 (postgres). In local, the application works perfectly. Instead, if I try to Dockerize my app and execute it:
FROM node:16
WORKDIR /app
COPY package*.json /app
RUN npm install
COPY . /app
CMD ["npm", "start"]
I receive the following error:
original: Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1157:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5432
}
}
As if it was not possible to connect to the postgres db. Why this happens? In local I perfectly can connect to postgres, but if I execute my app as a Docker Container, I can't connect to postgres. Thank you everybody!