3

I am trying to connect redis container to python app container using environment variable. I passed password as an environment variable but it is not connecting, if I don't use an environment variable and hard code the password it works fine otherwise it gives redis.exceptions.ConnectionError

version: "3.7"
services:
  nginx_app:
    image: nginx:latest
    depends_on:
      - flask_app
    volumes:
      - ./default.conf:/etc/nginx/conf.d/default.conf
    ports:
      - 8090:80
    networks:
      - my_project_network

  flask_app:
    build:
      context: .
      dockerfile: Dockerfile
    expose:
      - 5000
    environment:
      - PASSWORD=pass123a
    depends_on:
      - redis_app
    networks:
      - my_project_network

  redis_app:
    image: redis:latest
    command: redis-server --requirepass ${PASSWORD} --appendonly yes
    environment:
      - PASSWORD=pass123a
    volumes:
      - ./redis-vol:/data 
    expose:
      - 6379
    networks:
      - my_project_network
networks:
  my_project_network:

index.py

from flask import Flask
from redis import Redis
import os
app = Flask(__name__)
redis = Redis(host='redis_app', port=6379, password=os.getenv('PASSWORD'))
@app.route('/')
def hello():
    redis.incr('hits')
    return 'Hello World! I have been seen %s times.' % redis.get('hits')
if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True)

2 Answers2

4

Update your docker-compose.yaml

the environment is a list of strings:

docker-composer interpolates ${ENV} where the value of ENV is loaded from .env file

Use:

command: redis-server --requirepass $PASSWORD --appendonly yes

Instead of:

command: redis-server --requirepass ${PASSWORD} --appendonly yes

You can verify environment variable inside ur container by:

docker-compose run --rm flask_app printenv | grep PASSWORD

That should return:

PASSWORD=pass123a

docker-compose example for environment variables: Here

WSMathias9
  • 669
  • 8
  • 15
  • I have already added it in docker-compose file. Its not working –  Jul 19 '20 at 20:51
  • I have updated my answer, environment is a list of strings you are writing it wrong. – WSMathias9 Jul 20 '20 at 01:29
  • I have made the changes its returns PASSWORD=wahaj123a but when I go to the local host it still shows `redis.exceptions.ConnectionError: Error -2 connecting to redis_app:6379. Name or service not known.` I have updated my question to the current docker-compose file with the changes that you suggested still having the same error –  Jul 20 '20 at 05:55
  • When I change this `--requirepass wahaj123a` to `--requirepass ${PASSWORD}` then it gives an error `redis.exceptions.ConnectionError: Error -2 connecting to redis_app:6379. Name or service not known.` –  Jul 20 '20 at 06:15
  • 1
    create .env file in the same folder and add this line PASSWORD=wahaj123a then try `docker-compose down && docker-compose up` – WSMathias9 Jul 20 '20 at 06:52
  • do I have to do any changes to the python file too? as still there is a connection error. --requirepass ${PASSWORD} works fine now but when I pass the env variable to python it gives the same error. hard coding the password there works fine –  Jul 20 '20 at 07:09
  • Inshort how to give python script access to read the `PASSWORD` from .env file –  Jul 20 '20 at 07:10
  • 1
    this should help you: https://stackoverflow.com/questions/4906977/how-to-access-environment-variable-values – WSMathias9 Jul 20 '20 at 07:26
0

Looks like you have missed passing the environment variable to your Redis container.

Try This:

version: "3.7"
services:
  nginx_app:
    image: nginx:latest 
    #LOCAL IMAGE
    depends_on:
      - flask_app
    volumes:
      - ./default.conf:/etc/nginx/conf.d/default.conf
    ports:
      - 8082:80
    networks:
      - my_project_network

  flask_app:
    build:
      context: .
      dockerfile: Dockerfile
    expose:
      - 5000
    environment:
      - PASSWORD=pass123a
    depends_on:
      - redis_app
    networks:
      - my_project_network

  redis_app:
    image: redis:latest
    command: redis-server --requirepass ${PASSWORD} --appendonly yes
    environment:
      - PASSWORD=pass123a
    volumes:
      - ./redis-vol:/data 
    expose:
      - 6379
    networks:
      - my_project_network
networks:
  my_project_network:
Suhas NM
  • 960
  • 7
  • 10
  • I have made the changes and updated my question but when I go to the local host it still shows `redis.exceptions.ConnectionError: Error -2 connecting to redis_app:6379. Name or service not known.` –  Jul 20 '20 at 05:55
  • When I change this `--requirepass wahaj123a` to `--requirepass ${PASSWORD}` then it gives an error `redis.exceptions.ConnectionError: Error -2 connecting to redis_app:6379. Name or service not known.` –  Jul 20 '20 at 06:15
  • 1
    Check if the container is running. It may have closed with an error – Suhas NM Jul 20 '20 at 07:12