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

我无法从运行节点&Sequelize的单独的docker服务连接到默认的Postgres映像。

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

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

错误消息是

 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 

我正在运行一个看起来像这样的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' 

而我从我的networking服务的连接看起来像这样

 const sequelize = new Sequelize('postgresql://guy:password@0.0.0.0/engauge'); 

监听地址是0.0.0.0,它用于侦听所有接口,而不是连接的IP。 在您的连接string中,在您的案例database指定要连接到的主机名的服务名称。