具体操作如下:
# 拉取centos镜像
docker pull centos:centos7
# 运行一个docker container,并把8000端口映射到宿主机端口
docker run -dti -m 3g -p 8000:8000 centos:centos7 /bin/bash
# 查看container列表
docker container list
# 显示如下
# CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 318ae87a35c7 centos:centos7 "/bin/bash" 4 seconds ago Up 3 seconds 0.0.0.0:8000->8000/tcp romantic_swirles
# 进入container
docker exec -it 318ae87a35c7 /bin/bash
# 创建所需要的目录
mkdir /usr/local/flask_project
mkdir /usr/local/python_src
# ctrl+d退出container的shell
# 从宿主机复制需要的程序和文件
docker cp ./Workspace/pycharm/flaskProject/flask_project.zip 318ae87a35c7:/usr/local/flask_project/
docker cp ./Downloads/Python-3.9.12.tar.xz 318ae87a35c7:/usr/local/python_src
# 进入container
docker exec -it 318ae87a35c7 /bin/bash
# 查看目录及文件是否成功创建&复制进来
cd /usr/local
ll
# total 48
# ...
# drwxr-xr-x 2 root root 4096 Apr 5 03:33 flask_project
# drwxr-xr-x 2 root root 4096 Apr 5 03:35 python_src
# ...
ls flask_project/
# flask_project.zip
ls python_src/
# Python-3.9.12.tar.xz
yum -y install unzip
cd flask_project/
unzip flask_project.zip
cd ../python_src/
tar -xf Python-3.9.12.tar.xz
cd Python-3.9.12
yum -y install gcc gcc-c++
yum -y install make
yum -y install libffi-devel # 项目需要用到
yum -y install zlib zlib-devel # 同上
yum -y install xz-devel # 同上
yum -y install bzip2-devel # 同上
yum -y install openssl-devel
./configure --prefix=/usr/local/python3
make # 等一段漫长的时间
make install # 如果上一步没啥问题,执行这步
cd /usr/local/
ll # 这时应该会看到如下目录
# drwxr-xr-x 6 root root 4096 Apr 5 03:56 python3
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
cd /usr/local/flask_project/
pip3 install --upgrade setuptools -i https://pypi.tuna.tsinghua.edu.cn/simple
pip3 install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
pip3 install gunicorn -i https://pypi.tuna.tsinghua.edu.cn/simple
/usr/local/python3/bin/gunicorn -D -w 4 -b 0.0.0.0:8000 --log-file main.log create_table_v2:app # 启动服务
tailf main.log # 查看日志