Docker Community Forums

Share and learn in the Docker community.

Why my Node.js app can not connect to remote Postgres

Hi ,
I am looking for a help to understand what I’m doing wrong that my Node.js app can not connect to a remote ( non containerized ) POSTGRES DATABASE

My Environment

Database Host : 192.168.0.189
Application Host : 192.168.0.150

My Connection to database is coded like this :slight_smile:

const { Pool } = require("pg");
var parse = require('pg-connection-string').parse 
var HOST = ''
var DATABASE_URL = ''

const { DB_NAME, DB_USER, DB_PORT, DB_PASS, NODE_ENV } = process.env

console.log(DB_USER)
console.log(NODE_ENV)

if (NODE_ENV == 'development') {
  HOST = 'macmini2012.local'
  console.log(HOST)
  pool = new Pool({
    user: DB_USER,
    password: DB_PASS,
    host: HOST,
    database: DB_NAME,
    port: DB_PORT
  })

When I run my application outside a Container … I can connect to Database and everything works fine .

But I would like to have this app inside a container … defined with this docker-compose.yml

drestoque:     
     build: 
       context: .
       dockerfile: Dockerfile.dev    
     ports:
       - 3009:3009  
     env_file:
       - config/env/.env  
     volumes:
       - .:/app

Dockerfile.dev is like this

ARG NODE_ENV
FROM node:14.17-slim



WORKDIR /app

COPY package.json /app/package.json


RUN npm install 
COPY . /app 
EXPOSE 3009

CMD ["npm", "run", "dev"]

When I try to run the app inside the Container … I can not connect to Progres Database

I can connect from inside a test container running the following

Dockerfile

FROM ubuntu
RUN apt-get update
RUN apt-get install -y postgresql-client
ENV PGPASSWORD myPassword
CMD psql --host=<database host ip> --port=5432 --username=postgres -c "SELECT * from drestoque.organization ;"

docker build -t testconn .

docker run --rm testconn:latest

id |     name      |  address   | taxid
----+---------------+------------+-------
  1 | Organizacao 1 | Endereco 1 |   191
  2 | Organizacao 2 | Endereco 2 |   191
  3 | Organizacao 3 | Endereco 3 |   191
(3 rows)

Any idea what I am missing ?

I change a Dockerfile for tests letting like this :

FROM ubuntu
RUN apt-get update
RUN apt-get install -y postgresql-client
RUN apt-get install -y iputils-ping
ENV PGPASSWORD tiger
CMD bash

then I run to interact from inside the container

docker run -it testconn:latest /bin/bash  ✔  5s   17:27:23 
root@d63735b2510c:/# hostname
d63735b2510c
root@d63735b2510c:/# psql --host=macmini2012.local --port=5432 --username=drestoque -d postgres -c "SELECT * from drestoque.organization ;"
id | name | address | taxid
----+---------------+------------+-------
1 | Organizacao 1 | Endereco 1 | 191
2 | Organizacao 2 | Endereco 2 | 191
3 | Organizacao 3 | Endereco 3 | 191
(3 rows)
root@d63735b2510c:/# ping 192.168.0.189
PING 192.168.0.189 (192.168.0.189) 56(84) bytes of data.
64 bytes from 192.168.0.189: icmp_seq=1 ttl=37 time=0.865 ms
64 bytes from 192.168.0.189: icmp_seq=2 ttl=37 time=1.27 ms
64 bytes from 192.168.0.189: icmp_seq=3 ttl=37 time=1.22 ms
64 bytes from 192.168.0.189: icmp_seq=4 ttl=37 time=1.50 ms
^C
--- 192.168.0.189 ping statistics ---
`4 packets transmitted, 4 received, 0% packet loss, time 3025ms
`rtt min/avg/max/mdev = 0.865/1.212/1.497/0.226 ms
root@d63735b2510c:/# ping macmini2012
PING MacMini2012 (192.168.0.189) 56(84) bytes of data.
64 bytes from 192.168.0.189 (192.168.0.189): icmp_seq=1 ttl=37 time=1.15 ms
64 bytes from 192.168.0.189 (192.168.0.189): icmp_seq=2 ttl=37 time=1.29 ms
64 bytes from 192.168.0.189 (192.168.0.189): icmp_seq=3 ttl=37 time=0.875 ms
^C
--- MacMini2012 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2005ms
rtt min/avg/max/mdev = 0.875/1.105/1.294/0.173 ms

root@d63735b2510c:/# hostname
d63735b2510c
root@d63735b2510c:/#

My Gosh … I will look for other connection packages

Please, pay more attention to code formatting in your posts. I had to edit almost every line of your second post to make the code blocks right since every line was a formatted code itself inside a code block and your first post contained formatted code blocks inside code blocks making the rendered output unreadable. Check your messages after posting them and edit the posts to fix errors. I almost always find errors in my posts so many of my posts are marked as “edited”.

Do you have any container logs from your nodejs container? Any exception or error message?
I would try to use the IP address instead of hostname to connect to the remote DB in your LAN network. This is the only relevant difference I can see between your working and nonworking connections.

1 Like

Your friend is
docker exec -it container bash
Use that. I think you will see that the problem is in the name resolution of macmini.local. there are ways to expose an internal name like that to the container environment. You should not rely on the name resolution on the host but on the internal DNS server inside the docker environment.