notalentgeek
notalentgeek

Reputation: 5829

Gunicorn does not work when try to launch Django application

I am following this tutorial, until up to this part.

start.sh

#!/bin/bash

# Start Gunicorn processes
echo Starting Gunicorn.
exec gunicorn helloworld.wsgi:application \
    --bind 0.0.0.0:8000 \
    --workers 3

My directories is like this.

awesome_app
-awesome_app
--__init__.py
--celery.py
--settings.py
--urls.py
--wsgi.py
-awesome_app_to_do_list
--a lot of stuffs here
-manage.py
-start.sh

Here is the content of my wsgi.py.

"""
WSGI config for airport project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "awesome_app.settings")

application = get_wsgi_application()

I adapted the launch code to this.

#!/bin/bash

# Start Gunicorn processes
echo starting gunicorn
exec gunicorn awesome_app.wsgi:application \
    --bind 0.0.0.0:8080 \
    --workers 3

After I make it executable and run the script from the root of the project awesome_app and not from awesome_app/awesome_app. I received this error, ImportError: No module named 'myproject'. I have looked at this SO discussion, but the error is still there. What should I do?

Upvotes: 0

Views: 2538

Answers (4)

notalentgeek
notalentgeek

Reputation: 5829

Solved this issue. Apparently the culprit was celery.py settings. In it, I has these codes.

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')

app = Celery('myproject')

Stupidly enough, I just copy paste codes from Celery + Django example. Even more stupid because my web application works fine, idk why.

So I change that codes in celery.py to these and it is working.

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'awesome_app.settings')

app = Celery('awesome_app')

For anyone comes in having the same problem please take a look at os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings') and change myproject to whatever your Django project named.

Upvotes: 1

misraX
misraX

Reputation: 985

First of all The ONBUILD image variants are deprecated, and their usage is discouraged. For more details, see docker-library/official-images#2076. check: Python Docker Hub

So for a best practice way with more understanding to what's actually happening the dockerfile will be like this:

IMPORTANT Make sure you are in your host directory that has your initiated django project and the excuted start.sh.

1) Fetching FROM a base image FROM python:2 change it to 3 if you want to use python3.

2) Specify WORKDIR inside the docker img lets say WORKDIR /usr/scr/project that will insure a correct path to gunicorn when running the start.sh

3) Copying COPY requirements.txt to the docker image COPY requirements.txt ./

4) Runing RUN pip to install your requirements inside the docker image pip install --no-cache-dir -r requirements.txt

5) Copying COPY all the project files into the docker image COPY . .

4) Define a starter command CMD CMD ./start.sh.

DockerFile

# Dockerfile

# Fetching `FROM` a base image `FROM python:2` 
# Change it to 3 if you want to use python3.
FROM python:2

# Specify WORKDIR inside the docker img 
# Lets say WORKDIR /usr/scr/project 
# That will insure a correct path to gunicorn when running the start.sh
WORKDIR /usr/scr/project

# Copying COPY requirements.txt to the docker image COPY requirements.txt ./
COPY requirements.txt ./

#Runing RUN pip to install your requirements inside the docker image pip install --no-cache-dir -r requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Copying COPY all the project files into the docker image COPY . .
COPY . .

# EXPOSE port 8000 to allow communication to/from server
EXPOSE 8000

# Define a starter command CMD CMD ./start.sh.
CMD ["./start.sh"]

edited: for missing RUN in the dockerfile

Upvotes: 0

Dorian Dore
Dorian Dore

Reputation: 962

BaconSandwich's answer is the way to go. However, If the script is required, try setting the working directory to the directory the script is located in.

#!/bin/bash
cd "$(dirname "$0")"

Try prepending this to the script and see if it works.

Upvotes: 0

BaconSandwich
BaconSandwich

Reputation: 165

Get into your project directory and try just running this in your command line instead of running the script and see if it works:

gunicorn --bind 0.0.0.0:8000 myprojectname.wsgi

Upvotes: 1

Related Questions