1

I am trying to connect postgresdb service with nodejs web service using docker compose

My docker-compose.yml file

version: "3"                                                                                                                                                                                                                                                                                                                  
services:                                                                                                                                                                                                                                                                                                                     
  web:                                                                                                                                                                                                                                                                                                                        
      build: ./                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
      ports:                                                                                                                                                                                                                                                                                                                  
       - "40000:3000"                                                                                                                                                                                                                                                                                                        
      depends_on:                                                                                                                                                                                                                                                                                                                  
        - postgres
  postgres:                                                                                                                                                                                                                                                                                                                   
      image: kartoza/postgis:9.6-2.4                                                                                                                                                                                                                                                                                          
      restart: always                                                                                                                                                                                                                                                                                                         
      volumes:                                                                                                                                                                                                                                                                                                                
        - postgresdata:/data/db                                                                                                                                                                                                                                                                                               
      environment:                                                                                                                                                                                                                                                                                                            
        - POSTGRES_PASS=password                                                                                                                                                                                                                                                                                              
        - POSTGRES_DBNAME=sticki                                                                                                                                                                                                                                                                                              
        - POSTGRES_USER=renga                                                                                                                                                                                                                                                                                                 
        - ALLOW_IP_RANGE=0.0.0.0/0                                                                                                                                                                                                                                                                                            
      ports:                                                                                                                                                                                                                                                                                                                  
        - "1000:5432"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
volumes:                                                                                                                                                                                                                                                                                                                      
  postgresdata:                                                                                                                                                                                                                                                                                                               

So when i do docker-compose up in my root directory both services are running and i can access web service using localhost:40000 and postgres service using postico on localhost:1000

But in Node Web service i have written code to access postgres using Sequelize as

const sequelize = new Sequelize('sticki', 'renga', 'password', {
  host: 'postgres',
  dialect: 'postgres',
});

But I get the following error

SequelizeConnectionRefusedError: connect ECONNREFUSED 172.18.0.2:1000

Why does postgres Connection is made to 172.18.0.2 instead of localhost(0.0.0.0)? What i am doing wrong?

2 Answers 2

3

For your web container postgres is a DNS name defined in compose as a service. It fetches the postgres DNS IP address via docker internal DNS & network, that's why it's resolving to 172.18.0.2. If you go to web container & ping postgres, you will get the same IP.

As a fix, configure your node service to connect to host postgres on port 5432 since it's the container port. Port 1000 is the host machine port, if you want to use port 1000, configure node service to connect to your MACHINE_IP:1000.

PS - Localhost within a container means the container itself & nothing else.

Sign up to request clarification or add additional context in comments.

Comments

0

Service name is taken from container_name - which is fixed. In your case you do not have that and name is created from folder where docker-compose.yml is + _ + service name + _1. With this DNS name you can reach your service on the default network that docker-compose will create, from one service to reach the other.

Thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.