Django with Postgres, uWSGI, Nginx, Python 3.
10K+
Dockerfile for Django with uWSGI, Nginx, Postgres, Python 3.
This Image/Dockerfile aims to create a container for Django with PostgreSQL, Python 3 and using uWSGI, Nginx to hosting.
You can build this Dockerfile yourself:
sudo docker build -t "chenjr0719/django-nginx-postgres" .
Or, just pull my image:
sudo docker pull chenjr0719/django-nginx-postgres
Then, run this image:
sudo docker run -itd -p 80:80 chenjr0719/django-nginx-postgres
Wait a minute, you can see the initial project of Django at http://127.0.0.1
You can check is Django work properly with PostgreSQL Server by:
First, query your Django Admin Password:
sudo docker exec -it $CONTAINER_ID cat /home/django/password.txt
Access http://127.0.0.1/admin and log in as Username: admin.
Choose Model_Example to test CRUD function.
If you want to use your Django project which you already developed, use following command:
sudo docker run -itd -p 80:80 -v $PROJECET_DIR:/home/django/website chenjr0719/django-nginx-postgres
Make sure you already add your requirements module at $PROJECET_DIR/requirements.txt.
This image will auto install all requirements module in this file.
If you are using SQLite, this image will auto migrate your data into PostgreSQL Server.
But, if you are using other database, you need to dump data yourself.
Using this command:
python manage.py dumpdata -e sessions -e admin -e contenttypes -e auth --natural-primary --natural-foreign --indent=4 > $PROJECT_DIR/dump.json
If your project name is not website, this image will not work properly.
you need modify the setting of uwsgi.ini in your container:
sudo docker exec $CONTAINER_ID sed -i "s|module=website.wsgi:application|module=$PROJECT_NAME.wsgi:application|g" /home/django/uwsgi.ini
sudo docker restart $CONTAINER_ID
If you want to use Django static files, you have to:
sudo docker exec -it $CONTAINER_ID bash
SETTING_PATH=`find /home/django/website -name settings.py`
vim $SETTING_PATH
In the Static files section, if your static files are in templates/static, add following setting:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "templates/static"),
]
STATIC_ROOT = os.path.join(BASE_DIR, "static")
In default it will use /static/, you can change it by modifying STATIC_ROOT in settings.py
echo yes | python3 /home/django/website/manage.py collectstatic
You can use this command:
sed -i "s|/home/django/website/static|/home/django/website/$STATIC_FOLDER_NAME|g" /etc/nginx/sites-available/default
exit
sudo docker restart $CONTAINER_ID
Content type
Image
Digest
Size
247.6 MB
Last updated
about 10 years ago
docker pull chenjr0719/django-nginx-postgres