aditya gaddhyan
aditya gaddhyan

Reputation: 73

Redis on docker fails to run due to multiple error

I have been trying to connect a redis container to my node app container but i have been getting the following error. Please slove before reporting the question.

docker-compose

version: "3"

services:
  vote:
    build: ./voting/.
    ports:
      - "3000:3000"
    networks:
      - front-tier
      - back-tier

  result:
    build: ./result/.
    ports:
      - "3011:3011"
    networks:
      - front-tier
      - back-tier

  worker:
    build:
      context: ./service_worker/.
    depends_on:
      - "redis"
      - "db"
    networks:
      - back-tier

  redis:
    image: redis:alpine
    container_name: redis
    ports: ["6379"]
    networks:
      - back-tier

  db:
    image: mongo:latest
    container_name: db
    networks:
      - back-tier

volumes:
  db-data:

networks:
  front-tier:
  back-tier:

in docker-compose up

vote_1    | the app is running
vote_1    | events.js:292
vote_1    |       throw er; // Unhandled 'error' event
vote_1    |       ^
vote_1    | 
vote_1    | Error: Redis connection to 0.0.0.0:6379 failed - connect ECONNREFUSED 0.0.0.0:6379
vote_1    |     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
vote_1    | Emitted 'error' event on RedisClient instance at:
vote_1    |     at RedisClient.on_error (/usr/src/app/node_modules/redis/index.js:341:14)
vote_1    |     at Socket.<anonymous> (/usr/src/app/node_modules/redis/index.js:222:14)
vote_1    |     at Socket.emit (events.js:315:20)
vote_1    |     at emitErrorNT (internal/streams/destroy.js:92:8)
vote_1    |     at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
vote_1    |     at processTicksAndRejections (internal/process/task_queues.js:84:21) {
vote_1    |   errno: 'ECONNREFUSED',
vote_1    |   code: 'ECONNREFUSED',
vote_1    |   syscall: 'connect',
vote_1    |   address: '0.0.0.0',
vote_1    |   port: 6379
vote_1    | }

in docker logs redis

1:C 17 Oct 2020 07:36:16.705 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 17 Oct 2020 07:36:16.706 # Redis version=6.0.8, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 17 Oct 2020 07:36:16.706 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 17 Oct 2020 07:36:16.708 * Running mode=standalone, port=6379.
1:M 17 Oct 2020 07:36:16.708 # Server initialized
1:M 17 Oct 2020 07:36:16.708 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 17 Oct 2020 07:36:16.709 * Ready to accept connections

When I try docker exec -ti redis redis-server

13:C 17 Oct 2020 07:42:16.608 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
13:C 17 Oct 2020 07:42:16.608 # Redis version=6.0.8, bits=64, commit=00000000, modified=0, pid=13, just started
13:C 17 Oct 2020 07:42:16.608 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
13:M 17 Oct 2020 07:42:16.608 # Could not create server TCP listening socket *:6379: bind: Address in use

Now what i think here is in several places, i have seen this error of redis-config file but how to solve it in docker image, this is explained nowhere nicely. There is no problem in connection but the problem lies in conf of redis. I am able to ping into that container from other container with dns/ip. I think if the last part of error where *:6379 could not be heard, if this and the conf file is edited, the app will work fine.

Upvotes: 2

Views: 1963

Answers (1)

mehrdadep
mehrdadep

Reputation: 1019

You are connecting to redis container from another container with 0.0.0.0:6379 address. Use docker service name redis:6379 or your host's ip if you're exposing the redis container port to the host machine.

Upvotes: 2

Related Questions