I can't figure out how to connect to my redis
service from my app
service. Using DDocker version 18.03.1-ce, build 9ee9f40ocker for Mac.
I've tried connecting the various ways I've found on similar questions:
const client = redis.createClient({ host: 'localhost', port: 6379});
const client = redis.createClient({ host: 'redis', port: 6379});
const client = redis.createClient('redis://redis:6379');
const client = redis.createClient('redis', 6379); // and reversed args
I always get some form of:
Error: Redis connection to localhost:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
Error: Redis connection to redis:6379 failed - connect ECONNREFUSED 172.20.0.2:6379
Docker containers
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0fd798d58561 app_app "pm2-runtime start e…" 2 seconds ago Up 7 seconds app
65d148e498f7 app_redis "docker-entrypoint.s…" About a minute ago Up 8 seconds 0.0.0.0:6379->6379/tcp redis
Redis works:
$ docker exec -it redis /bin/bash
root@65d148e498f7:/data# redis-cli ping
PONG
Redis Dockerfile (pretty simple)
FROM redis:4.0.9
COPY redis.conf /usr/local/etc/redis/redis.conf
CMD ["redis-server", "/usr/local/etc/redis/redis.conf"]
app Dockerfile
FROM node:10.3.0-slim
RUN mkdir -p /app
COPY src/* /app/
CMD ["pm2-runtime", "start", "/app/ecosystem.config.js"]
docker-compose.yml
version: "3"
services:
redis:
build: ./redis/
container_name: redis
restart: unless-stopped
ports:
- "6379:6379"
expose:
- "6379"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
environment:
- 'API_PORT=6379'
- 'NODE_ENV=production'
app:
depends_on:
- redis
build: ./app/
container_name: app
restart: unless-stopped
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /app/node_modules
environment:
- 'NODE_ENV=production'
sudo lsof -i tcp:6379
– Priorate6379
to debug. Both had the same error as noted above. – Tijuanabuild: ./redis/
toimage: your_image_name_build_from_dockerfile
? – PriorateRedis
not yet start... May you need to check your docker file, and redis configuration... – Prioratedocker ps
lists the redis container? – Result6379
port, that mean there is some problem with his Redis. It can be start and map to host port 6739 – PriorateRedis connection to redis:6379 failed
? Which ip address does it show? Is it the same asdocker inspect redis
shows? – Result172.20.0.2:6379
– Tijuanadocker run --net container:app_app redis redis-cli -h redis ping
– FortyfiveCould not connect to Redis at redis:6379: Connection refused
, when runningdocker run --net container:app redis redis-cli -h redis ping
– TijuanaCould not connect to Redis at redis:6379: Name or service not known
– Curren