Docker introduction and project deployment

Install Docker

Close the SELINUX service

SELINUX is a security service program bundled with the CentOS system. Because the security policy is too strict, it is recommended to build and close this service

Modify the /etc/selinux/config file and set SELINUX=disabled

vim /etc/selinux/config
# 设置SELINUX=disabled

# 设置完成后重启Centos系统
reboot

Install Docker

yum install docker -y
# 常用命令
service docker start
service docker stop
service docker restart

What is DockerHub

DockerHub is a Docker public image warehouse, which provides users with a large number of image files.
Official website: https://hub.docker.com/

Since the domestic network access to DockerHub is very slow, we can use the Docker accelerator

Configure Docker Accelerator

DaoCloud accelerator adopts self-developed intelligent routing and caching technology, and introduces cash protocol layer optimization, which greatly improves the speed and experience of pulling images

# 在centos终端中输入下面命令
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io

Then, modify the /etc/docker/daemon.json file to remove the trailing comma (if any)

What is a Docker image

In order to quickly package and deploy software environments, Docker introduces a mirroring mechanism, which is a configured read-only layer software environment

We can create a mirror through the dockerfile file, or download the mirror from DockerHub

Steps to make an image:
download the python image from DockerHub, create a container, install the required programs in the container, convert the container into an image, and upload it to DockerHub

What is a Docker container

The container is a virtual instance created on the basis of the image, and the content is readable and writable

A Docker image can create multiple containers, and the containers are isolated from each other, and the deployed programs will not interfere with each other

All containers directly use the host's Linux kernel, memory and hard disk, so the performance of the container is very close to that of the host

insert image description here

insert image description here
Mirror related commands

# 下载python3.8镜像
docker pull python:3.8

# 查看已有镜像
docker images

# 查看某个镜像的详细信息
docker inspect python:3.8

# 导出镜像:适合不能联网的服务器安装镜像
# docker save 镜像名:TAG > 压缩文件
docker save python:3.8 > /root/python.tar

# 删除镜像:docker rmi 镜像名:TAG
docker rmi python:3.8

# 从压缩包导入镜像
docker load < /root/python.tar

Note about container-related commands
: After creating a container through the run command, the container will stop when the exit command is executed, but the container will not stop when the exit command is executed to enter the container through the exec command.

# 创建容器
# 用python:3.8这个镜像创建一个名为p1的容器,并进入它的bash命令界面
docker run -it --name=p1 python:3.8 bash

# 查看容器状态
docker ps -a 

# 启动容器:docker start 容器名
docker start p1

# 停止容器
docker stop p1

# 暂停容器
docker pause p1

# 恢复容器运行
docker unpause p1

# 进入已经运行的容器
docker exec -it p1 bash

# 查看容器详细信息:docker inspect 容器名
docker inspect p1

# 删除容器(必须是stop状态才能删除)
docker rm p1

Necessary technologies for creating python containers

Docker network environment

By default, the Docker environment will assign a dynamic IP address to the container, which will cause the IP address to change when the container is started next time.
insert image description here

Docker network management: assign fixed IP addresses to containers

We can create a separate network segment inside Docker (172.18.0.X)

# 创建网段
docker network create --subnet=172.18.0.0/16 mynet
# 查看docker环境中存在哪些网段
docker network ls
# 删除网段:注意需要将关联该网段的容器先删除了
docker network rm mynet
# 指定网段来创建容器(名为p1)
docker run -it --name=p1 --net mynet --ip 172.18.0.2 python:3.8 bash

Docker port mapping

By default, remote access to Docker containers is inaccessible to any host except the host

Through port mapping, the port of the container can be mapped to the port of the host, so that other hosts can access the container

Ports mapped to the host can be used without setting firewall rules.
insert image description here

# -p 宿主机端口:容器端口
docker run -it -p 9500:5000 --name=p1 python:3.8 bash
docker run -it -p 9500:5000 -p 9600:3306 --name=p1 python:3.8 bash

Docker directory mount

In order to save part of the business data outside the Docker environment, or transfer the files of the host machine into the container, it is necessary to mount the directory of the host machine to the container.
One advantage of this is that even if the container fails to start, our data will not be lost.

The Docker environment only supports directory mounting, not file mounting, and a container can mount multiple directories

After the directory is mounted, mount the newly created file in the directory in the container, and it can also be seen in the mounted directory of the host

# -v 宿主机目录:容器目录
docker run -it -v /root/test:/root/project --name=p1 python:3.8 bash

Common parameters for creating containers

  • -it must add this parameter when creating and entering the python container
  • -p maps the port in the container to the host
  • --name container name
  • -d run in the background
  • -v directory mount
  • –net specifies the network segment
  • –ip specifies the ip address

Create a python container

Because we already have a python:3.8 image and a network segment named mynet created by ourselves, so now we directly create the python container.

# 假设我们再root目录创建项目文件
cd /root/
mkdir project
docker run -it -d --name=p1 -p 3002:5000 -v /root/project:/root/project --net mynet --ip 172.18.0.2 python:3.8 bash

Use pip to install project dependencies
Tsinghua University: https://pypi.tuna.tsinghua.edu.cn/simple
Alibaba Cloud: https://mirrors.aliyun.com/pypi/simple/University
of Science and Technology of China https://pypi.mirrors. ustc.edu.cn/simple/Huazhong
University of Science and Technology: http://pypi.hustunique.com/Shandong
University of Technology: http://pypi.sdutlinux.org/Douban
: http://pypi.douban.com/simple/

pip install flask -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install mysql-connector-python -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install sqlalchemy -i https://pypi.tuna.tsinghua.edu.cn/simple

Create mysql container

Download mysql image

docker pull mysql:8.0.31

Create a mysql container: be careful not to add the -it parameter

docker run --name=m1 -p 3001:3306 --net mynet --ip 172.18.0.3 -v /root/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=abc123456 -d mysql:8.0.31

Connect using client visualizer

When using MySQL8.0 to report the error Public Key Retrieval is not allowed
insert image description here
Solution:
1. Right-click to edit the connection
insert image description here
2. Click the driver properties, find allowPublicKeyRetrieval, change false to true
insert image description here
3, and finally restart

After solving the error, let's use the visualization tool to create a new database
insert image description here
and import data
insert image description here
. The import results are as follows:
insert image description here

Code connection error:
ImportError: cannot import name 'connector' from partially initialized module 'mysql' (most likely due to a circular import)
Reason: The current .py file has the same name as the package in the library, just change the file name!
We created a mysql.py before and changed it to mysql_data.py!

Deploy the flask project into docker

Because we need to deploy the project inside the container, the IP address and port to connect to the database need to be changed to the IP and port of the Docker container.
insert image description here
After modification, push to github or Code Cloud

cd /root/
mkdir project
##################### 假设之前还没创建容器和按照依赖(已创建可忽略)
docker run -it -d --name=p1 -p 3002:5000 -v /root/project:/root/project --net mynet --ip 172.18.0.2 python:3.8 bash
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
pip list # 查看是否暗装成功
######################
# 拉取项目代码
git clone https://gitee.com/asong5857229/flask_docker.git

# 进入容器
docker exec -it p1 bash
# 进入挂载目录
cd /root/project/flask_docker/
# 启动程序
python app.py
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:5000
 * Running on http://172.18.0.2:5000

External network access: the external network accesses the IP address 192.168.0.37 of the Linux host and port 3002 of the python project port mapping
insert image description here

insert image description here
As can be seen from the above figure, the python project can be accessed successfully and the database can also be accessed successfully.

When officially deploying, we need to run app.py in the background startup mode

 nohup python app.py > ./flask.log 2>&1 & 

Guess you like

Origin blog.csdn.net/qq_34184505/article/details/128224343