程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Deploy Django project with docker

編輯:Python

I've written an article about using centos7+nginx Deploy django Blog post of the project , Because this project is a front-end and back-end mode , Some static files need to use nginx Agents can access , Not used docker.
Original link :https://blog.csdn.net/qq_39253370/article/details/106454587

Directory structure

If the front and back ends are separated , The back end only needs to provide an interface , Add some permission control , Deployment is also relatively simple .

There are two files to note :

  1. pip.conf: Replace pip Source , Downloading the third package will be quick
  2. start.sh: In the use of uwsgi When the deployment , This is the start command
pip.conf:
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
start.sh
uwsgi --ini /var/www/html/mysite1/uwsgi.ini

Upload project to server

It can be placed under any directory :

 Put it here. : /home/{
xx}/data/project below

Pictured :

To write Dockerfile

# establish python3.7 Environmental Science 
FROM python:3.7
# Mirror author 
MAINTAINER xx
# Set up python environment variable 
ENV PYTHONUNBUFFERED 1
# Set up pip The source is domestic 
# I made a mistake when running here , Not for the time being , You can configure according to your server path 
# COPY /home/yufuxiao/data/project/gongyequ/pip.conf /root/.pip/pip.conf
# In a container /var/www/html/ Create mysite1 Folder 
# Be careful : Here is the specific operation in the container ,run After getting up, the following directory will be created 
RUN mkdir -p /var/www/html/mysite1
# Set the working directory in the container 
WORKDIR /var/www/html/mysite1
# Add the current directory file to the working directory of the container (. Indicates the current host directory )
# Will be /home/{xx}/data/project All the following files will be added to the container , Here we only put our project files 
ADD . /var/www/html/mysite1
# utilize pip Installation dependency 
# Installation dependency 
# RUN python3.7 -m pip install -r /home/yufuxiao/data/project/gongyequ/requirements.txt
# Windows Written in the environment of start.sh There is extra at the end of each command line \r character , Need to be removed .
# Start project 
RUN sed -i 's/\r//' /var/www/html/mysite1/gongyequ/start.sh
# Set up start.sh File executable rights 
RUN chmod +x /var/www/html/mysite1/gongyequ/start.sh

Build the image and run

sudo docker build -t docker_django_img:v1 .
-t: tagging
.: Context Directory , Current project and dockerfile Points can be used in a directory , If not in a directory , You need to specify the directory

sudo docker images Look at the constructed image , Here's the picture :
delete mirror :sudo docker rmi Mirror image id -f Parameters : Mandatory deletion

function :

sudo docker run --name Name yourself -p Host port : Container port Image name .
Example :sudo docker run --name gongyequ -p 10418:8000 django_docker_img:v1

Check to see if startup succeeded :sudo docker ps
View the running status of all containers :sudo docker ps -a
Restart container :sudo docker restart Container name or id
Delete container :sudo docker rm Container name or id -f Parameters : Mandatory deletion

Enter the container and run the project

sudo docker exec -it Containers id Or name /bin/bash

Another way to get into the container :docker attach [OPTIONS] CONTAINER
Install all dependent packages :pip install -r requirements.txt
install uwsgi :pip install uwsgi
Start project :python manager.py runserver 0.0.0.0:8000
If the startup is successful , It shows that more than half of the work has been completed here .

To write uwsgi.ini

In the above we used Django It comes with its own runserver The command starts the test server , However, it can only be used for user's local mode development , However, in the actual generation environment, you should use the one that supports high concurrency uwsgi Server to start Django service .

[uwsgi]
# Project name 
project=mysite
# Project path 
base=/var/www/html
# Specify the directory of the project , stay app Switch to the current directory before loading , This is something to pay attention to , yes app Catalog 
# gongyequ:app name 
chdir=/var/www/html/mysite1/gongyequ
# Specify the application, Load the specified python WSGI modular ( Module path must be in PYTHONPATH in )
# Here is the django project wsgi.py file 
# nsproject:django Project name 
module=nsproject.wsgi:application
# Start a master Process to manage other processes 
master=True
# Number of processes , If the main process master Then the subprocess will also kill fall 
processes=2
# use nginx When it comes to socket , When running directly, it is equipped with http
# Appoint IP port // Direct external access 
http=0.0.0.0:8000
# Setting is used for uwsgi Internal buffer size for package resolution . The default is 4k Maximum 64
buffer-size=65536
# Timeout time 
harakiri=60
# Set up running in the background 
daemonize=uwsgi.log

Execute this file :uwsgi uwsgi.ini
External access indicates that the deployment is successful .

Report errors :no python application found, check your startup logs for errors

The general question is uwsgi.ini The configuration in is not written correctly , Please recheck , The most error prone place is the path configuration :

# Specify the directory of the project , stay app Switch to the current directory before loading , This is something to pay attention to , yes app Catalog 
# gongyequ:app name 
chdir=/var/www/html/mysite1/gongyequ
# Specify the application, Load the specified python WSGI modular ( Module path must be in PYTHONPATH in )
# Here is the django project wsgi.py file 
# nsproject:django Project name 
module=nsproject.wsgi:application

Other auxiliary class packages :

It's hard to avoid editing files in containers , Need to install vim,Ubuntu Lower installation vim:

apt-get update && apt-get install apt-file -y && apt-file update && apt-get install vim

Check whether the port is on :

apt-get update && apt-get install net-tools

If there is access inside the container redis Please put bind Change it to 0.0.0.0 Or directly comment bind

uwsgi Other commands

https://www.jianshu.com/p/07458e99198a

docker command

https://www.runoob.com/docker/docker-command-manual.html

uwsgi Related explanations

Django It's a Web frame , The role of the framework is to deal with request and reponse, The rest is not what the framework cares about . So how to deploy Django No Django What we need to care about .

Django What is provided is a development server , This development server , Not tested for safety , And it uses Python Self contained simple HTTPServer Created , It's not safe or efficient

and uWSGI It's a full-featured HTTP The server , All he has to do is put HTTP The protocol is transformed into a language supported network protocol . For example HTTP The agreement translates into WSGI agreement , Give Way Python You can use it directly .
uwsgi It's a kind of uWSGI Internal agreement of , Use binary mode to communicate with other applications .
Reference resources :https://blog.csdn.net/mnszmlcd/article/details/78819237
uwsgi General summary :
First, the client requests to come ------ Send to uwsgi Do the processing ( encapsulation 、 Processing data )------ middleware ---- route ---- Try the function ----- Database operation or template rendering

Recommended articles :https://blog.csdn.net/qq_39253370/article/details/104620553


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved