Проблема Python+Redis в Docker-compose (except socket.error, e:)

Всем привет! Пишу небольшое приложение на Python 3.12.0 и использую Redis как брокер сообщений для Celery (БД=0). Так же использую Redis для хранения кеша (БД=1). При разработке, Redis запускал в docker. Все отлично работает. Теперь решил все упаковать в docker-compose. И тут начались проблемы... Мой Dockerfile:

FROM python:3.12.0

LABEL authors="agrytsai"

ENV PYTHONUNBUFFERED=1

RUN pip install poetry

WORKDIR /app

COPY pyproject.toml poetry.lock ./

RUN poetry install --no-dev --no-root

COPY . .

Docker-compose:

services:
  app:
    build:
      context: .
    env_file:
      - .env
    command: >
      sh -c "poetry run python manage.py wait_for_db &&
              poetry run python manage.py migrate &&
              poetry run celery -A MyProject worker --pool=solo --loglevel=INFO &&
              poetry run python main.py"
    depends_on:
      - redis

  redis:
    image: redis:alpine
    restart: on-failure
    env_file:
      - .env
    ports:
      - "6379:6379"
    volumes:
      - $REDISDATA
      - redis_data:/data
    command: redis-server /usr/local/etc/redis/redis_cache.conf

volumes:
  redis_data:

При попытке выполнить docker-compose up --build, Redis запускаеться нормально, python manage.py wait_for_db и python manage.py migrate тоже. А дальше получаю ошибку:

app-1    | Traceback (most recent call last):
app-1    |   File "/root/.cache/pypoetry/virtualenvs/prozorro-parsing-9TtSrW0h-py3.12/bin/celery", line 8, in <module>
app-1    |     sys.exit(main())                                                                                                                                                            
app-1    |              ^^^^^^                                                                                                                                                             
app-1    |   File "/root/.cache/pypoetry/virtualenvs/prozorro-parsing-9TtSrW0h-py3.12/lib/python3.12/site-packages/celery/__main__.py", line 15, in main                                   
app-1    |     sys.exit(_main())                                                                                                                                                           
app-1    |              ^^^^^^^                                                                                                                                                            
app-1    |   File "/root/.cache/pypoetry/virtualenvs/prozorro-parsing-9TtSrW0h-py3.12/lib/python3.12/site-packages/celery/bin/celery.py", line 236, in main                                
app-1    |     return celery(auto_envvar_prefix="CELERY")                                                                                                                                  
app-1    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                  
app-1    |   File "/root/.cache/pypoetry/virtualenvs/prozorro-parsing-9TtSrW0h-py3.12/lib/python3.12/site-packages/click/core.py", line 1157, in __call__                                  
app-1    |     return self.main(*args, **kwargs)                                                                                                                                           
app-1    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                           
app-1    |   File "/root/.cache/pypoetry/virtualenvs/prozorro-parsing-9TtSrW0h-py3.12/lib/python3.12/site-packages/click/core.py", line 1078, in main                                      
app-1    |     rv = self.invoke(ctx)                                                                                                                                                       
app-1    |          ^^^^^^^^^^^^^^^^                                                                                                                                                       
app-1    |   File "/root/.cache/pypoetry/virtualenvs/prozorro-parsing-9TtSrW0h-py3.12/lib/python3.12/site-packages/click/core.py", line 1688, in invoke                                    
app-1    |     return _process_result(sub_ctx.command.invoke(sub_ctx))                                                                                                                     
app-1    |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app-1    |   File "/root/.cache/pypoetry/virtualenvs/prozorro-parsing-9TtSrW0h-py3.12/lib/python3.12/site-packages/click/core.py", line 1434, in invoke                                    
app-1    |     return ctx.invoke(self.callback, **ctx.params)                                                                                                                              
app-1    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                              
app-1    |   File "/root/.cache/pypoetry/virtualenvs/prozorro-parsing-9TtSrW0h-py3.12/lib/python3.12/site-packages/click/core.py", line 783, in invoke                                     
app-1    |     return __callback(*args, **kwargs)                                                                                                                                          
app-1    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                          
app-1    |   File "/root/.cache/pypoetry/virtualenvs/prozorro-parsing-9TtSrW0h-py3.12/lib/python3.12/site-packages/click/decorators.py", line 33, in new_func                              
app-1    |     return f(get_current_context(), *args, **kwargs)                                                                                                                            
app-1    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                            
app-1    |   File "/root/.cache/pypoetry/virtualenvs/prozorro-parsing-9TtSrW0h-py3.12/lib/python3.12/site-packages/celery/bin/base.py", line 135, in caller                                
app-1    |     return f(ctx, *args, **kwargs)                                                                                                                                              
app-1    |            ^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                              
app-1    |   File "/root/.cache/pypoetry/virtualenvs/prozorro-parsing-9TtSrW0h-py3.12/lib/python3.12/site-packages/celery/bin/worker.py", line 348, in worker
app-1    |     worker = app.Worker(                                                                                                                                                        
app-1    |              ^^^^^^^^^^^                                                                                                                                                        
app-1    |   File "/root/.cache/pypoetry/virtualenvs/prozorro-parsing-9TtSrW0h-py3.12/lib/python3.12/site-packages/celery/worker/worker.py", line 93, in __init__                          
app-1    |     self.app.loader.init_worker()                                                                                                                                               
app-1    |   File "/root/.cache/pypoetry/virtualenvs/prozorro-parsing-9TtSrW0h-py3.12/lib/python3.12/site-packages/celery/loaders/base.py", line 110, in init_worker                       
app-1    |     self.import_default_modules()                                                                                                                                               
app-1    |   File "/root/.cache/pypoetry/virtualenvs/prozorro-parsing-9TtSrW0h-py3.12/lib/python3.12/site-packages/celery/loaders/base.py", line 104, in import_default_modules            
app-1    |     raise response                                                                                                                                                              
app-1    |   File "/root/.cache/pypoetry/virtualenvs/prozorro-parsing-9TtSrW0h-py3.12/lib/python3.12/site-packages/celery/utils/dispatch/signal.py", line 276, in send                     
app-1    |     response = receiver(signal=self, sender=sender, **named)                                                                                                                    
app-1    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                    
app-1    |   File "/root/.cache/pypoetry/virtualenvs/prozorro-parsing-9TtSrW0h-py3.12/lib/python3.12/site-packages/vine/promises.py", line 161, in __call__                                
app-1    |     return self.throw()                                                                                                                                                         
app-1    |            ^^^^^^^^^^^^
app-1    |   File "/root/.cache/pypoetry/virtualenvs/prozorro-parsing-9TtSrW0h-py3.12/lib/python3.12/site-packages/vine/promises.py", line 158, in __call__                                
app-1    |     retval = fun(*final_args, **final_kwargs)                                                                                                                                   
app-1    |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                   
app-1    |   File "/root/.cache/pypoetry/virtualenvs/prozorro-parsing-9TtSrW0h-py3.12/lib/python3.12/site-packages/celery/app/base.py", line 694, in _autodiscover_tasks                   
app-1    |     return self._autodiscover_tasks_from_fixups(related_name)                                                                                                                   
app-1    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                   
app-1    |   File "/root/.cache/pypoetry/virtualenvs/prozorro-parsing-9TtSrW0h-py3.12/lib/python3.12/site-packages/celery/app/base.py", line 703, in _autodiscover_tasks_from_fixups       
app-1    |     return self._autodiscover_tasks_from_names([                                                                                                                                
app-1    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                
app-1    |   File "/root/.cache/pypoetry/virtualenvs/prozorro-parsing-9TtSrW0h-py3.12/lib/python3.12/site-packages/celery/app/base.py", line 698, in _autodiscover_tasks_from_names        
app-1    |     return self.loader.autodiscover_tasks(                                                                                                                                      
app-1    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                      
app-1    |   File "/root/.cache/pypoetry/virtualenvs/prozorro-parsing-9TtSrW0h-py3.12/lib/python3.12/site-packages/celery/loaders/base.py", line 220, in autodiscover_tasks                
app-1    |     mod.__name__ for mod in autodiscover_tasks(packages or (),
app-1    |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                  
app-1    |   File "/root/.cache/pypoetry/virtualenvs/prozorro-parsing-9TtSrW0h-py3.12/lib/python3.12/site-packages/celery/loaders/base.py", line 246, in autodiscover_tasks                
app-1    |     return [find_related_module(pkg, related_name) for pkg in packages]                                                                                                         
app-1    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                              
app-1    |   File "/root/.cache/pypoetry/virtualenvs/prozorro-parsing-9TtSrW0h-py3.12/lib/python3.12/site-packages/celery/loaders/base.py", line 270, in find_related_module               
app-1    |     return importlib.import_module(module_name)                                                                                                                                 
app-1    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                 
app-1    |   File "/usr/local/lib/python3.12/importlib/__init__.py", line 90, in import_module                                                                                             
app-1    |     return _bootstrap._gcd_import(name[level:], package, level)                                                                                                                 
app-1    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                 
app-1    |   File "<frozen importlib._bootstrap>", line 1381, in _gcd_import                                                                                                               
app-1    |   File "<frozen importlib._bootstrap>", line 1354, in _find_and_load                                                                                                            
app-1    |   File "<frozen importlib._bootstrap>", line 1325, in _find_and_load_unlocked
app-1    |   File "<frozen importlib._bootstrap>", line 929, in _load_unlocked                                                                                                             
app-1    |   File "<frozen importlib._bootstrap_external>", line 994, in exec_module                                                                                                       
app-1    |   File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed                                                                                                  
app-1    |   File "/app/scraping/tasks.py", line 18, in <module>                                                                                                                           
app-1    |     from cache.redis import RedisService                                                                                                                                        
app-1    |   File "/app/cache/redis.py", line 5, in <module>                                                                                                                               
app-1    |     from redis import Redis                                                                                                                                                     
app-1    |   File "/root/.cache/pypoetry/virtualenvs/prozorro-parsing-9TtSrW0h-py3.12/lib/python3.12/site-packages/redis/__init__.py", line 2, in <module>                                 
app-1    |     from redis.client import Redis, ConnectionPool                                                                                                                              
app-1    |   File "/root/.cache/pypoetry/virtualenvs/prozorro-parsing-9TtSrW0h-py3.12/lib/python3.12/site-packages/redis/client.py", line 53
app-1    |     except socket.error, e:
app-1    |            ^^^^^^^^^^^^^^^
app-1    | SyntaxError: multiple exception types must be parenthesized

Ошибка говорит о несовместимости версий. У меня установлен redis = "^5.0.8", в документации указано:

**Note: ** redis-py 5.0 will be the last version of redis-py to support Python 3.7, as it has reached end of life. redis-py 5.1 will support Python 3.8+. Т.е. Python 3.12.0 + redis 5.0.8 полностю совместимы.

В чем может быть проблема? Уже почти неделю пытаюсь решить и все никак(


Ответы (1 шт):

Автор решения: Александр

Нашел решение - проблема была с виртуальным окружением. Удалил старое, создал новое, переустановил все пакеты и заработало.

→ Ссылка