后端容器无法连接到数据库-Sequelize,Node,Postgres,Docker

时间:2019-12-17 16:19:51

标签: node.js postgresql docker docker-compose sequelize.js

当我尝试使用Sequelize连接后端时,出现以下错误:

# This is the first dataframe  
make <- c("PORSCHE", "TESLA", "MITSUBISHI")
model <- c("Cayenne", "Model S", "Outlander - PHEV")
year <- c(2017, 2013, 2018)
electrification_level <- (PHEV, BEV, PHEV)
vin_data <- data.frame(make, model, year, electrification_level)

# This is the second dataframe    
make <- c("Porsche", "Tesla", "Mitsubishi")
# There are multiple versions of the models (an average of these would be ideal - e.g. avg. mpg)
model <- c("Cayenne S e-Hybrid", "Model S AWD - P85D", "Outlander 2WD")
year <- c(2017, 2013, 2018)
# These mpg are made up for the example
mpg <- c(75, 120, 80)
fueleconomy_data <- data.frame(make, model, year, mpg) 

docker-compose.yml

error ConnectionRefusedError [SequelizeConnectionRefusedError]: connect ECONNREFUSED 127.0.0.1:5432

我做错了什么?

预先感谢

1 个答案:

答案 0 :(得分:1)

假设您的后端正在连接到数据库,则应添加一个depends_on

  backend:
    build:
      context: ./server
      dockerfile: Dockerfile
    image: server
    depends_on:
      - db
    ports:
      - "8000:8000"
    volumes:
      - ./server:/usr/src/app

如果您的应用程序配置为连接到db:5432localhost:5432,则现在可以在主机172.0.0.1:5432上访问数据库,您需要将主机名localhost替换为db。您的postgres连接字符串也可能没有主机,并且可能默认情况下尝试连接到localhost。应该能够看一下续集找出如何通过主机。