简体   繁体   中英

NodeJS converting Docker Redis hostname to localhost

It seems the Redis container hostname is being converted to localhost by NodeJS.

Here are my files:

.env

REDIS_HOST=redis-eventsystem
REDIS_PORT=6379
REDIS_SECRET=secret

index.ts

// there are things above this
let Redis = require('redis');
let client : any = Redis.createClient({
  host: process.env.REDIS_HOST,
  port: process.env.REDIS_PORT,
  legacyMode: true
});
client.on('error', (err : Error) : void => {
  console.error(
    `Redis connection error: ${err}`
  );
});
client.on('connect', (err : Error) : void => {
  console.info(
    `Redis connection success.`
  );
});
client.connect();
// there are things bellow this

docker-compose.yml

version: '3.8'
services:
  eventsystem:
    image: eventsystem
    restart: always
    depends_on:
      - "redis-eventsystem"
    ports:
      - "80:3000"
    networks:
      - eventsystem
  redis-eventsystem:
    image: redis
    command: ["redis-server", "--bind", "redis-eventsystem", "--port", "6379", "--protected-mode", "no"]
    restart: always
    networks:
      - eventsystem
networks:
  eventsystem:
    driver: bridge

docker log

2022-11-21 17:50:41 eventsystem-redis-eventsystem-1  | 1:C 21 Nov 2022 20:50:41.106 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2022-11-21 17:50:41 eventsystem-redis-eventsystem-1  | 1:C 21 Nov 2022 20:50:41.106 # Redis version=7.0.5, bits=64, commit=00000000, modified=0, pid=1, just started
2022-11-21 17:50:41 eventsystem-redis-eventsystem-1  | 1:C 21 Nov 2022 20:50:41.106 # Configuration loaded
2022-11-21 17:50:41 eventsystem-redis-eventsystem-1  | 1:M 21 Nov 2022 20:50:41.106 * monotonic clock: POSIX clock_gettime
2022-11-21 17:50:41 eventsystem-redis-eventsystem-1  | 1:M 21 Nov 2022 20:50:41.108 * Running mode=standalone, port=6379.
2022-11-21 17:50:41 eventsystem-redis-eventsystem-1  | 1:M 21 Nov 2022 20:50:41.108 # Server initialized
2022-11-21 17:50:41 eventsystem-redis-eventsystem-1  | 1:M 21 Nov 2022 20:50:41.108 * Ready to accept connections
2022-11-21 17:50:41 eventsystem-eventsystem-1        | 
2022-11-21 17:50:41 eventsystem-eventsystem-1        | > eventsystem@1.0.0 start
2022-11-21 17:50:41 eventsystem-eventsystem-1        | > node index.js serve
2022-11-21 17:50:41 eventsystem-eventsystem-1        | 
2022-11-21 17:50:42 eventsystem-eventsystem-1        | Application is listening at http://localhost:3000
2022-11-21 17:50:42 eventsystem-eventsystem-1        | Mon Nov 21 2022 20:50:42 GMT+0000 (Coordinated Universal Time) - Redis connection error: Error: connect ECONNREFUSED 127.0.0.1:6379

As you all can see the connection is refused for the IP 127.0.0.1 but on my application the redis is set to work on the hostname for the container which holds the redis server. I can't think of anything that may be causing this problem.

So to answer my own question, basically the problem was related to the variables passed on createClient at my code.

It seems that for some unknown reason the host and port variables need to be passed inside a variable called socket inside the createClient argument object.

So, instead of doing the usual and passing the host and port inside the argument object, you must do the following:

let client : any = Redis.createClient({
  host: process.env.REDIS_HOST,
  port: process.env.REDIS_PORT,
  socket: {
    host: process.env.REDIS_HOST,
    port: process.env.REDIS_PORT
  },
  legacyMode: true
});

Hope to have helped someone else besides me.

Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM