fjurr
fjurr

Reputation: 552

Error getaddrinfo ENOTFOUND db when running sequelize migrations

I'm using docker-compose and I'm getting the following error when I try to run yarn sequelize db:migrate in my console:

"Loaded configuration file "src/config/database.js". ERROR: getaddrinfo ENOTFOUND db"

Everything works fine, but I can't run my migrations.

Dockerfile

FROM node:alpine

WORKDIR /usr/app
COPY package.json yarn.lock ./

RUN yarn

COPY . .

EXPOSE 3000
CMD ["yarn", "dev"]

Docker-compose.yml

version: '3'

services:
  api:
    container_name: api
    build: .
    ports:
      - '3000:3000'
    command: yarn dev
    volumes:
      - .:/usr/app
    links:
      - db

  db:
    container_name: db
    image: mysql:5.7
    volumes:
      - ./data:/mysql/db
    ports:
      - '3306:3306'
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: be_db
      MYSQL_USER: root
      MYSQL_PASSWORD: password

.env

# Database Variables
DB_HOST=db
DB_USER=root
DB_PASS=password
DB_NAME=be_db
DB_PORT:3306

/src/config/database.js

require('dotenv').config();

module.exports = {
  dialect: 'mysql',
  host: process.env.DB_HOST,
  username: process.env.DB_USER,
  password: process.env.DB_PASS,
  database: process.env.DB_NAME,
  define: {
    timestamps: false,
    underscored: true,
    underscoredAll: true,
  },
};

OBS: If I switch the DB_HOST=db to DB_HOST=localhost I'm able to run my migrations but them, my container pops up this error:

code: 'ECONNREFUSED',
api    |     syscall: 'connect',
api    |     address: '127.0.0.1',
api    |     port: 3306,
api    |     fatal: true

Upvotes: 6

Views: 9868

Answers (1)

7.oz
7.oz

Reputation: 119

Edit

Okay, I just have to separate the port and host into individual fields:

"development": {
    "username": "postgres",
    "password": "postgres",
    "database": "postgres",
    "host": "localhost",
    "port": "5434",
    "dialect": "postgres"
  },

I had the same issue with docker-compose and sequelize-cli

  node:
    build:
      context: ./node
      target: dev
    volumes:
      - ./node/src:/app/src
      - ./node/private:/app/private
    ports:
      - 3111:3000
    command: npm run dev
    environment:
      - NODE_ENV=development
    env_file:
      - ./node/.env
    depends_on:
      - db

  db:
    image: postgres:12-alpine
    ports:
      - 5434:5432
    environment:
      - POSTGRES_HOST_AUTH_METHOD=trust
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=postgres
  "development": {
    "username": "postgres",
    "password": "postgres",
    "database": "postgres",
    "host": "localhost:5434",
    "dialect": "postgres"
  }

when i run ❯ npx sequelize-cli db:migrate

I get the following error :

Sequelize CLI [Node: 15.2.1, CLI: 6.2.0, ORM: 6.3.5]

Loaded configuration file "config/config.json".
Using environment "development".

ERROR: getaddrinfo ENOTFOUND localhost:5434

and it works with the following command, It's more of a workaround

❯ npx sequelize-cli db:migrate --url 'postgres://postgres:postgres@localhost:5434/postgres'

Sequelize CLI [Node: 15.2.1, CLI: 6.2.0, ORM: 6.3.5]

Parsed url postgres://postgres:*****@localhost:5434/postgres
No migrations were executed, database schema was already up to date.

Upvotes: 5

Related Questions