I want to insert environmental variables from an .env file into my containerized Django application, so I can use it to securely set Django's settings.py.
However on $docker-compose up I receive part of an UserWarning which apparently originates in the django-environ package (and breaks my code):
/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(
The warning breaks at that point and (although all the container claim to be running) I can neither stop them from that console (zsh, Ctrl+C) nor can I access the website locally. What am I missing? Really appreciate any useful input.
Dockerfile: (located in root)
# 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 (located in root; I've tried either using env_file or environment as in the comments)
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. (also located in root)
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 protected]
EMAIL_HOST_PASSWORD=myMailPassword
TEMPLATE_DIR=frontend/templates/frontend/
Terminal Output after running $docker-compose up in the root
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
Let me know in case any further information is required.
Until now I do not know what caused the error, but in case anyone else has the same problem: switching to python-decouple instead of django-environ fixed it. Of course you have to adapt everything in settings.py accordingly, f.e. add
from decouple import config
andDEBUG = config('DEBUG', default=False, cast=bool)
.