Table of contents
Route a Django code through Nginx
Create a Docker file for an app django-notes-app from scratch.
Build an image using the Docker file and run a container.
Verify that the application is working as expected by accessing it in a web browser.
Push the image to a public or private repository (e.g. Docker Hub )
Install Nginx on your linux machine
Update your linux system using sudo apt update
first in order to install Nginx.
Once update is complete, install nginx using
sudo apt install nginx
To check whether nginx in active, use
systemctl status nginx
Create a folder called projects and clone the code from github using following command:
git clone <github_repo_link>
go inside the folder using
cd django-notes-app
To learn how to install docker, refer to my previous blog.
now lets build a docker file from scratch:
Create and open a file using the vim editor:
vim Dockerfile
Enter the following code in vim editor:
FROM python:3.9
COPY . .
RUN pip install -r requirements.txt
CMD ["python","manage.py","runserver","0.0.0.0:<portassign>"]
Now that the docker file is ready, let's build an image from the docker file:
docker build -t django-notes-app:latest .
check using docker ps
whether the we can see image in the list:
Since, we have an image now, lets run a container using the docker image:
docker run -d -p 9000:9000 react-notes-app:latest
Your docker container will be ready, check using docker ps
But now, we dont want to expose our public ip in the browser, but we want to serve it to the user using reverse proxy and route it through nginx.
To check whether your application is working on local host,
curl -L http:127.0.0.1:9000
Now we need to make changes to nginx configuration. For that,
cd
cd /etc/nginx/sites-enabled
You will see a default file:
now edit the default file using vim editor. Since, we will not have user permissions to access it, use sudo
sudo vim default
edit the location part on default file and add this below location / {
:
proxy_pass http://127.0.0.1:9000;
Now save the file.
After this, we need to restart nginx
go in django-todo-app>mynotes-build and copy the static folder to /var/www/html
cp static /var/www/html
Now just copy the public IP to the browser and you will see your page is running