Im trying to dockerize an existing django project.
I have corsheaders installed
and its included in my installed apps as follows:
INSTALLED_APPS = [
'corsheaders',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
and in the middleware
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
after that i build my docker using
docker-compose --build
and then run it
docker-compose up
but always the same error
ModuleNotFoundError: No module named 'corsheaders'
i have tried to delete corsheaders from my installed aps but then i get
ModuleNotFoundError: No module named 'restframework'
ps: if i run the server through py manage.py runserver
it works perfectly.
Dockerfile
# syntax=docker/dockerfile:1
FROM python:3
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
Requirements.txt
Django>=4.0
psycopg2>=2.8
django-cors-headers==3.10.1
djangorestframework==3.13.1
pandas
2
Answers
I solved it by realizing the when i install the package through :
its not getting installed in the venv of the project itself. or through:
So i copied it to the venv and it worked.
you need to add restframework and pass the cors at the end