Redis connection to my-redis:6379 failed - getaddrinfo ENOTFOUND when running seeds

Viewed 4327

I am using Docker for the container service.

I have created a seed file and run it by npx sequelize-cli db:seed:all, then error occur:

Sequelize CLI [Node: 13.12.0, CLI: 6.2.0, ORM: 6.5.1]

Loaded configuration file "migrations/config.js".
Using environment "development".
events.js:292
      throw er; // Unhandled 'error' event
      ^

Error: Redis connection to my-redis:6379 failed - getaddrinfo ENOTFOUND my-redis
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26)
Emitted 'error' event on RedisClient instance at:
    at RedisClient.on_error (/Users/CCCC/Desktop/Source Tree/my-server/node_modules/redis/index.js:342:14)
    at Socket.<anonymous> (/Users/CCCC/Desktop/Source Tree/my-server/node_modules/redis/index.js:223:14)
    at Socket.emit (events.js:315:20)
    at Socket.EventEmitter.emit (domain.js:485:12)
    at emitErrorNT (internal/streams/destroy.js:84:8)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  errno: -3008,
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'my-redis'
}

It seems to show that my redis is not found/not running in port 6379.

Then I run docker ps, it shows my-redis run in port 6379.


CONTAINER ID        IMAGE                            COMMAND                  CREATED             STATUS              PORTS                               NAMES
...
f637ee218d03        redis:6                          "docker-entrypoint.s…"   18 minutes ago      Up 18 minutes       0.0.0.0:6379->6379/tcp              my-server_my-redis_1

docker-compose.yml

version: '2.1'

services:
  my-db:
    image: mysql:5.7
    ...
    ports:
      - 3306:3306
  my-redis:
    image: redis:6
    ports:
      - 6379:6379
  my-web:
    restart: always
    environment:
      - NODE_ENV=dev
      - PORT=3030
    build: .
    command: >
      sh -c "npm install && ./wait-for-db-redis.sh my-db my-redis npm run dev"
    ports:
      - "3030:3030"
    volumes:
      - ./:/server
    depends_on:
      - my-db
      - my-redis

.sequelizerc

const path = require('path');

module.exports = {
  'config': path.resolve('migrations/config.js'),
  'seeders-path': path.resolve('migrations/seeders'),
  'models-path': path.resolve('migrations/models.js')
};

migrations/model.js

const Sequelize = require('sequelize');
const app = require('../src/app');
const sequelize = app.get('sequelizeClient');
const models = sequelize.models;

module.exports = Object.assign({
  Sequelize,
  sequelize
}, models);

config.js

const app = require('../src/app');
const env = process.env.NODE_ENV || 'development';
const dialect = 'mysql';

module.exports = {
  [env]: {
    dialect,
    url: app.get(dialect),
    migrationStorageTableName: '_migrations'
  }
};

1 Answers

Are you running the migration within the Docker Compose container for your app, or on the Docker host machine?

From the host machine's point of view, there is no such hostname as my-redis (it's only a thing within a Docker overlay network with that container in it).

Since you've exposed the Redis port 6379 to your host (and in fact the whole wide world), you'd use localhost:6379 on the host machine.

Related