user11992940
user11992940

Reputation:

Connecting to Redis cluster (in Docker) with Python

I'm setting up a highly available Redis cluster (with Sentinels), and need to write data to it in Python. However, I don't know which hostname to use when connecting from Python, and I am also receiving "Connection refused".

My Dockerfile:

FROM redis:latest
ADD sentinel.conf /etc/redis/sentinel.conf
RUN chown redis:redis /etc/redis/sentinel.conf
ENV SENTINEL_QUORUM 2
RUN mkdir app
WORKDIR /app
ENV SENTINEL_DOWN_AFTER 5000
ENV SENTINEL_FAILOVER 10000
ENV SENTINEL_PORT 26000
ADD entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 26379 6379

FROM python:2.7-slim
WORKDIR /app
COPY . /app
RUN pip install --trusted-host pypi.python.org -r requirements.txt
CMD ["python", "app.py"]

docker-compose.yml

ervices:

  redis-master:
    container_name: redis-master
    image: redis:latest
    command: redis-server --port 6379
    ports:
      - "6379:6379"
    volumes:
      - .:/app

  redis-slave:
    container_name: redis-slave
    image: redis:latest
    command: redis-server --slaveof redis-master 6379 --protected-mode no
    volumes:
       - .:/app

  sentinel-1:
    container_name: sentinel-1
    build: sentinel
    environment:
      - SENTINEL_DOWN_AFTER=5000
      - SENTINEL_FAILOVER=5000

  sentinel-2:
    container_name: sentinel-2
    build: sentinel
    environment:
      - SENTINEL_DOWN_AFTER=5000
      - SENTINEL_FAILOVER=5000

  sentinel-3:
    container_name: sentinel-3
    build: sentinel
    environment:
      - SENTINEL_DOWN_AFTER=5000
      - SENTINEL_FAILOVER=5000

Python code I'm trying to run:

r = redis.StrictRedis(host='0.0.0.0', port=6379, db=0)
print ("set key1 123")
print (r.set('key1', '123'))
print ("get key1")
print(r.get('key1'))

Error:

Traceback (most recent call last):
  File "app.py", line 7, in <module>
    print (r.set('key1', '123'))
  File "/usr/local/lib/python2.7/site-packages/redis/client.py", line 1519, in set
    return self.execute_command('SET', *pieces)
  File "/usr/local/lib/python2.7/site-packages/redis/client.py", line 836, in execute_command
    conn = self.connection or pool.get_connection(command_name, **options)
  File "/usr/local/lib/python2.7/site-packages/redis/connection.py", line 1071, in get_connection
    connection.connect()
  File "/usr/local/lib/python2.7/site-packages/redis/connection.py", line 543, in connect
    raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error 111 connecting to 0.0.0.0:6379. Connection refused.

I've tried changing host to redis, redis-master, actual Docker IP, some where not found, and some still connection refused.

I've created a network, but then Sentinels couldn't communicate to one another.

I've also tried starting Redis master with configuration file, but error was that it couldn't be found. I did add WORKDIR and COPY to Dockerfile for this purpose.

How can I connect to Redis from Python (also inside Docker)?

Thank you for your time, any advice is appreciated.

Upvotes: 3

Views: 3224

Answers (1)

LinPy
LinPy

Reputation: 18608

to connect from python which is in container from the same compose file:

r = redis.StrictRedis(host='redis-master', port=6379, db=0)

so you need to use the service name

if the Python Container is not from the same compose file, you nned to connect it using the network from the redis compose and declare it as external in your python compose file

Example with docker run:

docker run -itd --network=MyNetworkFromTheOtherCompose busybox

Example with compose:

networks:
  default:
    external:
      name: my-pre-existing-network-FromTheOtherCompose

Upvotes: 1

Related Questions