[英]Docker env_file not working when running build
我有以下docker-compose.yml
:
version: '3'
services:
website:
build:
context: website
env_file: website/config/production.env
website
服務對應這個website/Dockerfile
:
FROM python:2.7
RUN apt-get update
COPY src /usr/website
WORKDIR /usr/website
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
RUN python manage.py migrate
我也有website/config/production.env
,有幾個設置,如下所示:
DJANGO_SECRET_KEY=
DJANGO_DATABASE_NAME=
DJANGO_DATABASE_USER=
DJANGO_DATABASE_PASSWORD=
DJANGO_DATABASE_HOST=
DJANGO_DATABASE_PORT=3306
如果我運行docker-compose config
,變量在environment
鍵下正確顯示,但是當我運行docker-compose build
我得到
django.core.exceptions.ImproperlyConfigured:DJANGO_SECRET_KEY 不在您的環境中
那是因為我的settings.py
文件中有這個:
def require_environ(key):
if key in os.environ:
return os.environ.get(key)
raise ImproperlyConfigured('%s is not in your environment' % (key,))
因此,代碼正常工作,但未定義變量。 為什么不?
正如@David Maze 在上面的評論中所說,您不能從 docker 文件運行遷移。 環境變量在構建階段不可用,因此必須/應該在之后運行遷移。
為了解決這個問題,我創建了一個腳本start.sh
,如下:
#!/bin/bash
set -e
# Django: migrate
#
# Django will see that the tables for the initial migrations already exist
# and mark them as applied without running them. (Django won’t check that the
# table schema match your models, just that the right table names exist).
python manage.py migrate --fake-initial
# Django: collectstatic
#
# This will upload the files to s3 because of django-storages-redux
# and the setting:
# STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
python manage.py collectstatic --noinput
#####
# Start uWSGI
#####
/usr/local/bin/uwsgi --emperor /etc/uwsgi/django-uwsgi.ini
該腳本作為Dockerfile
的最后一行運行:
CMD [ "/usr/start.sh" ]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.