My Environment
docker 17.12-ce
python 3.6.3
django 1.10.8
I have a django application that I want to containerise.
Trying to maintain best practice I have followed the advice to split the settings.py file into a base file and then a file per stage
so my base.py file where it loads the secret settings looks like this
# Settings imported from a json file
with open(os.environ.get('SECRET_CONFIG')) as f:
configs = json.loads(f.read())
def get_secret(setting, configs=configs):
try:
val = configs[setting]
if val == 'True':
val = True
elif val == 'False':
val = False
return val
except KeyError:
error_msg = "ImproperlyConfigured: Set {0} environment variable".format(setting)
raise ImproperlyConfigured(error_msg)
And it gets the file path from the SECRET_CONFIG environment variable.
This works well when running the application locally without docker.
I have created a dockerfile that uses the python3 onbuild image.
My Dockerfile looks like this
# Dockerfile
# FROM directive instructing base image to build upon
FROM python:3.6.4-onbuild
MAINTAINER Lance Haig
RUN mkdir media static logs
VOLUME ["$WORKDIR/logs/"]
# COPY startup script into known file location in container
COPY docker-entrypoint.sh /docker-entrypoint.sh
# EXPOSE port 8000 to allow communication to/from server
EXPOSE 8000
# CMD specifcies the command to execute to start the server running.
CMD ["/docker-entrypoint.sh"]
# done!
The dockder-entrypoint.sh file looks like this
#!/bin/bash
python manage.py migrate # Apply database migrations
python manage.py collectstatic --noinput # Collect static files
# Prepare log files and start outputting logs to stdout
touch /usr/src/app/logs/gunicorn.log
touch /usr/src/app/logs/access.log
tail -n 0 -f /usr/src/app/logs/*.log &
export DJANGO_SETTINGS_MODULE=django-app.settings.development
# Start Gunicorn processes
echo Starting Gunicorn.
# exec gunicorn django-app.wsgi:application --bind 0.0.0.0:8000 --workers 3
exec gunicorn django-app.wsgi:application \
--name sandbox_django \
--bind 0.0.0.0:8000 \
--workers 3 \
--log-level=info \
--log-file=/usr/src/app/logs/gunicorn.log \
--access-logfile=/usr/src/app/logs/access.log \
"$@"
I have tried setting the environment variable SECRET_CONFIG when I start the container using this command
docker run -e SECRET_CONFIG=/home/stokvis/dev/app/secrets.json --name django-app-test -it django-app:latest
but it seems that docker will not want to load the variable.
is there a better way to provide the secrets to an image if it is to be run on a docker host or a kubernetes cluster?
Have I missed something basic?