title: How to set up a Docker redis container with ssl
body: <p>I'm following <a href="https://blog.miguelgrinberg.com/post/using-celery-with-flask" rel="noreferrer">this</a> tutorial.</p>
  
  <p>In my case I am operating in a Docker environment, and I have a secured site (i.e. <a href="https://localhost" rel="noreferrer">https://localhost</a>). which requires secured ssl communication.</p>
  
  <p>I adjusted the <em>web</em>, and <em>celery</em> containers for secure connection.<br>
  But I don't know how to configure the Redis container for secure connection with ssl<br>
  Note that when I run without ssl connection in the <em>web</em> and <em>celery</em> containers, the connection is fine.</p>
  
  <p><strong>How do I configure and run redis with ssl?</strong></p>
  
  <p>Thanks</p>
  
  <hr>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I followed <a href="https://redislabs.com/blog/stunnel-secure-redis-ssl/" rel="noreferrer">this</a> tutorial to set redis with ssl and <a href="https://github.com/madflojo/redis-tls-dockerfile" rel="noreferrer">this</a> tutorial to set redis with ssl via stunnel in Docker container.</p>
  
  <p>I successfully tested the connection from my localhost to the redis docker container, by invoking <code>redis-cli</code> from localhost (via stunnel) to the redis docker container, using the following call from the localhost:</p>
  
  <pre><code>redis-cli -h 127.0.0.1 -p 6381
  127.0.0.1:6381&gt; auth foobared
  OK
  127.0.0.1:6381&gt; 
  </code></pre>
  
  <p><strong>Related files on the redis server Docker side:</strong></p>
  
  <p><em>docker-compose file</em> (my webapp includes multiple services, but to for simplification I removed all services except for the redis container):</p>
  
  <pre><code>version: '3'
  
  services:
    redis:
      build:
        context: ./redis
        dockerfile: Dockerfile
      restart: always
      command: sh -c "stunnel /stunnel_take2.conf &amp;&amp; /usr/local/bin/redis-server /etc/redis/redis.conf"
      expose:
        - '6379'
      ports:
       - "6379:6379"
      volumes:
        - /home/avner/avner/certs:/etc/certs
        - /home/avner/avner/redis/conf:/etc/redis
  </code></pre>
  
  <p><em>redis container Dockerfile</em></p>
  
  <pre><code>FROM redis:5-alpine
  
  RUN apk add --no-cache \
      stunnel~=5.56 \
      python3~=3.8
  
  COPY stunnel-redis-server.conf /
  
  WORKDIR /
  
  ENV PYTHONUNBUFFERED=1
  </code></pre>
  
  <p>redis server redis conf file - <em>redis/conf/redis.conf</em></p>
  
  <pre><code>...
  requirepass foobared
  ...
  </code></pre>
  
  <p>redis server stunnel conf file - <em>redis/stunnel-redis-server.conf</em></p>
  
  <pre><code>cert = /etc/certs/private.pem
  pid = /var/run/stunnel.pid
  
  [redis]
  accept = 172.19.0.2:6380
  connect = 127.0.0.1:6379
  </code></pre>
  
  <p><strong>Related files on the client side (localhost):</strong></p>
  
  <p>redis client stunnel conf file - /etc/stunnel/redis-client.conf</p>
  
  <pre><code>cert = /etc/cert/private.pem
  client = yes
  pid = /var/run/stunnel.pid
  [redis]
  accept = 127.0.0.1:6381
  connect = 172.19.0.2:6380
  </code></pre>