I have a linux postgres database running on my localhost at port 5432. The IP of my machines network card is 192.168.68.80. My configuration files for postgres look as so:
/etc/postgresql/13/main/postgresql.conf
listen_addresses = '*'
/etc/postgresql/13/main/pg_hba.conf
host all all 0.0.0.0/0 md5
host all all ::/0 md5
Now I want to connect to this database from a Node application running inside a docker container. This is what my environment variables file looks like when loading into Node:
.env
PGHOST=localhost // Host of the postgres db server
PGPORT=5432 // Port of the postgres db
After this setup, I build my docker image using my docker file. I then try to run the dockerfile as a container with the following command on linux:
sudo docker run --network="host" --env-file .env -p 3000:3000 repo:tag
and the app begins to run, and listens for connections on port 3000. But whenever I try to connect to the app, the app fails to connect to the postgres database server and gives the following error:
/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:172
reject(new sequelizeErrors.ConnectionRefusedError(err));
^
ConnectionRefusedError [SequelizeConnectionRefusedError]: connect ECONNREFUSED 127.0.0.1:5432
at Client._connectionCallback (/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:172:24)
at Client._handleErrorWhileConnecting (/node_modules/pg/lib/client.js:305:19)
at Client._handleErrorEvent (/node_modules/pg/lib/client.js:315:19)
at Connection.emit (node:events:394:28)
at Socket.reportStreamError (/node_modules/pg/lib/connection.js:52:12)
at Socket.emit (node:events:394:28)
at emitErrorNT (node:internal/streams/destroy:193:8)
at emitErrorCloseNT (node:internal/streams/destroy:158:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
parent: Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1133:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5432
},
original: Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1133:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5432
}
}
I can not figure out why the connection is being refused as it seems like it should be able to connect.
EDIT:
My current ip tables in case that helps:
user$ sudo iptables --list
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy DROP)
target prot opt source destination
DOCKER-USER all -- anywhere anywhere
DOCKER-ISOLATION-STAGE-1 all -- anywhere anywhere
ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
DOCKER all -- anywhere anywhere
ACCEPT all -- anywhere anywhere
ACCEPT all -- anywhere anywhere
ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
DOCKER all -- anywhere anywhere
ACCEPT all -- anywhere anywhere
ACCEPT all -- anywhere anywhere
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain DOCKER (2 references)
target prot opt source destination
Chain DOCKER-ISOLATION-STAGE-1 (1 references)
target prot opt source destination
DOCKER-ISOLATION-STAGE-2 all -- anywhere anywhere
DOCKER-ISOLATION-STAGE-2 all -- anywhere anywhere
RETURN all -- anywhere anywhere
Chain DOCKER-ISOLATION-STAGE-2 (2 references)
target prot opt source destination
DROP all -- anywhere anywhere
DROP all -- anywhere anywhere
RETURN all -- anywhere anywhere
Chain DOCKER-USER (1 references)
target prot opt source destination
RETURN all -- anywhere anywhere