0

I got a redis nodejs setup. where I can not connect between the two services as long as nodejs is in a dockercontainer.

When I start the nodejs service directly on the server or connect with rediscli from an other server it works. I tried to create a dockernetwork but no change. I also tried different naming settings. I also tried to activate legacymode on the redisclient.

do I have to activate interconnections in docker somehow?

Error message:

url-shortener_1  | Server running: 1339
url-shortener_1  | node:internal/process/promises:289
url-shortener_1  |             triggerUncaughtException(err, true /* fromPromise */);
url-shortener_1  |             ^
url-shortener_1  |
url-shortener_1  | Error: connect ECONNREFUSED 127.0.0.1:6379
url-shortener_1  |     at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16)
url-shortener_1  | Emitted 'error' event on Commander instance at:
url-shortener_1  |     at RedisSocket.<anonymous> (/app/node_modules/@redis/client/dist/lib/client/index.js:390:14)
url-shortener_1  |     at RedisSocket.emit (node:events:512:28)
url-shortener_1  |     at RedisSocket._RedisSocket_connect (/app/node_modules/@redis/client/dist/lib/client/socket.js:167:18)
url-shortener_1  |     at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
url-shortener_1  |   errno: -111,
url-shortener_1  |   code: 'ECONNREFUSED',
url-shortener_1  |   syscall: 'connect',
url-shortener_1  |   address: '127.0.0.1',
url-shortener_1  |   port: 6379
url-shortener_1  | }

docker-compose.yml

version: '3'

services:
  url-shortener:
    build: .
    ports:
      - 1339:1339
    depends_on:
      - redis
    networks:
      - ubuntu-host01
    links:
      - redis
  redis:
    image: redis:latest
    ports:
      - 6379:6379
    command: redis-server /usr/local/etc/redis/redis.conf
    volumes:
      - ./redis.conf:/usr/local/etc/redis/redis.conf
    networks:
      - ubuntu-host01

networks:
  ubuntu-host01:
    external: true

docker network ls

NETWORK ID     NAME               DRIVER    SCOPE
38a53faf180b   ubuntu-host01      bridge    local

redis.conf

requirepass PASSWORD
#aclfile /etc/redis/users.acl

index.js

const express = require('express')
const redis = require('redis')
const google = require("./google.js");

const port = 1339


const rdb = redis.createClient({
  legacyMode: true,
  host: 'redis', # also tried 127.0.0.1 or localhost
  port: '6379',
  password: 'PASSWORD'
})

rdb.on('connect', () => {
  console.log('Connected to Redis')
})

rdb.connect();
const app = express()

those are the dependencies in the package.json

  "dependencies": {
    "express": "^4.18.2",
    "googleapis": "^111.0.0",
    "redis": "^4.6.4"
  }
  • Try to add `hostname: redis` to redis service in the docker-compose file. Also try `docker inspect redis` to see what hostname is redis using. It's under `Config.Hostname` – Molda Mar 02 '23 at 10:01

1 Answers1

0

Change you redis client. Use this

rdb = redis.createClient({
socket: {
    host: 'redis',
    port: '6379',
    password: 'PASSWORD',
    legacyMode: true,
}

})

More information here redis client

P.S. User --build for recreating containers local. Some times compose use old containers with old files.

  • this worked, but makes not really sense to me as the code works outside docker I had to remove the password in redis as this resulted in a new error NOAUTH required – Olivier Eggimann Mar 02 '23 at 10:45
  • I think at some point there was a syntax change in the `redis.createClient()` call. If you don't have the `socket:` then the `host:` isn't recognized; that's why you see `127.0.0.1` in the error message even though you've specified a different host name in the `createClient()` call. – David Maze Mar 02 '23 at 11:48