Here we are with another docker issue. I am writing a node app that needs to use redis, node app is inside a container and redis another container both on the same custom network.
Using docker compose:
node-app-test:
platform: linux/amd64
container_name: node-app-test
depends_on:
- redis-test
image: my-custom-image
ports:
- 8000:8000
networks:
- my-network-test
links:
- redis-test
redis-test:
platform: linux/amd64
container_name: redis-test
image: redis
ports:
- 6379:6379
networks:
- my-network-test
The basics of the node app that connects to redis is simply:
const redis = require("redis");
const redisClient = redis.createClient({
host: 'redis-test',
port: 6379
})
redisClient.connect();
redisClient.on("connect", () => {
console.log("REDIS IS CONNECTED")
})
redisClient.on("error", (error) => {
console.log("REDIS ERROR", error)
})
The error event is trapped with the following:
connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1195:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 6379
}
I have done a test outside of the container and the redis container does accept connections, not sure what's going on.
UPDATE
I think it's the redis node library that's the issue, it works if i connect by URL instead of host and port