Docker build Python Web Environment

Docker out looking for the deployment of Python-related projects to learn, find the NEXT Tencent open class in the classroom to build Python Web Docker [environment] of course, this sort its contents and knowledge points summary.

The course is a total of about six hours, it is suitable for white entry Docker deployment of basic courses. The main chapters following courses:

System and software configuration:

Win10 + CentOS7 + Python3 + MySQL8 + PyCharm  + VMware15 pro + MobaXterm + Navicat Premium 12

 

First, the Basics

1, CentOS default installation of Python2, you can install Python3, original Python2 not uninstall, CentOS7 there is Python2 program relies.

2, yum command can not be directly installed Python, the source need to install epel

yum  install warm-release

3, Linux meaning each folder

1 / the Boot the default directory is stored in the Linux kernel and boot files.
2 / bin directory contains the Linux common commands.
3 / sbin This directory is used to store management procedures used by system administrators.
. 4 / var directory is for the file often changes, including a log data file.
5 / etc directory will be used during the various storage system management configuration files and subdirectories, such as network configuration files, Mysql configuration file.
. 6 / dev This directory contains all the external devices use the Linux system, it is actually these external port access devices, such as printers.
7 / mnt temporarily hang another file system in the directory.
8 / root if you are logged on as superuser, this is the super-user's home directory.
. 9 / home to establish if a user named "xx", then at / home there is a corresponding directory "/ home / XX" path, used to store the user's home directory.
10 / usr user applications and files are stored virtually in the directory.
11 /lib This directory is used to store system dynamically linked shared libraries, almost all applications make use of shared libraries in that directory.
12 / opt default when you install third-party software will find this directory, so when you do not install such software it is empty, but if once you delete it later when installing such software is likely to run into trouble .
13 / tmp for temporary data generated during execution of the different programs, the directory will be automatically cleared.
14 / proc directory can be obtained in this system information that is generated by the system in memory of their own, not the contents of the directory on your hard disk and in memory.
15 / misc allow multiple users to transfer their accumulation and temporary files.
16 / Lost + found directory which in most cases are empty. But when a sudden power failure or abnormal shutdown after some temporary file is stored here. 
17 files color meaning: blue folders; green is the executable file; blue is linked files; red box file is added to the SUID bit, arbitrarily limit the right; red for compressed files; brown for the device file.

4, commonly used Linux commands

. 1  Touch   # Create a blank document
 2  RM -rf # delete files or folders (-r recursive Representative - Representative force delete f)
 . 3  mkdir   # Create an empty folder
 . 4  CD .. # back to the previous
 . 5  the tar       # compression and decompression , -cvf compression parameters, - xvf decompress
 # .tar above is specific to Linux compressed file format, a compressed file that can retain information on file permissions

Two, Web articles

1, Flask framework compared with Django is a lightweight Web framework that can be expanded through third-party.

2, Flask has a lot of expansion libraries, developers are free to choose to install:

. 1 Flask- the Admin: Manage widget increase
 2 Flask- the Cache: connecting the Redis
 . 3 Flask- Cors: Cross-domain Ajax
 . 4 Flask- the User: a user management module
 . 5 Flask- Snow: Snow algorithms produce globally unique primary key values
 . 6 Flask- the SSE : push messages
 7 Flask- SQLAlchemy: read and write MySQL database
 8 the Flask-the uploads: accept the file browser upload

3, Flask frame advantages:

  1. Getting started is simple, it can be clearly understood by the official guide the operation of Flask retention
  2. Support MVC model (Model-View-Controller)
  3. Falsk compatible with a variety of database templates
  4. Flask routing and routing functions set by the decorators, developers do not need the aid of other matching files

 4, MVC pattern: a complex software system, by abstracted model layer (the Model), layer view (View) and the control layer (the Controller), a clear division of responsibilities

 

 5, Blueprint Flask function items can be defined in different WEB function modules

 

 6, Flask definition module file: First create a Python file, and then create a blueprint for an object (Blueprint), the blueprint for the object to the registration Web function above, and finally the object to the registration blueprint Flask object.

7, Flask Module Language:

  1. Flask support dynamic page technology, in the background rendering dynamic HTML pages
  2. Jinja2 Python is a widely used template engine, the syntax is easy to learn
  3. Flask中的模板文件存放在templates目录,图片、样式和脚本存放在static目录

8、WEB方法中,render_template()函数用来把模板文件渲染成动态HTML

 

 9、模板文件中,双大括号({{变量/表达式}})可以对模板页面插值

 

 10、部署Python项目的时候,为了不泄露Python程序代码,所有我们要编译Python程序(pycharm项目文件夹会自动生成pyc文件)

 

 11、Flask只允许本机访问5000端口,如果外机访问,需修改python源代码,app.run(host='0.0.0.0')

12、Linux开发5000端口持久生效:

1 firewall -cmd --zone=pulic --add-port=5000/tcp --permanent

 三、Docker篇

 1、虚拟化部署环境的重要性:

  1. 直接把程序按照或者部署在Linux操作系统上,很容易引发资源冲突(比如CentOS自带MariaDB数据库,再安装MySQL数据库会冲突)
  2. 程序卸载不干净,导致无法安装或部署新程序
  3. 每次部署之前要安装很多软件,修改复杂的配置文件
  4. 无法让服务器硬件资源实现多租户服务
  5. 增大了在Linux系统上部署集群和分布式的难度

2、Vmware和Docker对比:

  1. Vmare是重量级虚拟机,每个虚拟实例都虚拟了完整的硬件环境,所以对硬件要求较高
  2. Docker是轻量级虚拟机,只虚拟了少量的硬件设备,虚拟实例共享使用Linux内核

 

3、Docker是跨平台的轻量级虚拟机,但是在Window和MacOS上面有部分功能无法使用(Win10 有内置Linux虚拟机,故能运行Docker)

4、云计算中的Docker虚拟机:

 

5、SELINUX是CentOS系统捆绑的安全服务程序,因为安全策略太过严格,建议关闭,修改/etc/selinux/config文件,设置SELINUX=disabled

6、使用yum命令安装Docker服务即可

1 yum install docker -y #-y代表默认安装

 

7、DockerHub是Docker公共镜像仓库,为用户提供大量的镜像文件:https://hub.docker.com/,由于国内网络访问DockerHub很慢,无法下载镜像文件,可以使用Docker加速器:

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://n1mngx9r.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

8、Docker 包含三个基本概念,分别是镜像(Image)、容器(Container)和仓库(Repository)。镜像是 Docker 运行容器的前提,仓库是存放镜像的场所。

9、什么是镜像:

  1. 为了快速打包和部署软件环境,Docker引入镜像机制
  2. 镜像是一个配置好的只读层软件环境
  3. 我们可以通过dockerfile文件创建出镜像,也可以从Docker仓库中下载到镜像

10、什么是容器:

  1. 容器是镜像基础之上创建出的虚拟实例,内容可读可写
  2. 一个Docker镜像可以创建出多个容器,而且容器直接相互隔离,部署的程序不会相互干扰
  3. 所有的容器直接使用书主机的Linxux内核、内存和硬盘,所以容器的性能非常接近于宿主机

11、常用Docker命令:

 

 

 

12、Docker网络环境:容器默认会分配动态IP地址,会导致下次启动容器时候IP地址发生变化,因此可以创建一个Docker内部网段:

docker network create --subnet=172.18.0.0/16 mynet #创建18网段名称为mynet
docker network rm mynet #删除mynet网段

 

13、创建容器示例(容器使用端口映射/目录挂载/网络分配技术):

docker run -it -d --name=p1 -p 9500:5000 -v /root/project:/root/project -net mynet --ip 172.18.0.2 python:3.8 bash
#创建容器,-d后台默认运行,--name容器名称p1,-p端口映射宿主机9500端口,-v目录挂载,-net使用mynet网络并分配ip,使用python3:3.8镜像,bash命令行界面

 

14、容器安装flask,可以使用国内镜像加快速度(sqlalchemy / mysql-connector-python):

docker exec -it p1 bash #进入p1容器
pip install flask -i https://pypi.tuna.tsinghua.edu.cn.simple/#清华大学镜像

 

15、用docker pull命令下载MySQL镜像(为确保部署没问题,Win端与Linux的各软件版本应尽量保持一致)

 

 16、创建MySQL容器:

docker run --name m1 -p 4306:3306 --net mynet --ip 172.18.0.3 -v /root/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=abc123456 -d mysql:8.0.18
#创建MySQL容器不需要加-it,--name容器名称,--net分配网络 -v目录挂载(数据库挂了文件仍能提取),-e用户密码 ,-d该容器创建即后台运行

 

17、创建Session类(用来连接数据库)

from sqlalchemy.orm import sessionmaker #用以创建session类
from sqlalchemy import create_engine #保存数据库连接信息
url="myslq+mysqlconnector://root:[email protected]:4306/test" #数据库用户名密码地址端口及库名
engin=create_engine(url,pool_size=5)  #数据库连接池对象个数
Session=sessionmaker(bind=engin) #创建session类

 

18、执行查询语句:先创建Session对象,然后才可以执行SQL语句,最后还要关闭数据库连接

session=Session()
sql="select empno,ename,job from t_emp"
cursor=session.execute(sql)
result=cursor.fetchall()
session.close()

 

最后补下课程链接:

https://ke.qq.com/course/478268?taid=4069730621541436

Guess you like

Origin www.cnblogs.com/rango-lhl/p/12293100.html