【问题标题】:docker-compose up gets stuck at .env doesn't existdocker-compose up 卡在 .env 不存在
【发布时间】:2021-08-22 15:38:55
【问题描述】:

我想将 .env 文件中的环境变量插入到我的容器化 Django 应用程序中,这样我就可以使用它来安全地设置 Django 的 settings.py。

但是,在 $docker-compose up 上,我收到了部分 UserWarning,它显然源自 django-environ 包(并破坏了我的代码):

/usr/local/lib/python3.9/site-packages/environ/environ.py:628: UserWarning: /app/djangoDocker/.env 不存在 - 如果你没有单独配置你的环境,创建一个。网站 |警告.warn(

此时警告中断并且(尽管所有容器都声称正在运行)我既不能从该控制台(zsh、Ctrl+C)阻止它们,也不能在本地访问该网站。我错过了什么?非常感谢任何有用的意见。

Dockerfile:(位于根目录)

# pull official base image
FROM python:3.9.5

# set environment variables, grab via os.environ
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1

# set work directory
WORKDIR /app

# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt

# add entrypoint script
COPY ./entrypoint.sh ./app

# run entrypoint.sh
ENTRYPOINT ["./entrypoint.sh"]

# copy project
COPY . /app

docker-compose.yml(位于根目录;我尝试过使用 env_file 或 cmets 中的环境)

version: '3'

services:

    web:
      build: .
      container_name: web
      command: gunicorn djangoDocker.wsgi:application --bind 0.0.0.0:8000
      volumes:
        - .:/app
      ports:
        - "8000:80"
      env_file:
        - .env
      # environment:
      #   BASE_URL: ${BASE_URL}
      #   SECRET_KEY: ${SECRET_KEY}
      #   ALLOWED_HOSTS: ${ALLOWED_HOSTS}
      #   DEBUG: ${DEBUG}
      #   SQL_ENGINE: ${SQL_ENGINE}
      #   SQL_DATABASE: ${SQL_DATABASE}
      #   SQL_USER: ${SQL_USER}
      #   SQL_PASSWORD: ${SQL_PASSWORD}
      #   SQL_HOST: ${SQL_HOST}
      #   SQL_PORT: ${SQL_PORT}
      #   EMAIL_HOST_USER: ${EMAIL_HOST_USER}
      #   EMAIL_HOST_PASSWORD: ${EMAIL_HOST_PASSWORD}
      #   TEMPLATE_DIR: ${TEMPLATE_DIR}
      depends_on:
        - pgdb

    pgdb:
      image: postgres
      container_name: pgdb
      environment:
        - POSTGRES_DB=postgres
        - POSTGRES_USER=postgres
        - POSTGRES_PASSWORD=postgres
      volumes:
      - pgdata:/var/lib/postgresql/data/

    nginx:
      build: ./nginx
      volumes: 
        - .:/app
      links:
        - web:web
      ports: 
        - "80:80"
      depends_on:
        - web
volumes:
    pgdata:

.env。 (也位于根目录)

BASE_URL=localhost
SECRET_KEY=mySecretKey
ALLOWED_HOSTS=localhost,127.0.0.1,0.0.0.0
DEBUG=True

SQL_ENGINE=django.db.backends.postgresql
SQL_DATABASE=postgres
SQL_USER=postgres
SQL_PASSWORD=postgres
SQL_HOST=pgdb
SQL_PORT=5432

EMAIL_HOST_USER=my@mail.com
EMAIL_HOST_PASSWORD=myMailPassword

TEMPLATE_DIR=frontend/templates/frontend/

终端输出在根目录下运行 $docker-compose up

pgdb is up-to-date
Recreating web ... done
Recreating djangodocker_nginx_1 ... done
Attaching to pgdb, web, djangodocker_nginx_1
nginx_1  | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx_1  | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
nginx_1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
nginx_1  | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist
web      | [2021-06-04 14:58:09 +0000] [1] [INFO] Starting gunicorn 20.0.4
nginx_1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
web      | [2021-06-04 14:58:09 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
web      | [2021-06-04 14:58:09 +0000] [1] [INFO] Using worker: sync
web      | [2021-06-04 14:58:09 +0000] [8] [INFO] Booting worker with pid: 8
nginx_1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
nginx_1  | /docker-entrypoint.sh: Configuration complete; ready for start up
nginx_1  | 2021/06/04 14:58:09 [notice] 1#1: using the "epoll" event method
nginx_1  | 2021/06/04 14:58:09 [notice] 1#1: nginx/1.20.1
nginx_1  | 2021/06/04 14:58:09 [notice] 1#1: built by gcc 10.2.1 20201203 (Alpine 10.2.1_pre1) 
nginx_1  | 2021/06/04 14:58:09 [notice] 1#1: OS: Linux 4.19.121-linuxkit
nginx_1  | 2021/06/04 14:58:09 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
nginx_1  | 2021/06/04 14:58:09 [notice] 1#1: start worker processes
nginx_1  | 2021/06/04 14:58:09 [notice] 1#1: start worker process 23
nginx_1  | 2021/06/04 14:58:09 [notice] 1#1: start worker process 24
nginx_1  | 2021/06/04 14:58:09 [notice] 1#1: start worker process 25
nginx_1  | 2021/06/04 14:58:09 [notice] 1#1: start worker process 26
pgdb     | 
pgdb     | PostgreSQL Database directory appears to contain a database; Skipping initialization
pgdb     | 
pgdb     | 2021-06-04 14:34:00.119 UTC [1] LOG:  starting PostgreSQL 13.3 (Debian 13.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
pgdb     | 2021-06-04 14:34:00.120 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
pgdb     | 2021-06-04 14:34:00.120 UTC [1] LOG:  listening on IPv6 address "::", port 5432
pgdb     | 2021-06-04 14:34:00.125 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
pgdb     | 2021-06-04 14:34:00.134 UTC [27] LOG:  database system was shut down at 2021-06-04 14:21:19 UTC
pgdb     | 2021-06-04 14:34:00.151 UTC [1] LOG:  database system is ready to accept connections
web      | /usr/local/lib/python3.9/site-packages/environ/environ.py:628: UserWarning: /app/djangoDocker/.env doesn't exist - if you're not configuring your environment separately, create one.
web      |   warnings.warn(

requirements.txt

Django==3.2
gunicorn==20.0.4
djoser==2.1.0
django-environ

psycopg2-binary~=2.8.0
django-cors-headers==3.5.0
django-templated-mail==1.1.1
djangorestframework==3.12.2
djangorestframework-simplejwt==4.7.0

如果需要任何进一步的信息,请告诉我。

【问题讨论】:

  • docker-compose.env 在同一个目录中?
  • @Jeet Patel 是的先生,两者都在项目的根目录中
  • 重命名您的文件.en.dev 可能会起作用。
  • 感谢 Jeet 的建议,不幸的是它没有帮助。如果我只更改文件名,它仍在寻找 .env ,因此会引发一个(不同的)错误,即找不到 .env 。如果我也更改了 env_file 中的文件名,则会发生与我的问题完全相同的错误(即使在重建所有内容之后,仍然包含 .env)

标签: django docker docker-compose environment-variables environ


【解决方案1】:

直到现在我还不知道是什么导致了错误,但如果其他人有同样的问题:切换到 python-decouple 而不是 django-environ 修复它。当然,您必须相应地调整 settings.py 中的所有内容,例如添加from decouple import configDEBUG = config('DEBUG', default=False, cast=bool)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-18
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2019-07-22
    • 1970-01-01
    相关资源
    最近更新 更多