Use Docker to build a python environment

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

image-20230322230331515

2. Environmental preparation

  1. One Linux server (mine is CentOS7)

  2. Project Preparation: College Management System

    image-20230322231514922

3. Implementation ideas

  1. Install docker on linux server
  2. pyhonBuild images based on project dependencies
  3. Run the new pythoncontainer, 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

image-20230323200059786

1.2. Writing DockerfileDocumentation

  1. 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

  2. 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/
    

    image-20230322231952799

  3. 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

    image-20230322232208756

  4. 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”是项目的执行入口
    
  5. 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

    image-20230323201045951

  6. After the build is successful, you will see a new image (docker images)

image-20230323201122468

  1. Create a python container and check whether the dependent packages are installed successfully

    build test_envcontainer

    docker run -id --name=test_env python3.7:student_env /bin/sh
    

    image-20230323201215272

    Enter test_envthe container and view the installation package

    docker exec -it -u 0 test_env  /bin/sh
    

    image-20230323201327655

    If there are dependent packages that are not installed, they can student_envbe installed in the container by themselves

    pip install 依赖包==版本 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
    

2. Map the project code in the python container

  1. Map the project code location to the container

    Explanation: Here I created another envcontainer

    docker 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容器中
    

    image-20230323202330193

    1. 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.pythe command to start the project.

      image-20230323202805686

      It's simpler, don't bother, use it directly vimto modify, in fact, it is better to use mapping (but this is not very good)

      VIMI don't know why I can't install it in the container

      image-20230323210334388

      docker 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 
      
      
    2. verify

      It will prompt that port 8099 will be occupied, because I have already run the dokcer container when I start itrun_server.py

      image-20230323210627726

      ip:8099/api/departments/We enter in the browser:

      If the following picture appears, it is allowed to be correct

      image-20230323210932586

Guess you like

Origin blog.csdn.net/AAIT11/article/details/130075077