I am trying to containerize my whole developer workflow using docker-compose. Problem I am facing is with redis contianer. My worker container is unable to connect to redis. I trie different solution from stackoverflow like:
Setting up node with redis using docker-compose
Access redis locally on docker - docker compose
nodejs, redis, npm and docker-compose setup
Docker-compose , anyway to specify a redis.conf file?
How to connect to a Redis container using Docker Compose?
And many others also from github, tried different examples but no luck.
Here is my docker-compose file:
version: "3.6"
services:
redis_db:
# image: bitnami/redis:latest # tried different image also
image: redis
container_name: rme_redis
network_mode: host # tried networks also
# command: ['redis-server', '/redis.conf']
# volumes:
# - ./docker/redis.conf:/redis.conf
# hostname: redis_db
# networks:
# - rme
# environment:
# - ALLOW_EMPTY_PASSWORD=yes
# - REDIS_PASSWORD="redis"
ports:
- 6379:6379
# networks:
# node_net:
# ipv4_address: 172.28.1.4
worker:
build: worker
image: worker
container_name: rme_worker
command: node worker.js
# networks:
# - rme
# network_mode: host
environment:
- REDIS_URL="redis://redis_db:6379" # localhost when netowk_mode is host
volumes:
- ./worker:/app
# links:
# - redis_db
depends_on:
- redis_db
Error I am getting is:
Error: Redis connection to "redis://redis_db:6379" failed - connect ENOENT "redis://redis_db:6379"
at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) {
errno: -2,
code: 'ENOENT',
syscall: 'connect',
address: '"redis://redis_db:6379"'
}
Operating system: macOS
Docker: Docker Desktop
Edit: Found a solution using host and port in js code
const redisClient = redis.createClient({
host: process.env.REDIS_HOST,
port: parseInt(process.env.REDIS_PORT)
})
# docker-compose.yml
version: "3.6"
services:
redis_db:
image: redis
# only needed to directly access Redis from host
ports:
- 6379:6379
worker:
build: worker
environment:
- REDIS_HOST=redis_db
- REDIS_PORT=6379
depends_on:
- redis_db
If you find solution using redis connection string do share.
Thanks for your help