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");