使用Sequelize的Docker Compose无法连接到Postgres容器

Guy*_*mas 6 postgresql sequelize.js docker docker-compose

我无法从运行节点和Sequelize的单独docker服务连接到默认的Postgres图像.

我假设我的容器设置不正确,也许我的连接信息也不正确,因为我在运行docker-compose --build时无法使用连接信息连接到我的数据库:

Host: 0.0.0.0
Port: 5432
User: guy
Password: password
Database: engauge
Run Code Online (Sandbox Code Playgroud)

错误消息是

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
Run Code Online (Sandbox Code Playgroud)

我正在运行一个看起来像这样的YML文件

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'
Run Code Online (Sandbox Code Playgroud)

我在我的Web服务中的连接看起来像这样

const sequelize = new Sequelize('postgresql://guy:password@0.0.0.0/engauge');
Run Code Online (Sandbox Code Playgroud)

BMi*_*tch 7

0.0.0.0是监听地址,它用于监听所有接口,而不是您连接的IP.在您的连接字符串中,指定要连接的主机名的服务名称(在您的情况下)database.