I’m having a issue trying to connect my app container to the redis container.
Keep getting the same error -> Error: connect ECONNREFUSED 127.0.0.1:6379
But this only happens if I run both in a docker container. If I try to run only redis in a container and my app locally all seems to be working fine.
Here is part of my .js code that connects to redis:
const express = require('express');
const router = express.Router();
const userService = require('../service/userService')
const constants = require('../helpers/constants')
const redis = require('redis');
const client = redis.createClient(6379, 'redis');
client.connect();
var checkCache = async (req, res, next) => {
const data = await client.get('get-users');
if(!data){
next();
}else{
client.disconnect();
return res.status(200).json(JSON.parse(data));
}
};
router.get('/users', checkCache, async function (req, res) {
const users = await userService.getUser();
client.set('get-users', JSON.stringify(users));
client.expire('get-users', 60);
client.disconnect();
return res.status(200).json(users)
});
My docker-compose.yml:
version: '3.2'
services:
redis:
container_name: "redis"
image: redis:latest
ports:
- "6379:6379"
networks:
node_net:
ipv4_address: 172.28.1.4
volumes:
- $PWD/redis.conf:/usr/local/etc/redis/redis.conf
command: [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
market:
tty: true
container_name: "market_place"
build:
context: ./
dockerfile: ./docker/Dockerfile
volumes:
- .:/usr/src/app
networks:
node_net:
ipv4_address: 172.28.1.5
ports:
- "${MF_PORT:-4000}:4000"
depends_on:
- redis
links:
- redis
networks:
node_net:
ipam:
driver: default
config:
- subnet: 172.28.0.0/16
The error I get after building and starting my container is the following:
Listening in port 4000
market_place | node:internal/process/promises:246
market_place | triggerUncaughtException(err, true /* fromPromise */);
market_place | ^
market_place |
market_place | Error: connect ECONNREFUSED 127.0.0.1:6379
market_place | at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16)
market_place | Emitted 'error' event on Commander instance at:
market_place | at RedisSocket.<anonymous> (/usr/src/app/node_modules/@node-redis/client/dist/lib/client/index.js:332:14)
market_place | at RedisSocket.emit (node:events:390:28)
market_place | at RedisSocket._RedisSocket_retryConnection (/usr/src/app/node_modules/@node-redis/client/dist/lib/client/socket.js:146:14)
market_place | at processTicksAndRejections (node:internal/process/task_queues:96:5)
market_place | at async RedisSocket._RedisSocket_connect (/usr/src/app/node_modules/@node-redis/client/dist/lib/client/socket.js:113:55)
market_place | at async Commander.connect (/usr/src/app/node_modules/@node-redis/client/dist/lib/client/index.js:156:9) {
market_place | errno: -111,
market_place | code: 'ECONNREFUSED',
market_place | syscall: 'connect',
market_place | address: '127.0.0.1',
market_place | port: 6379
market_place | }
market_place | npm notice
market_place | npm notice New minor version of npm available! 8.1.2 -> 8.3.0
market_place | npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.3.0
market_place | npm notice Run npm install -g [email protected] to update!
market_place | npm notice
Can somebody explain why it’s trying to connect to 127.0.0.1 even though my container ip is 172.28.1.4?
If i remove the network configuration on the docker compose and run my code locally, I’m able to connect and save everything in the redis database.
I’ve already checked some other questions here posted but none of them asked and resolved my problem.
Thanks!
2
Answers
Your code does not provide the IP of the Redis container when creating the client. Try this:
Check
createClient
configuration for more info.it can also work using