6

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');
1
  • You can only listen on 0.0.0.0, you cannot connect to it. Have you tried 127.0.0.1? (Or whatever the container's IP address is. ) Commented Mar 11, 2017 at 23:03

1 Answer 1

9

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.

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

2 Comments

Thanks so much! I'll also add that I had to clear the existing volumes and containers after making this change for them to take effect.
I don't see any volumes listed above. With compose, you can just run docker-compose up -d to apply new container configs. It's usually smart enough to see which have changed settings and recreate the necessary containers.

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.