Docker deploys Python projects

Introduction

One of the biggest troubles in software development is environment configuration, operating system settings, and installation of various libraries and components. Only if they are correct can the software run. If you run another operating system from one operating system, the usual strategy we adopt is to introduce a virtual machine, such as running a Linux system in a Windows system. This method has a big disadvantage that it takes up more resources, has many redundant steps, and is slow to start. One of the most popular Linux container solutions is Docker. Its biggest advantage is its light weight, low resource usage, and fast startup. What is Docker from this article? What problem does Docker solve? What are the benefits? How to deploy the implementation to a comprehensive introduction.

Advantages of docker deployment project#####

Imagine such a real case. If we want to deploy a Python application, what should we do? First, you need the python operating environment, for example, python3 is deployed, and python2 is on the machine. Install python3 first, but also install various dependent packages, and the machine may have some possible conflicts. After installing python, I found that mysql or redis was also installed. Continue to download and install the configuration. What? The server is no longer needed, need to change to a server? Then do it again. What? Basic applications are too good to be promoted, and need to guide other vendors to deploy? What to do? It can be seen that there were the following pain points in the operation and maintenance of the software industry before Docker:

# 1. Software release and deployment are inefficient and cumbersome, and always require manual intervention
# 2. Ensuring the consistency of the environment
# 3. The cost of migration between different environments is high

Before completing the Docker deployment and installation, let's first understand the advantages of Docker:

# 1. Easy software construction and easy distribution
# 2. Applications are isolated and dependencies are lifted
# 3. Can be used perfectly for CI/CD
# 4. Quick deployment, easy to destroy after testing

Method 1: Based on Python basic mirror

# Prepare data directory
mkdir myproject
cd myproject
docker run -di --name=myproject -p 8080:8080-v /home/myproject:/home python:3.6

# Upload your project to the storage volume mounted on the server
scp django.tar.gz 149.129.38.117:
cp /root/django.tar.gz /home/myproject/
tar xvf django.tar.gz

# Enter the container to install related dependencies
pip install -r requirement.txt
apt-get update
apt-get install vi

# setting.Change py to the following
ALLOWED_HOSTS =['*']
# Run the project(wsgiref)
python manage.py runserver 0.0.0.0:8080

uwsgi run project

pip3 install uwsgi

# Create a uwsgi in the project root path.ini file, write

[ uwsgi]
# Configure the socket connection to nginx
socket=0.0.0.0:8080
# You can also use http
# http=0.0.0.0:8080
# Configure the project path, the directory where the project is located
chdir=/home/django_test
# Configure wsgi interface module file path
wsgi-file=django_test/wsgi.py
# Configure the number of processes started
processes=4
# Configure the number of threads per process
threads=2
# Configuration start management main process
master=True
# Configure the process number file that stores the main process
pidfile=uwsgi.pid
# Configure dump logging
daemonize=uwsgi.log

# Start, stop, restart, view
uwsgi --ini uwsgi.ini #start up
lsof -i :8001    #Query by port number
ps aux | grep uwsgi   #Query by program name
kill -913844       #Kill process

uwsgi --stop uwsgi.pid      #Stop uwsgi via uwsg
uwsgi --reload uwsgi.pid  #Reboot

# nginx forwarding

mkdir -p nginx/conf nginx/html nginx/logs

# Create nginx in the conf directory.conf

worker_processes  1;
events {
 worker_connections  1024;}
http {
 include       mime.types;
 default_type  application/octet-stream;
 sendfile        on;
 keepalive_timeout  65;
 server {
  listen       80;
  server_name  localhost;
  location /{
   # uwsgi_pass 149.129.38.117:8080;
   proxy_pass http://149.129.38.117:8080;}  
  error_page   500502503504/50x.html;
  location =/50x.html {
   root   html;}}}

docker run --name nginx -id -p 80:80-v /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /home/nginx/html:/etc/nginx/html -v /home/nginx/logs:/var/log/nginx nginx

# Run the project with uwsgi in python's docker

# External access: http://149.129.38.117/

Recommended Posts

Docker deploys Python projects
Centos7 deploys python3 virtual environment
Deploy python3 and nginx projects on ubuntu18.04