1

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

noname
  • 153
  • 2
  • 12
  • The connection error is showing 127.0.0.1, which means the app is not resolving `redis-test` – BMitch Jun 02 '22 at 10:24
  • 1
    Newer versions of the `redis` library require the connection parameters to be inside a `socket: { ... }` object and silently ignore directly passing `host:` in the object parameter. See for example [Docker Redis Error: connect ECONNREFUSED 127.0.0.1:6379](https://stackoverflow.com/questions/71360779/docker-redis-error-connect-econnrefused-127-0-0-16379). – David Maze Jun 02 '22 at 10:36

0 Answers0