Redis cluster on Docker Compose with network_mode host - Unable to connect to redis-cluster

Viewed 1134

I set up a Redis cluster with 6 nodes as follows. From the redis-cluster documentation, network_mode host must be used.

version: "3.9"
    services:
        redis0:
          build:
            context: .
            dockerfile: ./DockerFile_Builders/Redis/Dockerfile-base
            args:
              PORT_CONFIG: 7000
              PORT_BUS_CONFIG: 8100
          network_mode: "host"
    
        redis1:
          build:
            context: .
            dockerfile: ./DockerFile_Builders/Redis/Dockerfile-base
            args:
              PORT_CONFIG: 7001
              PORT_BUS_CONFIG: 8101
          network_mode: "host"
    
        redis2:
          build:
            context: .
            dockerfile: ./DockerFile_Builders/Redis/Dockerfile-base
            args:
              PORT_CONFIG: 7002
              PORT_BUS_CONFIG: 8102
          network_mode: "host"
    
        redis3:
          build:
            context: .
            dockerfile: ./DockerFile_Builders/Redis/Dockerfile-base
            args:
              PORT_CONFIG: 7003
              PORT_BUS_CONFIG: 8103
          network_mode: "host"
    
        redis4:
          build:
            context: .
            dockerfile: ./DockerFile_Builders/Redis/Dockerfile-base
            args:
              PORT_CONFIG: 7004
              PORT_BUS_CONFIG: 8104
          network_mode: "host"
    
        redis5:
          build:
            context: .
            dockerfile: ./DockerFile_Builders/Redis/Dockerfile-base
            args:
              PORT_CONFIG: 7005
              PORT_BUS_CONFIG: 8105
          network_mode: "host"
    
    
        redis-cluster:
          tty: true
          build:
            context: .
            dockerfile: ./DockerFile_Builders/Redis/Dockerfile
          #hostname: redis-server
          depends_on:
            - redis1
            - redis2
            - redis3
            - redis4
            - redis5
          network_mode: "host"
        
        user_service:
          depends_on:
            - "redis-cluster"
          links:
            - "redis-cluster"
          build:
            context: .
            dockerfile: DockerFile_Builders/Services/User/Dockerfile

Now it is required to connect to the Redis cluster from the user-service container, when I try to connect using:

RedisURI masterURI = RedisURI.create("redis://redis-cluster:7001");

I get the following error:

19:57:28.329 [lettuce-epollEventLoop-4-1] WARN com.lambdaworks.redis.cluster.topology.ClusterTopologyRefresh - Unable to connect to redis-cluster/:7003 chat_service_1 | java.util.concurrent.CompletionException: java.net.UnknownHostException: redis-cluster

However, when the user-service is launched locally (without the docker container) and the Redis cluster is still in a Docker container, the connection is established normally using:

RedisURI masterURI = RedisURI.create("redis://localhost:7001");
1 Answers

I am not a Java developer but it looks like you are building the DSN to your Redis cluster using the Docker Compose service name:

redis://redis-cluster:7001

redis-cluster is the name of the service in your docker-compose.yml file

Docker provides such hostnames within its own networks from its embedded DNS server. As you are using the host network mode there is no Docker network which are usually declared in the networks section of the docker-compose.yml.

That means that you must use host names known to your current machine. During development you can continue to use localhost. When deploying, depending on the architecture of your application I think you will have to use private/public IPv4 address of the production server.

Managing multiple environments is easy with Docker Compose/Swarm using multiple configuration files.

Related