Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't connect to Postgres container with Docker Compose from Sequelize

I can't connect to the default Postgres image from a separate docker service running node & Sequelize.

I'm assuming that my setup of the container is not correct and also perhaps my connection info is incorrect too because I when I run docker-compose --build I cannot connect to my database using the connection info:

Host: 0.0.0.0
Port: 5432
User: guy
Password: password
Database: engauge

The error message is

Unable to connect to the database: { SequelizeConnectionRefusedError: connect ECONNREFUSED 0.0.0.0:5432
at /usr/src/app/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:98:20

I am running with a YML file that looks like this

version: '2' # specify docker-compose version

# Define the services/containers to be run
services:
  web: # name of the first service
    build: . # specify the directory of the Dockerfile
    ports:
      - "3000:3000" # specify port forewarding
    links:
      - database
  database: # name of the third service
    image: postgres # specify image to build container from
    ports:
      - "5432:5432" # specify port forewarding
    environment:
         - POSTGRES_USER:'guy'
         - POSTGRES_PASSWORD:'password'
         - POSTGRES_DB:'engauge'

And my connection from within my web service looks like this

const sequelize = new Sequelize('postgresql://guy:[email protected]/engauge');
like image 928
Guy Thomas Avatar asked Mar 11 '17 23:03

Guy Thomas


1 Answers

The 0.0.0.0 is the listening address, it's used to listen on all interfaces and isn't an IP that you connect to. In your connect string, specify the service name for the hostname to connect to, in your case database.

like image 87
BMitch Avatar answered Sep 24 '22 06:09

BMitch