Answer a question

I'm trying to migrate the data but it always cant find my postgreSQL.

version: '3'
services:
  server:
    build: ./server
    ports:
      - 3002:3002
    depends_on:
      - db
    environment:
      CORS_ALLOW_ORIGINS: http://localhost:8080,http://localhost:80
      PORT: 3002
      DEBUG: vuichoi*
      DEBUG_HIDE_DATE: 1
      SECRET_OR_PRIVATE_KEY: secret
      SECRET_OR_PUBLIC_KEY: secret
      DATABASE_URL: postgres://vuichoi:@db:5432/vuichoi #point the server container to the db container's IP address
      command: bash -c "sequelize db:migrate && sequelize db:seed:all"
  db:
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: vuichoi
      POSTGRES_DB: vuichoi
    image: postgres:10

config.json

{
"development": {
    "username": "vuichoi",
    "database": "vuichoi",
    "host": "localhost",
    "dialect": "postgres"
},
"test": {
    "username": "vuichoi",
    "password": "vuichoi",
    "database": "database_test",
    "host": "localhost",
    "dialect": "postgres"
},
"production": {
    "use_env_variable": "DATABASE_URL"
}

}

after docker-compose up --build enter image description here

when im trying to migrate:

enter image description here

Answers

The problem is that you running the server and the database at same time, and when the server starts it tries to connect to the DB that is still being started/created.

A solution for this problem is using a script that only launches the server a few seconds after the db has started. In https://github.com/PedroS11/node-postgres-redis-docker you have an example, the entry-point.sh. Or, if you want, search for "wait for it docker", and you will find a script for this and with more examples.

Logo

PostgreSQL社区为您提供最前沿的新闻资讯和知识内容

更多推荐