Search code examples
dockerflaskredisdocker-network

How to setup redis as cache with flask-session when working with containers?


I'm trying to deploy a flaskapp with docker, so I have two containers, one for my flaskapp and another for redis. I've setup a network and executed both within it. When accessing my flaskapp the following error occurs:

ConnectionError: Error 99 connecting to localhost:6379. Cannot assign requested address.

This is happening because I'm using redis as cache for flask-session and I'm hosting redis at a container called exampleredis.

My question is: how can I setup redis host to be exampleredis in Flask?

I have the following config file:

class DevConfig(Config):
    REDIS_HOST = 'exampleredis'
    SESSION_TYPE = 'redis'
    JSONIFY_PRETTYPRINT_REGULAR = False
    JSON_SORT_KEYS = False
    BCRYPT_LOG_ROUNDS = 15
    DEBUG = True

Solution

  • According to the document of flask-session, this should probably work:

    import redis
    
    class DevConfig(Config):
        SESSION_REDIS = redis.from_url('redis://exampleredis:6379')
        SESSION_TYPE = 'redis'
        ...