Detailed tutorial on deploying Django project using Docker on centos8

Detailed tutorial on deploying Django project using Docker on centos8

introduction

In this article, we will introduce how to deploy Django projects in Docker using django + uwsgi + nginx deployment.

Since what is recorded is the learning process, the current higher version is used.

Python version is 3.8.3
Django version is 3.0.6
nginx version is 1.17.10

Okay, after a brief introduction, let’s get to the point.

Create a working directory

Create a working directory to store the project and files such as Dockerfile.

mkdir uwsgidocker

Briefly explain each file

docker-compose.yml: Docker Compose is a command line tool provided by Docker to define and run applications composed of multiple containers.
            Using compose, we can declaratively define the services of an application through a YAML file and create and start the application with a single command.
            At the beginning I did not use docker-compose.yml
Dockerfile: It is a text file used to build an image. The text content contains the instructions and instructions required to build the image.
my_django: is a newly created Django project, mainly changing ALLOWED_HOSTS = [] to ALLOWED_HOSTS = ["*"] 
nginxconf: is a folder containing nginx configuration and Dockerfile files for creating nginx image.
pip.conf: It is about pip configuration, mainly used for pip to accelerate downloading.
uwsgi_conf.ini: uwsgi configuration file

Note: In the settings.py file in the Django project, you need to change ALLOWED_HOSTS = [] to ALLOWED_HOSTS = [" * "] .

Make uwsgi image

Create a uwsgi image according to the Dockerfile file and run it.

FROM python:3.8.3
# Create a directory RUN mkdir -p /usr/src/app
# Set the working directory WORKDIR /usr/src/app

# Copy the pip.conf file to /root/.pip/pip.conf
COPY pip.conf /root/.pip/pip.conf
# Update pip
RUN pip install --upgrade pip
# Download Django and uwsgi In general projects, you only need to download requirement.txt
RUN pip install django && pip install uwsgi
#Copy all the files in the current directory, only copy the project, uwsgi configuration file COPY . /usr/src/app/
# Start uwsgi at run time
CMD uwsgi --ini uwsgi_conf.ini
# Expose ports EXPOSE 80 8080 8000 8888

uwsgi configuration file, official website

[uwsgi]
# Project directory. Since it is in the current directory, just write chdir = my_django
# uwsgi startup file, wsgi.py under the project
module = my_django.wsgi
# Allow the main thread to exist (true)
master = true
# Number of processes processes = 1
# Used to specify the port where the project runs. You can use socket and http. I use http for easy viewing. http = 0.0.0.0:8000
# socket = 0.0.0.0:8000

# http = 10.0.0.10:8000
# socket = 10.0.0.10:8008
# socket = /usr/src/app/my_django/uwsgi.sock
# Automatically clean up the environment when the server exits, delete the unix socket file and pid file vacuum = true

OK, with these two files, you can create a uwsgi image. Execute the following command to generate the image.

docker build -t myuwsgi ./

Use docker images to view images

Run the uwsgi image

Now that the image has been created, the next step is to run the image and view it in the browser.

Use the following command directly to expose the port for easy viewing.

docker run --rm -it --name webuwsgi -p 8000:8000 myuwsgi

Operation Results

Next, you can access it in the browser, enter the IP and port to access 192.168.56.102:8000

This proves that the uwsgi image has been created and can run successfully.

The next step is to create an nginx image for reverse proxy

Creating an Nginx Image

First enter the nginxconf directory, cd nginxconf/ for easy operation, and then edit the Dockerfile file

FROM nginx

# Delete the default nginx configuration file RUN rm -rf /etc/nginx/conf.d/default.conf
# Copy the configuration file in the current directory to the /etc/nginx/conf.d/directory COPY nginx.conf /etc/nginx/conf.d/nginx.conf

EXPOSE 80

Edit nginx.conf file

server {
 # Listening port listen 80;
 #Host name server_name localhost;
 location / {
 include uwsgi_params;
 # The ip and port of the uwsgi service,
 proxy_pass http://192.167.0.2:8000;
 # When uwsgi uses socket, you can use it directly # uwsgi_pass 192.167.0.2:8000;
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header Host $host;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }
 
 location /static {
 # Static file alias /usr/share/nginx/html/static;
 }
}

The IP of the uwsgi container can be checked using docker inspect container name

docker inspect webuwsgi

Check

OK, the next step is to create an nginx image. Use the following command to create an image

docker build -t mynginx ./

Use docker images to view the image

Run the Nginx image

Now that the image has been created, the next step is to run the image and view it in the browser.

Use the following command directly to expose the port for easy viewing.

docker run --rm -it -p 80:80 --name nginxweb mynginx

Operation Results

Next, you can access it in the browser and enter the IP address 192.168.56.102 directly.

Well, this is how to deploy the Django project using uwsgi+nginx on docker.

Next, we use uwsgi+nginx+docker-compose to deploy the Django project.

Using Docker-compose

Edit the docker-compose.yml file, assign an IP address, and make some changes to the nginx and uwsgi configuration files.

docker-compose.yml file

version: '3'
services:
version: '3'
services:
 uwsgi:
 build:
 context: ./
 image: uwsgi
 restart: always
 networks:
 Django:
 ipv4_address: 10.0.0.10
 ports:
 - "8000:8000"
 volumes:
 - /root/uwsgidocker/:/usr/src/app/:rw
 command: uwsgi --ini /usr/src/app/uwsgi_conf.ini

 nginx:
 image: myweb
 build:
 context: ./nginxconf
 ports:
 - "80:80"
 - "8080:8080"
 volumes:
 - /root/uwsgidocker/nginxconf/nginx.conf:/etc/nginx/conf.d/nginx.conf:rw
 restart: always
 privileged: true
 networks:
 Django:
 ipv4_address: 10.0.0.20
networks:
 Django:
 ipam:
 config:
 - subnet: 10.0.0.0/24

uwsgi_conf.ini file, just change the ip

[uwsgi]
chdir = my_django

module = my_django.wsgi

uid = root
gid = root

master = true

processes = 1

# http = 0.0.0.0:8000
# socket = 0.0.0.0:8000

# http = 10.0.0.10:8000
socket = 10.0.0.10:8008
# socket = /usr/src/app/my_django/uwsgi.sock

vacuum = true

nginx.conf file, similarly, change the ip

server {
 listen 80;
 server_name localhost;
 location / {
 include uwsgi_params;
 # proxy_pass http://192.167.0.2:8000;
 # uwsgi_pass 192.167.0.2:8000;
 # proxy_set_header Host $host;
 # proxy_redirect off;
 # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 # uwsgi_pass unix:/usr/src/app/my_django/uwsgi.sock;
 uwsgi_pass 10.0.0.10:8008;
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header Host $host;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }
 location /static {
 alias /usr/share/nginx/html/static;
 }
}

OK, after slightly modifying the configuration, you can run it directly

docker-compose run

1. Start-Build image &&Start container

docker-compose up or docker-compose up -d background run

2. Stop

docker-compose stop #Stop the container

3. Stop and delete the container

docker-compose down

4. Enter the IP address and access the results.

Do you find it very convenient to use docker-compose ? I recommend you to use it!

Summarize

This is the end of this article about the detailed tutorial on how to deploy Django projects on centos8 using Docker. For more information about deploying Django projects on Docker, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the deployment of Django to CentOS7
  • Deploy the Django project to the CentOs server
  • Detailed explanation of the whole process of centos7+django+python3+mysql+Alibaba Cloud deployment project
  • Tutorial on deploying nginx+uwsgi in Django project under Centos8
  • How to deploy Django service nginx+uwsgi on Centos
  • Detailed tutorial on deploying Django project under CentOS
  • How to deploy Django project to CentOS

<<:  LINUX Checks whether the port is occupied

>>:  MySQL query specifies that the field is not a number and comma sql

Recommend

Summary of common Mysql DDL operations

Library Management Create a library create databa...

Details of 7 kinds of component communication in Vue3

Table of contents 1. Vue3 component communication...

Centos7 installation and configuration of Mysql5.7

Step 1: Get the MySQL YUM source Go to the MySQL ...

jQuery plugin to achieve image suspension

This article shares the specific code of the jQue...

Detailed explanation of Linux system directories sys, tmp, usr, var!

The growth path from a Linux novice to a Linux ma...

MySQL 8.0.23 free installation version configuration detailed tutorial

The first step is to download the free installati...

The HTML 5 draft did not become a formal standard

<br />Yesterday I saw at W3C that the new HT...

Several ways to easily traverse object properties in JS

Table of contents 1. Self-enumerable properties 2...

MySQL 8.0.11 installation and configuration method graphic tutorial

The installation and configuration methods of MyS...

Elements of user experience or elements of web design

System and user environment design <br />Th...

Use Element+vue to implement start and end time limits

This article example shares the specific code for...

Detailed tutorial for installing MySQL 8.0.11 compressed version under win10

After reinstalling my computer recently, I downlo...