Alex
Alex

Reputation: 732

Nginx + Django: ModuleNotFoundError: No module named 'app'

I am trying to run my Django application with Nginx and Gunicorn in Docker

In docker compose logs i have the following error: (full log: https://pastebin.com/EXd3Bsii)

File "/usr/local/lib/python3.9/site-packages/gunicorn/util.py", line 359, in import_app
django_1  |     mod = importlib.import_module(module)
django_1  |   File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
django_1  |     return _bootstrap._gcd_import(name[level:], package, level)
django_1  |   File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
...
django_1  |   File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
django_1  | ModuleNotFoundError: No module named 'app'

How can I solve the problem?

Should I specify wsgi configuration?

My code wsgi,py

import os
from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'potok.settings')
application = get_wsgi_application()

nginx-conf.conf

upstream app {
    server django:8000;
}

server {
    listen:80;
    server_name: localhost;

    location / {
        proxy_pass http://app;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect_off;
    }

    location /staticfiles/ {
        alias /var/www/html/staticfiles/;
    }
}

docker-compose.yml

version: '3.9'

services:
  django:
    build: . # path to Dockerfile
    command: sh -c "gunicorn --bind 0.0.0.0:8000 app.wsgi"
    volumes:
      - .:/project
      - static_data:/project/static
    expose:
      - 8000
    environment: 
      - DATABASE_URL=postgres://postgres:XXXXXXXXX@localhost:5432/lk_potok_2"
      - DEBUG=1
  ...
  
  nginx:
    image: nginx:1.19.8-alpine
    depends_on: 
      - django
    ports: 
      - "80:80"
    volumes:
      - static_data:/var/www/html/static
      - ./nginx-conf.d:/etc/nginx/conf.d

volumes:
    pg_data:
    static_data:

Upvotes: 0

Views: 1199

Answers (1)

Pooya Mohammadi Kazaj
Pooya Mohammadi Kazaj

Reputation: 153

Your nginx.conf has a problem: proxy_pass http://django:8000;

upstream app {
    server django:8000;
}

server {
    listen:80;
    server_name: localhost;

    location / {
        proxy_pass http://django:8000;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect_off;
    }

    location /staticfiles/ {
        alias /var/www/html/staticfiles/;
    }
}

Nginx and django are in the same network, nginx can access the django instances generated by gunicorn using the name of the container and the port guniconr binds the workers to.

And also you need to modify the docker-compose.yaml as well. Gunicorn can't access app because you have named it application not app.

version: '3.9'

services:
  django:
    build: . # path to Dockerfile
    command: sh -c "gunicorn --bind 0.0.0.0:8000 wsgi:application"
    volumes:
      - .:/project
      - static_data:/project/static
    expose:
      - 8000
    environment:  

wsgi:application is the address to your application. In your docker-file you can change "current directory" with WORKDIR <dir-path. It's easier to change your docker-container directory to the one containing wsgi. In that case the wsgi:application will work. If there are barriers that prevent you from changing the container's WorkDir you need to provide the full address to your wsgi.py which is as follow: <full-path>.wsgi:application

Upvotes: 1

Related Questions