docker-compose up 卡在 .env 不存在



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

然而,在$docker compose上,我收到了一个UserWarning的一部分,它显然源于django environment包(并破坏了我的代码(:

//usr/local/lib/python3.9/site-packages/environment/py.628:UserWarning:/app/djanoDocker/.env不存在-如果您没有单独配置环境,请创建一个。web|warnings.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或注释中的environment(

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(

需求.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

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

到目前为止,我还不知道是什么导致了错误,但如果其他人也有同样的问题:切换到python解耦而不是django environment修复了它。当然,你必须相应地调整设置中的所有内容。例如,添加from decouple import configDEBUG = config('DEBUG', default=False, cast=bool)

相关内容

最新更新