Kunok
Kunok

Reputation: 8769

While using docker-compose, node application fails to connect to Redis

I am running Ubuntu and trying to setup dev environment for node app, this is my docker-compose.yml:

version: "2"
services:
  redis:
    image: redis
    ports:
     - "6381:6379"
    volumes:
      - /var/lib/thinklog-queue:/data
  activity-service:
    build:
      context: ./activity-service
    command: yarn dev
    ports:
      - "3001:3001"
    environment:
      REDIS_QUEUE_URL: "redis://redis:6381"
    depends_on:
      - redis

and Dockerfile for the node app looks like this:

FROM node:9
WORKDIR /app
COPY package.json ./
COPY yarn.lock ./
RUN yarn install
COPY . .
EXPOSE 4000
CMD ["yarn", "start"]

I extend Redis connection string from env variable.

and when I run docker-compose build && docker-compose up inside logs I get this output from the node app:

activity-service_1 | Error: Redis connection to redis:6381 failed - connect ECONNREFUSED 172.20.0.2:6381

It appears that setup is right however there is something blocking intern connections to Redis. (inside this docker compose network)

Connections to this Redis instance from outside, e.g other shell session works just fine:

kuno@dell:~/code/THINKLOG/activity-service$ redis-cli -p 6381
127.0.0.1:6381> ping
PONG

I also tried mounting custom config like so (added new item to volume arrays for Redis inside docker-compose.yml):

  - ./redis.conf:/usr/local/etc/redis/redis.conf

which has bind line commented out as well as modifying it to be like so (the last address is one that docker-compose uses internally):

bind 192.168.1.100 10.0.0.1 172.20.0.2

Should I also mount in custom Redis configuration and configure Redis to allow connections within docker network?

Upvotes: 0

Views: 839

Answers (1)

nguyenkha
nguyenkha

Reputation: 1356

Change port to 6379 in the url REDIS_QUEUE_URL: "redis://redis:6379" 6381 is the port on the host IP not the redis container port.

Upvotes: 2

Related Questions