Search code examples
c#dockerasp.net-coredocker-composeredis

redis-commander can't connect to redis service


I am trying to set up redis and redis-commander in docker-compose. The redis container is successfully created and started, but in redis-commander I get the following errors:

 setUpConnection (R:localhost:6379:0) Redis error Error: connect ECONNREFUSED 127.0.0.1:6379
     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16)

 setUpConnection (R:6379:6379:0) Redis error Error: connect ECONNREFUSED 0.0.24.235:6379
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16)

Here is the setup in docker-compose:

redis:
    image: redis:latest
    restart: always
    ports:
      - '6379:6379'
    command: redis-server --requirepass myPassword
    environment:
      - REDIS_USERNAME=myUsername
      - REDIS_PASSWORD=myPassword
    volumes:
      - cache:/data
    healthcheck:
      test: [ "CMD", "redis-cli", "ping" ]
      interval: 10s
      timeout: 5s
      retries: 3
      
  redis-commander:
    image: rediscommander/redis-commander
    depends_on:
      - redis
    ports:
      - "8081:8081"
    environment:
      - REDIS_HOSTS=redis:6379
      - REDIS_PASSWORD=myPassword
      - REDIS_USERNAME=myUsername
        
volumes:
  cache:
  redis:
  redis-commander:

I tried different options;

- REDIS_HOSTS=redis:6379
- REDIS_HOSTS=127.0.0.1:6379

Also explicitly specify the command:

redis-server --requirepass myPassword --bind 127.0.0.1

Solution

  • Try updating the REDIS_HOSTS variable for the redis-commander service as follows:

    environment:
      - REDIS_HOSTS=local:redis:6379
    

    The local is an alias for the Redis connection, which you can customize if needed.