Article Directory
Use Docker to build a python environment
1. Implementation ideas
The college management system is mainly used for learning and doing interface testing projects
This project comes from "Official Account: Test Qi Tan", you can go to the official account to reply [Interface Test Project]
Remarks: This college management system is mainly used by the author of the official account for interface testing
2. Environmental preparation
-
One Linux server (mine is CentOS7)
-
Project Preparation: College Management System
3. Implementation ideas
- Install docker on linux server
pyhon
Build images based on project dependencies- Run the new
python
container, execute the project
4. Build python service
1. Use Dockerfile to build a python image
1.1 Pull python image
Note: the college management project can only use python3.7
docker pull python:3.7.16-alpine3.17
1.2. Writing Dockerfile
Documentation
-
Export the project dependency package to the requirements.txt file, the name of the txt file can be written freely
pip freeze > requirements.txt
Since requirements.txt already exists in our project, no operation will be performed here
-
Create a new directory on the host machine (the machine where docker is installed) (my directory is in /app), and copy the requirements.txt file into it
mkdir -p app cp student_env/requirements.txt app/
-
Create a Dockerfile file in the app directory (the file name must be Dockerfile, without suffix)
FROM python:python:3.7.16-alpine3.17 WORKDIR /app ADD requirements.txt /app RUN pip install -r requirements.txt CMD["python","run_server.py"]
- The contents of the requirements.txt file are as follows
-
Dockerfile content and explanation are as follows
FROM python:3.7.16-slim # 基于python:3.7.16-slim这个基础镜像构建镜像 WORKDIR /app #切换换工作空间 ADD equirements.txt /app # 将python项目依赖包添加到镜像 RUN pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple# 安装依赖包 CMD ["python","run_server.py"]# 设置容器执行后自动执行的命令,这里的“run_server.py”是项目的执行入口
-
Execute the image build command in the app directory
docker build -t python3.7:student_env .
- -t: mirror name
- The point (.) indicates the directory where the Dockerfile file is located
-
After the build is successful, you will see a new image (docker images)
-
Create a python container and check whether the dependent packages are installed successfully
build
test_env
containerdocker run -id --name=test_env python3.7:student_env /bin/sh
Enter
test_env
the container and view the installation packagedocker exec -it -u 0 test_env /bin/sh
If there are dependent packages that are not installed, they can
student_env
be installed in the container by themselvespip install 依赖包==版本 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
2. Map the project code in the python container
-
Map the project code location to the container
Explanation: Here I created another
env
containerdocker run -id -v /root/student_env:/app -p 8099:8099 -u=root --name=env python3.7:student_env
参数说明: -i:表示运行容器 -t:表示容器启动后会进入其命令行 -d:守护式方式创建容器在后台运行 --name:容器名称 -p 8099:8099:端口映射,宿主机端口:python服务端口(8099) -u=root:指定容器用户为root用户 -v /root/student_env:/app:将项目代码映射到python容器中
-
Verify that the python project can run
python run_server.py
If the following error is reported when running (caused by Django version compatibility)
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x0000024D934BDD08> Traceback (most recent call last): …… File "C:\Python37\lib\site-packages\django\contrib\admin\widgets.py", line 152 '%s=%s' % (k, v) for k, v in params.items(), ^ SyntaxError: Generator expression must be parenthesized
You need to jump to the error reporting path, remove the comma, and then re-enter
python run_server.py
the command to start the project.It's simpler, don't bother, use it directly
vim
to modify, in fact, it is better to use mapping (but this is not very good)VIM
I don't know why I can't install it in the containerdocker cp
Use copy to modify first#将容器中的文件拷贝出来 docker cp 0e49a9fb637d:/usr/local/lib/python3.7/site-packages/django/contrib/admin/widgets.py . #将容器中的文件拷贝回去 docker cp widgets.py 0e49a9fb637d:/usr/local/lib/python3.7/site-packages/django/contrib/admin/widgets.py
-
verify
It will prompt that port 8099 will be occupied, because I have already run the dokcer container when I start it
run_server.py
ip:8099/api/departments/
We enter in the browser:If the following picture appears, it is allowed to be correct
-