i have seen this question asked around a couple times, but never found a proper answer that works for me, so i will give it a go.
I have sat up a worker service in its own docker container which uses Bull Queue. Then i have redis in a seperate container. All worked fine when redis was in a container and the worker was running locally, but now the worker just prints Error connecting to Redis: Error: connect ECONNREFUSED 127.0.0.1:6379 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1278:16) { errno: -111, code: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 6379 }
Which i find kind of strange, since i am referring to the container name when i set up Bull. In fact no matter what i change the host to in the setup, it prints the same error. Unless i run the worker locally, then it prints whichever host i input. I have also tried changing the "connectionName" field without any changes.
Docker compose:
version: '3.1'
services:
redis:
image: redis:latest
restart: always
container_name: my_cache
ports:
- '6379:6379'
command: redis-server --save 20 1 --loglevel warning --requirepass pirate
volumes:
- redis-data:/data
worker:
image: worker
build: ../worker
environment:
- REDIS_HOST=local:redis:6379
ports:
- "6000:6000"
links:
- redis
volumes:
redis-data:
driver: local
queue.ts
import Bull from "bull";
export const gameEventQueue = new Bull("game-event", {
redis: {
host: 'redis',
password: "pirate"
}
});
Bull versions "bull": "^4.12.2", "bull-board": "^2.1.3",
i also tried to set ENV in docker compose like this
worker:
image: worker
build: ../worker
environment:
- REDIS_HOST=local:redis:6379
And connect like this,
import Bull from "bull";
export const gameEventQueue = new Bull("game-event", {
redis: {
host: process.env.REDIS_HOST,
password: "pirate"
}
});
But still no Joy. I appreciate all help
The network name for the Redis container will simply be
redis. Here's a setup to demonstrate.docker-compose.ymlI'm suppressing output from the
redisservice just to keep things clean. There are three environment variables being passed to theworkerservice, with the network host and port for the Redis container and the password.Now a simple Docker image for the worker. The
iputils-pingpackage is just required so that we can runpingin the worker.DockerfileFinally a worker script, which is just a simple BASH script that demonstrates connecting to the Redis container. This is what it does:
pingto echo packets off the Redis container.redis-clito send thePINGcommand to the Redis server.worker.shAnd here's what that looks like:
If you look carefully you'll see:
PING redis. This establishes that the worker container can see the Redis container.PINGcommand, to which the server responds with aPONG(which is also visible in the logs).