introduction
In this article, we will introduce the deployment of Django projects in Docker through django + uwsgi + nginx deployment.
As it records 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
After a brief introduction, we move on to the topic.
Create a working directory to store the project, Dockerfile and other files.
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 each service of the application through YAML files, and create and start the application by a single command.
I did not use docker at the beginning-compose.yml
Dockerfile:It is a text file used to build a mirror. The text content contains instructions and instructions for building a mirror.
my_django:Is a newly created django project, mainly ALLOWED_HOSTS =[]Change to ALLOWED_HOSTS =["*"]
nginxconf:It is a folder that contains nginx configuration and Dockerfile file for creating nginx image.
pip.conf:It is about pip configuration, mainly used for pip to accelerate downloads.
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 a mirror image of uwsgi based on the Dockerfile and run it.
FROM python:3.8.3
# Create a directory
RUN mkdir -p /usr/src/app
# Set working directory
WORKDIR /usr/src/app
# Pip.Copy the 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 only need to download requirements.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
# Exposed port
EXPOSE 80808080008888
uwsgi configuration file, official website
[ uwsgi]
# The project directory, since it is in the current directory, just write it directly
chdir = my_django
# uwsgi startup file, wsgi under the project.py
module = my_django.wsgi
# Allow the main thread to exist (true)
master =true
# Number of processes
processes =1
# Used to specify the running port of the project, socket and http can be used, 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 unix socket files and pid files
vacuum =true
Well, with two files, uwsgi mirror can be made. Execute the following command to generate a mirror image.
docker build -t myuwsgi ./
Use docker images
to view the image
Run uwsgi mirror
Now that the image has been made, the next step is to run the image and view it in the browser.
Use the following commands directly to expose ports for easy viewing.
docker run --rm -it --name webuwsgi -p 8000:8000 myuwsgi
operation result
Then you can access in the browser, enter the ip and port to access 192.168.56.102:8000
This proves that the uwsgi image is completed and can run successfully.
The next step is to make nginx mirrors for reverse proxy
First enter the nginxconf directory, cd nginxconf/
for easy operation, then edit the Dockerfile
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/etc/nginx/conf.d/Under contents
COPY nginx.conf /etc/nginx/conf.d/nginx.conf
EXPOSE 80
Edit the nginx.conf file
server {
# Listening port
listen 80;
# CPU name
server_name localhost;
location /{
include uwsgi_params;
# ip and port of uwsgi service,
proxy_pass http://192.167.0.2:8000;
# uwsgi can be used directly when using socket
# 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 files
alias /usr/share/nginx/html/static;}}
The IP of the uwsgi container can use docker inspect
container name
docker inspect webuwsgi
View
Okay, the next step is to make nginx mirror, use the following command to make mirror
docker build -t mynginx ./
Run Nginx image
Now that the image has been made, the next step is to run the image and view it in the browser.
Use the following commands directly to expose ports for easy viewing.
docker run --rm -it -p 80:80--name nginxweb mynginx
operation result
Then you can access it in the browser, enter ip to directly access 192.168.56.102
Well, this is the deployment of django project using uwsgi+nginx on docker.
Next we use uwsgi+nginx+docker-compose
to deploy the django project.
Edit the docker-compose.yml file, assign ip, and make slight modifications 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 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;}}
Well, after slightly modifying the configuration, you can run directly
docker-compose up or docker-compose up -d Run in the background
docker-compose stop #Stop the container
docker-compose down
Did you find that using docker-compose
is particularly convenient? I recommend you to use this too!
to sum up
So far this article on the detailed tutorial of centos8 using Docker to deploy Django projects is introduced. For more related docker deployment django project content, please search for previous articles of ZaLou.Cn or continue to browse related articles below. Hope you will support more in the future. ZaLou.Cn!
Recommended Posts