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
@MishUshakov Try delete the networks and replace redis section by:
That works for me.
Have you tried connecting to Redis via host “redis” (see https://docs.docker.com/compose/networking/)?
The same issue, any solution for this problem? Thanks.
version
docker-compose
application
error
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:
Then connecting from my docker-hosted Node app with:
If you want to then bind from the host machine, add in the requisite docker-compose port binding to the
redisconfig block:HTH!
you save my life
You saved me tonight
Still fails, but now with this error:
I fix this issue changing the
redis.conf, by default it use127.0.0.1, changing to0.0.0.0works like a charm Just create an redis.conf file with this content:and send this file to container, using compose just link this volume
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.
It’s ok after I run
docker-compose buildthendocker-compose up.Will, that is a network problem and it should not be related to ioredis.