Move my Django application + Apache to Docker

I am trying to migrate my test app to Docker, but I am always getting the same error, despite of trying many approaches.

This is my Dockerfile:

FROM python:3.10-slim-buster

RUN apt-get update && apt-get install -y apache2 libapache2-mod-wsgi-py3
WORKDIR /app
COPY requirements.txt /app/
RUN pip install -r requirements.txt
COPY morabusa /app/
COPY app.conf /etc/apache2/sites-available/app.conf
RUN a2dissite 000-default.conf && a2ensite app.conf
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

This is the app.conf for Apache vHost, which I have simplified a lot, in order to try to find the issue:

<VirtualHost *:80>

    ServerName morabusa.com

    ErrorLog /app/morabusaprj/logs/error.log                                                   
    CustomLog /app/morabusaprj/logs/access.log combine

    <Directory /app>                                                                                               
            Require all granted
    </Directory>

    WSGIScriptAlias / /app/morabusaprj/wsgi.py

</VirtualHost>

This is my wsgi.py file configuration (I have added the import sys, but it still fails):

import os
import sys

sys.path.append('/app')
sys.path.append('/app/morabusaprj')

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'morabusaprj.settings')

application = get_wsgi_application()

Traceback:

[Wed Feb 08 17:57:56.488523 2023] [wsgi:error] [pid 9:tid 140315142702848] [client 192.168.1.33:60062] mod_wsgi (pid=9): Failed to exec Python script file '/app/morabusaprj/wsgi.py'.
[Wed Feb 08 17:57:56.488574 2023] [wsgi:error] [pid 9:tid 140315142702848] [client 192.168.1.33:60062] mod_wsgi (pid=9): Exception occurred processing WSGI script '/app/morabusaprj/wsgi.py'.
[Wed Feb 08 17:57:56.488622 2023] [wsgi:error] [pid 9:tid 140315142702848] [client 192.168.1.33:60062] Traceback (most recent call last):
[Wed Feb 08 17:57:56.488633 2023] [wsgi:error] [pid 9:tid 140315142702848] [client 192.168.1.33:60062]   File "/app/morabusaprj/wsgi.py", line 17, in <module>
[Wed Feb 08 17:57:56.488635 2023] [wsgi:error] [pid 9:tid 140315142702848] [client 192.168.1.33:60062]     from django.core.wsgi import get_wsgi_application
[Wed Feb 08 17:57:56.488644 2023] [wsgi:error] [pid 9:tid 140315142702848] [client 192.168.1.33:60062] ModuleNotFoundError: No module named 'django'

The thing is that, django is installed correctly in the container, but somehow it is still giving me the same error.

I have the feeling that it is something related to the wsgi does not using the right python version, but not sure how to fix this issue. Despite of my python3.10 installation, I can still see python3.7 installed in the container.

root@fd5605c69de5:/app/morabusaprj/logs# whereis python

python: /usr/bin/python3.7m /usr/bin/python3.7 /usr/lib/python3.7 /etc/python3.7 /usr/local/bin/python /usr/local/bin/python3.10-config /usr/local/bin/python3.10 /usr/local/lib/python3.10 /usr/local/lib/python3.7

python does not have the path to your site-packages. You can add the tequires path in wsgi.py. Something like:

replacement for WSGIPythonHome "d:/..../django_project/env_folder"
# choose one:
sys.path.append('d:/.../env_folder/lib/site-packages')              # add individual virt.environment packages at the end of sys.path;  global env packages have prio
sys.path.insert(0,'d:/.../env_folder/lib/site-packages')            # add individual virt.environment packages at the beginning of sys.path;  indiv. virt.env packages have prio over global env
    
# replacement   WSGIPythonPath "d:/..../django_project/app_name"    
sys.path.append('d:/.../django_project/app_name')

just need to find out the path in your python installation in the container

Back to Top