0

I am installed django-environ and try to get my values from .env. I haven't problems when I run server in local, but when i run my app with docker-compose up --build I have error:

Traceback (most recent call last):
  File "/usr/bin/docker-compose", line 33, in <module>
    sys.exit(load_entry_point('docker-compose==1.29.2', 'console_scripts', 'docker-compose')())
  File "/usr/lib/python3/dist-packages/compose/cli/main.py", line 81, in main
    command_func()
  File "/usr/lib/python3/dist-packages/compose/cli/main.py", line 200, in perform_command
    project = project_from_options('.', options)
  File "/usr/lib/python3/dist-packages/compose/cli/command.py", line 60, in project_from_options
    return get_project(
  File "/usr/lib/python3/dist-packages/compose/cli/command.py", line 148, in get_project
    config_data = config.load(config_details, interpolate)
  File "/usr/lib/python3/dist-packages/compose/config/config.py", line 442, in load
    service_dicts = load_services(config_details, main_file, interpolate=interpolate)
  File "/usr/lib/python3/dist-packages/compose/config/config.py", line 558, in load_services
    return build_services(service_config)
  File "/usr/lib/python3/dist-packages/compose/config/config.py", line 537, in build_services
    return sort_service_dicts([
  File "/usr/lib/python3/dist-packages/compose/config/config.py", line 538, in <listcomp>
    build_service(name, service_dict, service_names)
  File "/usr/lib/python3/dist-packages/compose/config/config.py", line 526, in build_service
    service_dict = finalize_service(
  File "/usr/lib/python3/dist-packages/compose/config/config.py", line 947, in finalize_service
    service_dict['environment'] = resolve_environment(service_dict, environment, interpolate)
  File "/usr/lib/python3/dist-packages/compose/config/config.py", line 732, in resolve_environment
    env.update(env_vars_from_file(env_file, interpolate))
  File "/usr/lib/python3/dist-packages/compose/config/environment.py", line 38, in env_vars_from_file
    env = dotenv.dotenv_values(dotenv_path=filename, encoding='utf-8-sig', interpolate=interpolate)
AttributeError: module 'dotenv' has no attribute 'dotenv_values'

I have my .env file:

SECRET_KEY="django-insecure-t+)^5*1syzvs=p%tiy324)zxz26$ra!+__8)y8=!hnyjn_cg3m"

My docker-compose:

version: "3.10"
services:
  postgres:
    image: postgres:13.3
    env_file:
      - ./support/.env
    volumes:
      - .:/docker-entrypoint-initdb.d
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U alexey_razmanov -d support_app_db"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s
    restart: unless-stopped
    deploy:
      resources:
        limits:
          cpus: '1'
          memory: 4G
  web:
    build: .
    command: bash -c "make full-migrate-and-run"
    ports:
      - "8000:8000"
    env_file:
      - ./support/.env
    volumes:
      - .:/support
    depends_on:
      - postgres
      - redis
  redis:
    image: redis:alpine
    ports:
      - "6379:6379"
  celery:
    restart: always
    build:
      context: .
    command: bash -c "cd support && celery -A support worker -l info"
    env_file:
      - ./support/.env
    depends_on:
      - postgres
      - redis
      - web

My Dockerfile:

FROM python:3.10 as prod

ENV PYTHONUNBUFFERED=1 \
    POETRY_HOME=/opt/poetry \
    POETRY_VIRTUALENVS_CREATE="false"

ENV PATH="${POETRY_HOME}/bin:$PATH"

WORKDIR /app

RUN apt update && \
    apt install make

COPY Makefile poetry.lock pyproject.toml /app/

RUN curl -sSL https://install.python-poetry.org | python3 -

RUN make install-prod

COPY support /app/support

CMD make make-migrations && make migrate && make run-app

In settings.py:

 import environ
    
 env = environ.Env(DEBUG=(bool, False))
    
 BASE_DIR = Path(__file__).resolve().parent.parent
 environ.Env.read_env(os.path.join(BASE_DIR, ".env"))
 SECRET_KEY = env("SECRET_KEY")

I am tried use packege python-dotenv, poetry-dotenv, dotenv, load_env and all give me this error.

1 Answers1

0

I have the same issue with you when I have the following conditions

  • I'm using Arch Linux
  • I have pyenv usng mutiple version of Python
  • I have docker-compose installed using pip from different version of python

Solution

  • Uninstall all docker-compose install via pip in all Python version, then install docker-compose that is provided by AUR in my case yay -S docker-compose. Tada! now it works.
abmap
  • 141
  • 5