ioredis: Fails to connect to redis, when running inside of docker container

Hey, the library works like a charm, thanks a lot. My application is a microservice, which connects to a redis database, which is running inside of docker. However, I can not connect to redis, when my application is running inside of container. I can still connect to redis remotely via cli on other host and it clearly works. I can also run my application outside of docker (directly on my host machine) and it would still connect. But when I build and run the image, it says:

[ioredis] Unhandled error event: Error: connect ECONNREFUSED 127.0.0.1:6379
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1085:14)

I also tried to provision my application to docker swarm with compose file on the same network as redis

version: "3"
services:
  myapp:
    image: user/app:latest
    deploy:
      restart_policy:
        condition: on-failure
    ports:
      - "5000:5000"
    networks:
      - webnet

  redis:
    image: redis
    ports:
      - "6379:6379"
    volumes:
      - "./data:/data"
    command: redis-server --appendonly yes
    networks:
      - webnet
      
networks:
  webnet:

But it still wouldn’t connect

Here is the code, I use in my application:

const redis = require('ioredis')
const appdb = new redis()

module.exports = { db }

Thank you in advance.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 30

Most upvoted comments

@MishUshakov Try delete the networks and replace redis section by:

redis:
    image: redis:latest
    command: ["redis-server", "--bind", "redis", "--port", "6379"]

That works for me.

Have you tried connecting to Redis via host “redis” (see https://docs.docker.com/compose/networking/)?

const redis = require('ioredis')
const appdb = new redis({host: 'redis'})

module.exports = { db }

The same issue, any solution for this problem? Thanks.

version

"ioredis": "^4.17.3",
"typescript": "^3.8.3"

docker-compose

version: "3"

networks:
  dev_env_network:
    driver: bridge

services:
  redis:
    container_name: redis
    image: redis:6.0-alpine
    restart: always
    ports:
      - 6379:6379
    volumes:
      - redis:/data
    networks:
      - dev_env_network
  auth-service:
    container_name: auth-service
    image: node:12
    working_dir: /app
    env_file: .env
    environment:
      - PG_DB=auth-service
    ports:
      - 50002:50002
    volumes:
      - ../auth-service:/app
      - ../auth-service/node_modules:/app/node_modules
    networks:
      - dev_env_network
    command: yarn start:dev

application

import Redis from 'ioredis';
const redis = new Redis({
    host: 'redis',
    port: 6379,
  });

error

Error: connect ECONNREFUSED 127.0.0.1:6379
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) {
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 6379
}
Solution
const client = redis.createClient({
    host: 'redis-server',
    port: 6379
})

Then rebuild ur docker with => docker-compose up --build 

For those who still have an issue here, my docker compose setup is/was configured with a bridged network setup. Adding Redis to the bridged network was required to allow it’s network peers to connect:

redis:
  container_name: redis
  image: redis:latest
  command: ["redis-server", "--bind", "redis", "--port", "6379"]
  networks:
    - mynetwork 

networks:
  mynetwork:
    driver: bridge

Then connecting from my docker-hosted Node app with:

import redis from "ioredis"
const appdb = new redis({ host: "redis", port: 6379 })

If you want to then bind from the host machine, add in the requisite docker-compose port binding to the redis config block:

ports:
  - 6379:6379

HTH!

@mishushakov Try delete the networks and replace redis section by:

redis:
    image: redis:latest
    command: ["redis-server", "--bind", "redis", "--port", "6379"]

That works for me.

you save my life

@mishushakov Try delete the networks and replace redis section by:

redis:
    image: redis:latest
    command: ["redis-server", "--bind", "redis", "--port", "6379"]

That works for me.

You saved me tonight

Still fails, but now with this error:

[ioredis] Unhandled error event: Error: getaddrinfo ENOTFOUND redis redis:6379
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:57:26)

I fix this issue changing the redis.conf, by default it use 127.0.0.1, changing to 0.0.0.0 works like a charm Just create an redis.conf file with this content:

# bind 127.0.0.1
bind 0.0.0.0

and send this file to container, using compose just link this volume

volumes:
  - /path/to/redis.conf:/tmp/redis.conf

@mishushakov Try delete the networks and replace redis section by:

redis:
    image: redis:latest
    command: ["redis-server", "--bind", "redis", "--port", "6379"]

That works for me.

You saved me tonight

Unfortunately did not work for me. I deleted the docker networks. do i need to delete any other network, if any , please guide me. i still have the error: [ioredis] Unhandled error event: Error: connect ECONNREFUSED 127.0.0.1:6379

Below is my docker-compose.yml file version: ‘3’ services: redis: image: ‘redis’ command: [“redis-server”, “–bind”, “redis”, “–port”, “6379”] ports: - “6379:6379” networks: - app-tier apiserver: build: . ports: - “7001:7000” networks: - app-tier depends_on: - redis networks: app-tier: driver: bridge

below is my app.js const Redis = require(‘ioredis’); const redis = new Redis(); I changed the above to const redis = new Redis({host: ‘redis’}); but i still get the error

package.json file references to ioredis, “ioredis”: “^4.0.2”,

For others whose issue wasn’t fixed by the above, this stackoverflow answer worked for me https://stackoverflow.com/a/58891367

If you’re trying to connect from an app running in a container e.g. node app to redis running in a docker container, then the host string you set in your node app will need to be the name of your redis service name.

Same problem for me, it’s still exist I’ve tried all suggestions above. Who can help me to resolve this, please.

It’s ok after I run docker-compose build then docker-compose up.

Will, that is a network problem and it should not be related to ioredis.