Home > front end >  Docker is taking wrong settings file when creating image
Docker is taking wrong settings file when creating image

Time:02-04

I have Django application where my settings are placed in folder named settings. Inside this folder I have init.py, base.py, deployment.py and production.py.

My wsgi.py looks like this:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp_settings.settings.production")


application = get_wsgi_application()

My Dockerfile:

FROM python:3.8

ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1

RUN mkdir /code
COPY . /code/
WORKDIR /code

RUN pip install --no-cache-dir git https://github.com/ByteInternet/pip-install-privates.git@master#egg=pip-install-privates

RUN pip install --upgrade pip

RUN pip_install_privates --token {GITHUB-TOKEN} /code/requirements.txt

RUN playwright install --with-deps chromium
RUN playwright install-deps

RUN touch /code/logs/celery.log
RUN chmod  x /code/logs/celery.log

EXPOSE 80

My docker-compose file:

version: '3'

services:
  app:
    container_name: myapp_django_app
    build:
      context: ./backend
      dockerfile: Dockerfile
    restart: always
    command: gunicorn myapp_settings.wsgi:application --bind 0.0.0.0:80
    networks:
      - myapp_default
    ports:
      - "80:80"
    env_file:
      - ./.env

Problem

Every time I create image Docker is taking settings from development.py instead of production.py. I tried to change my setting using this command:

set DJANGO_SETTINGS_MODULE=myapp_settings.settings.production

It works fine when using conda/venv and I am able to switch to production mode however when creating Docker image it does not take into consideration production.py file at all.

Question

Is there anything else I should be aware of that causes issues like this and how can I fix it?

CodePudding user response:

YES, there is something else you need to check:

When you run your docker container you can specify environment variables. If you declare environment variable DJANGO_SETTINGS_MODULE=myapp_settings.development it will override what you specified inside of wsgi.py!

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp_settings.settings.production")

code above basically means: declare "myapp_settings.settings.production" as the default but if environment variable DJANGO_SETTINGS_MODULE is declared, take the value of that variable.

Edit 1

Maybe you can try specifying the environment variable inside your docker-compose file:

version: '3'

services:
  app:
    environment:
      - DJANGO_SETTINGS_MODULE=myapp_settings.settings.production
    container_name: myapp_django_app
    build:
      context: ./backend
      dockerfile: Dockerfile
    restart: always
    command: gunicorn myapp_settings.wsgi:application --bind 0.0.0.0:80
    networks:
      - myapp_default
    ports:
      - "80:80"
    env_file:
      - ./.env
  • Related