Deploy Django on Nginx using Docker – KT Notes

Nginx is an excellent web server. Start with a docker image.

Environment:

  • Private Ubuntu 20.04
  • Private/Public Django application hosted in GitHub

Get started:

Start Nginx with Docker

Start a new nginx docker container from the official Nginx docker image

docker run -it –rm -d -p 8080:80 –name web nginx

  • -it – creates an interactive terminal
  • –rm – remove container when it exits
  • -d – start container in detached mode
  • -p – 8080:80 – open host port 8080 mapped to container port 80 (nginx)
  • –name web – name the docker container
  • docker image name = nginx

Inspect and Connect to Docker Container

  • docker ps – list active containers
  • docker attach web – connects to the nginx executable running in container
  • docker exec -it web /bin/bash – connect to container with an interactive bash terminal

Setting up Nginx Docker Container

connect with Docker Exec, then start the setup.

How to create persistence for files…

This section is a work in progress…..

Setup Django folder as VENV (as needed, sudo apt install python3-env).

# if needed
sudo apt install python3-venv

# Setup a VENV
cd /opt/docker
python -m venv django
cd django
source bin/activate

# now in VENV, install Django
pip install django

# clone in the Django App
git clone git@github.com:username/webappname.git
cd webappname

# migrate data, create superuser
python manage.py migrate
python manage.py createsuperuser

# but wait, this should be in the docker container?
# note quite, as this all needs to persist between container instances

Next Steps