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

How to install Nginx in CentOS

Official documentation: https://nginx.org/en/linu...

Solve the problem of docker images disappearing

1. Mirror images disappear in 50 and 93 [root@h50...

CentOS 7 configuration Tomcat9+MySQL solution

Configure Tomcat First install Tomcat Installing ...

How to delete extra kernels in Ubuntu

Step 1: View the current kernel rew $ uname -a Li...

What is the function and writing order of the a tag pseudo class

The role of the a tag pseudo-class: ":link&qu...

MySQL 5.7.15 installation and configuration method graphic tutorial (windows)

Because I need to install MySQL, I record the ins...

Detailed steps for smooth transition from MySQL to MariaDB

1. Introduction to MariaDB and MySQL 1. Introduct...

MySQL trigger detailed explanation and simple example

MySQL trigger simple example grammar CREATE TRIGG...

What is BFC? How to clear floats using CSS pseudo elements

BFC Concept: The block formatting context is an i...

How to install and configure mysql 5.7.19 under centos6.5

The detailed steps for installing mysql5.7.19 on ...

MySQL turns off password strength verification

About password strength verification: [root@mysql...

HTML Tutorial: Unordered List

<br />Original text: http://andymao.com/andy...

Vue gets token to implement token login sample code

The idea of ​​using token for login verification ...

How to use bar charts in Vue and modify the configuration yourself

1. Import echart in HTML file <!-- Import echa...

The current better way to make select list all options when selected/focused

During development, I encountered such a requireme...