Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask redis server session: Error in save_session

Folks: I am moving my Flask app with redis for session storage into docker containers. My app runs happily when redis is run locally. However, when running a docker-compose with a redis image, I see the following error. I get an identical error whether my redis container is running or stopped:

2017-04-02 03:36:09,861] ERROR in app: Exception on / [GET]
web_1    | Traceback (most recent call last):
web_1    |   File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1988, in wsgi_app
web_1    |     response = self.full_dispatch_request()
web_1    |   File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1643, in full_dispatch_request
web_1    |     response = self.process_response(response)
web_1    |   File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1864, in process_response
web_1    |     self.save_session(ctx.session, response)
web_1    |   File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 926, in save_session
web_1    |     return self.session_interface.save_session(self, session, response)
web_1    |   File "/usr/local/lib/python2.7/site-packages/flask_session/sessions.py", line 165, in save_session
web_1    |     self.redis.setex(name=self.key_prefix + session.sid, value=val,
web_1    | AttributeError: 'str' object has no attribute 'setex'

I have added the following configuration properties to my app for session config:

SESSION_TYPE = "redis"
SESSION_REDIS = "redis:6379"

I don't think this is a docker-compose issue as my app is successfully able to connect to the MySQL instance, still, here is my docker-compose.yml file just in case it points to something:

version: "3"
services:
  mysql:
    build:
      context: db/
  web:
    build:
      context: web/
    ports:
    - "10080:80"
    depends_on:
    - "mysql"
    - "redis"
  redis:
    image: "redis:alpine"
like image 995
Raj Avatar asked Apr 02 '17 04:04

Raj


1 Answers

Figured it out. The issue was in session config. I had to change

SESSION_REDIS = "redis:6379"

to

SESSION_REDIS = redis.Redis("redis")
like image 87
Raj Avatar answered Nov 10 '22 04:11

Raj