Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails to connect to redis, when running inside of docker container #763

Closed
mishushakov opened this issue Dec 12, 2018 · 30 comments
Closed

Comments

@mishushakov
Copy link

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.

@luin
Copy link
Collaborator

luin commented Dec 12, 2018

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 }

@mishushakov
Copy link
Author

Hey, @luin thanks for quick reply, I will try it and comment back in the issue in a second. Thank you

@mishushakov
Copy link
Author

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)

@luin
Copy link
Collaborator

luin commented Dec 12, 2018

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

@mishushakov
Copy link
Author

Ok, then I will try to hack around on my docker machine. Thank you

@hendrixroa
Copy link

@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.

@aiyogg
Copy link

aiyogg commented Sep 6, 2019

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

@aiyogg
Copy link

aiyogg commented Sep 6, 2019

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.

@portinos
Copy link

portinos commented Sep 8, 2019

@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.

Hey! How about running redis without docker-compose? I mean, running only the image. I lift the redis container using docker run --name redis.server -d -p 6379:6379 redis:latest but I can't connect from Sails (Node.js) using redis.server as hostname; the response is ENOTFOUND. The docker container is the official: https://hub.docker.com/_/redis/

@codebanesr
Copy link

@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

@ravipam
Copy link

ravipam commented Nov 25, 2019

@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",

@armenr
Copy link

armenr commented Dec 7, 2019

I'm having a similar issue, and I've tried it a few different ways as suggested here.

@codebanesr
Copy link

Try using something like this, i dont see you using the port no to connect
to redis.
redis: new Redis({ port: 6379, host: process.env.REDIS_HOST, password: process.env.REDIS_PASS, })

in place of host you can simply use 'redis'
dont forget to provide the port number

@shamim-42
Copy link

shamim-42 commented May 20, 2020

@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.

After couple of days of stuck today my project is running in your way. Thanks !

@mhtsharma948
Copy link

@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.

This still doesn't works for me, throwing this error.
redis_1 | 1:M 21 Jul 2020 17:47:13.048 # Could not create server TCP listening socket redis:6380: Name or service not known

@saefullohmaslul
Copy link

@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

@cuongndc
Copy link

cuongndc commented Sep 11, 2020

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
}

@erinoggz
Copy link

erinoggz commented Oct 3, 2020

Solution
const client = redis.createClient({
    host: 'redis-server',
    port: 6379
})

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

@DrPep
Copy link

DrPep commented Nov 22, 2020

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!

@khamphou
Copy link

@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.

@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

You saved my life too, spent many days to fix this nodejs code, and it looks like it was caused by the docker used in my QNAP container station.

redis:
image: redis:latest
container_name: devredisjs
restart: unless-stopped
environment:
NODE_ENV: development
PORT: 32700
ports:
- "32700:6379"
volumes:
- /share/Smart-Web/cache/redis:/data
command: ["redis-server", "--bind", "redis", "--port", "6379"]

@houstondapaz
Copy link

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

@AA25
Copy link

AA25 commented Apr 27, 2021

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.

@sharkdevs
Copy link

Solution
const client = redis.createClient({
    host: 'redis-server',
    port: 6379
})

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

you are a saviour indeed

@codeyourwayup
Copy link

Solution
const client = redis.createClient({
host: 'redis-server',
port: 6379
})

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

Works!! thanks

@ahmad2smile
Copy link

Following seems related: StackExchange/StackExchange.Redis#1002

@cindyloo
Copy link

cindyloo commented Feb 9, 2022

inside my docker-compose.yml:

    image: redis:6.2
    restart: always
   
    ports:
      - "6379:6379"

and inside my code:

const redisClient = createClient({
   legacyMode:true
  });

// not sure if I need this or what this should be if we are in legacy mode. use host and port ???
//url: "redis://default:xxx@redis-15574.c17961xxx"

redisClient.on("error", (err) => console.log("Redis Client Error", err));

redisClient.connect();
...
store: new RedisStore({ client: redisClient }),

now I get

2022-02-09T14:57:36.816625+00:00 app[web.1]: Redis Client Error Error: connect ECONNREFUSED 127.0.0.1:6379
2022-02-09T14:57:36.816634+00:00 app[web.1]:     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1159:16) {
2022-02-09T14:57:36.816635+00:00 app[web.1]:   errno: -111,
2022-02-09T14:57:36.816636+00:00 app[web.1]:   code: 'ECONNREFUSED',
2022-02-09T14:57:36.816636+00:00 app[web.1]:   syscall: 'connect',
2022-02-09T14:57:36.816637+00:00 app[web.1]:   address: '127.0.0.1',
2022-02-09T14:57:36.816637+00:00 app[web.1]:   port: 6379
2022-02-09T14:57:36.816637+00:00 app[web.1]: }

@kmaramr
Copy link

kmaramr commented Jun 10, 2022

@cindyloo Were you able to resolve the connection issue? I have been having the same.

@renatoam
Copy link

renatoam commented Jun 17, 2022

I don't know why, neither if I'm doing something wrong, but what worked for me was put the same name to REDIS_HOST and the container_name, in compose file.

docker-compose.yaml

cache:
    image: redis
    restart: always
    command: redis-server --requirepass ${REDIS_PASSWORD}
    container_name: ${REDIS_HOST} # getting name from .env file

redis.ts

export const redis = new Redis({
  port: Number(process.env.REDIS_PORT) || 6379,
  host: process.env.REDIS_HOST, // getting the same name from .env file
  password: process.env.REDIS_PASSWORD
});

I just could notice that after @luin comment, so thanks fot that!

@sbk2k1
Copy link

sbk2k1 commented Jul 26, 2023

Update: 26 July 2023

My docker-compose looks like this

redis:
    image: redis:latest
    restart: always
    environment:
      - ALLOW_EMPTY_PASSWORD=yes
    networks:
      - my-network
    ports:
      - 6379:6379

Passing the REDIS url through the Backend like this

environment:
      - ACCESS_TOKEN_SECRET=${ACCESS_TOKEN_SECRET}
      - JWT_SECRET=${JWT_SECRET}
      - PORT=${PORT}
      - MONGO_DB_URL=${MONGO_DB_URL}
      - API_INTERVAL=${API_INTERVAL}
      - REDIS_URL=${REDIS_URL}

.env looks like this

REDIS_URL='redis://redis:6379'

and using it in Node

var client = redis.createClient({url: process.env.REDIS_URL});

@myoshi2891
Copy link

myoshi2891 commented Sep 9, 2023

#763 (comment)

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

Thank you so much for sharing your knowledge! You save my day🙏🏻🙏🏻🙏🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests