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"
}