Почему при запуске алембика через докер ошибка переменных окружения?

При запуске вручную alembic upgrade head все ок:
INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.


При запуске через докер:

> [web 7/7] RUN poetry run alembic upgrade head:                                                                                                                                                    
0.712 Traceback (most recent call last):                                                                                                                                                             
0.712   File "/root/.cache/pypoetry/virtualenvs/ai-omni-9TtSrW0h-py3.12/bin/alembic", line 8, in <module>                                                                                            
0.712     sys.exit(main())                                                                                                                                                                           
0.712              ^^^^^^
0.712   File "/root/.cache/pypoetry/virtualenvs/ai-omni-9TtSrW0h-py3.12/lib/python3.12/site-packages/alembic/config.py", line 641, in main
0.713     CommandLine(prog=prog).main(argv=argv)
0.713   File "/root/.cache/pypoetry/virtualenvs/ai-omni-9TtSrW0h-py3.12/lib/python3.12/site-packages/alembic/config.py", line 631, in main
0.713     self.run_cmd(cfg, options)
0.713   File "/root/.cache/pypoetry/virtualenvs/ai-omni-9TtSrW0h-py3.12/lib/python3.12/site-packages/alembic/config.py", line 608, in run_cmd
0.713     fn(
0.713   File "/root/.cache/pypoetry/virtualenvs/ai-omni-9TtSrW0h-py3.12/lib/python3.12/site-packages/alembic/command.py", line 403, in upgrade
0.713     script.run_env()
0.713   File "/root/.cache/pypoetry/virtualenvs/ai-omni-9TtSrW0h-py3.12/lib/python3.12/site-packages/alembic/script/base.py", line 583, in run_env
0.713     util.load_python_file(self.dir, "env.py")
0.714   File "/root/.cache/pypoetry/virtualenvs/ai-omni-9TtSrW0h-py3.12/lib/python3.12/site-packages/alembic/util/pyfiles.py", line 95, in load_python_file
0.714     module = load_module_py(module_id, path)
0.714              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
0.714   File "/root/.cache/pypoetry/virtualenvs/ai-omni-9TtSrW0h-py3.12/lib/python3.12/site-packages/alembic/util/pyfiles.py", line 113, in load_module_py
0.714     spec.loader.exec_module(module)  # type: ignore
0.714     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
0.714   File "<frozen importlib._bootstrap_external>", line 995, in exec_module
0.714   File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
0.714   File "/app/core/db/migrations/env.py", line 8, in <module>
0.714     from utils.config import settings
0.714   File "/app/core/utils/config.py", line 12, in <module>
0.714     settings = Settings()
0.714                ^^^^^^^^^^
0.714   File "/root/.cache/pypoetry/virtualenvs/ai-omni-9TtSrW0h-py3.12/lib/python3.12/site-packages/pydantic_settings/main.py", line 140, in __init__
0.714     super().__init__(
0.714   File "/root/.cache/pypoetry/virtualenvs/ai-omni-9TtSrW0h-py3.12/lib/python3.12/site-packages/pydantic/main.py", line 176, in __init__
0.714     self.__pydantic_validator__.validate_python(data, self_instance=self)
0.714 pydantic_core._pydantic_core.ValidationError: 5 validation errors for Settings
0.714 tg_token
0.714   Field required [type=missing, input_value={}, input_type=dict]
0.714     For further information visit https://errors.pydantic.dev/2.7/v/missing
0.714 tg_ids
0.714   Field required [type=missing, input_value={}, input_type=dict]
0.714     For further information visit https://errors.pydantic.dev/2.7/v/missing
0.714 ai_token
0.714   Field required [type=missing, input_value={}, input_type=dict]
0.714     For further information visit https://errors.pydantic.dev/2.7/v/missing
0.714 ai_base_url
0.714   Field required [type=missing, input_value={}, input_type=dict]
0.714     For further information visit https://errors.pydantic.dev/2.7/v/missing
0.714 postgres_url
0.714   Field required [type=missing, input_value={}, input_type=dict]
0.714     For further information visit https://errors.pydantic.dev/2.7/v/missing
------
failed to solve: process "/bin/sh -c poetry run alembic upgrade head" did not complete successfully: exit code: 1


То есть я так понимаю в самом докере проблема, потому что локально переменные окружения работают корректно. Все хранится в .env и импортятся из класса pydantic.

Вот докер файл:

FROM python:3.12-slim
RUN apt-get update && apt-get install -y \
    build-essential \
    libpq-dev \
    && rm -rf /var/lib/apt/lists/*
RUN pip install poetry
WORKDIR /app
COPY . .
RUN poetry install
RUN poetry run alembic upgrade head

Вот компот:

version: '3.8'

services:
  db:
    image: postgres:latest
    restart: always
    env_file:
      - .env
    volumes:
      - postgres_data:/var/lib/postgresql/data

  web:
    build: .
    depends_on:
      - db
    env_file:
      - .env
    ports:
      - "8000:8000"

volumes:
  postgres_data:

Если нужна доп инфа, пишите. Еще кода скину.
  • Вопрос задан
  • 171 просмотр
Пригласить эксперта
Ответы на вопрос 1
@va_k
Судя по логу, падает на чтении настроек. Наверно в докер образе их нет.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы