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.
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 }
Hey, @luin thanks for quick reply, I will try it and comment back in the issue in a second. Thank you
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)
Will, that is a network problem and it should not be related to ioredis.
Ok, then I will try to hack around on my docker machine. Thank you
@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.
Same problem for me, it's still exist I've tried all suggestions above. Who can help me to resolve this, please.
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.
@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/
@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
@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",
I'm having a similar issue, and I've tried it a few different ways as suggested here.
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
@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 !
@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
@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
The same issue, any solution for this problem? Thanks.
"ioredis": "^4.17.3",
"typescript": "^3.8.3"
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
import Redis from 'ioredis';
const redis = new Redis({
host: 'redis',
port: 6379,
});
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
}
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!
Most helpful comment
@MishUshakov Try delete the networks and replace redis section by:
That works for me.