The error message "Could not connect to Redis at 127.0.0.1:6380: Connection refused" typically means that Redis is not running on the specified port, or it is not accessible due to network issues, such as incorrect firewall or security group settings.
Since you're working with Docker on Windows, there are a few common reasons that might be causing this issue when trying to set up a Redis cluster:
-
Redis is not started: Ensure that all Docker containers for Redis are running by using the command docker ps to list all running containers.
-
Port mapping issues: Check that the port mappings in your Docker Compose file are correctly set up for the communication between the host and the containers. Also, ensure that the container ports are exposed to the correct host ports. The order of the port mapping is host:container.
-
Localhost binding: When running Docker on Windows, the localhost inside a container doesn't refer to the Windows host machine, but rather to the container itself. Therefore, trying to connect to other containers using 127.0.0.1 or localhost from inside a container will not work. Instead, you should connect to the other containers using their service names defined in the Docker Compose file as hostnames, or the internal Docker network IP addresses.
-
Firewall or Antivirus: Sometimes, your system's firewall or antivirus software can block connections. You might need to allow connections to the desired ports through the firewall.
-
Redis server not configured properly: Ensure that the Redis configurations are properly set to allow connections.
Based on these common issues, here are steps to troubleshoot and potentially solve the problem:
A. Check Running Containers
Make sure that all the Redis containers are up and running:
docker ps
B. Verify Networking
If you're running the redis-cli --cluster create ... command from outside a Docker container, make sure that Redis is configured to listen on 0.0.0.0 instead of 127.0.0.1 (the latter will be accessible only inside the individual container). You would need to update the command line options in the Docker Compose file (you might need to set --bind 0.0.0.0).
C. Connect Using Container Names
When setting up the Redis cluster using redis-cli, use the service names from the Docker Compose file as the hostnames.
# Use the container names instead of IP addresses
docker exec -it redis-1 redis-cli --cluster create redis-1:6379 redis-2:6380 redis-3:6381 --cluster-replicas 0
D. Disable Firewall/Antivirus Temporarily
Try temporarily disabling your firewall or antivirus software to see if the connection is getting blocked.
If you have verified these steps and the issue still persists, you might need to share more information about your setup and take a deeper look at the logs to diagnose the problem further. Check the log files of the Redis containers to see if there are any error messages:
docker logs [Container_Name]
Replace [Container_Name] with the name or ID of one of your Redis containers.