1:前提准备好所需要的环境软件,第三方库,例如
```bash
docker pull python:3.11.4
2:导入python镜像,目前我的项目使用的3.11.4(注意一定要看项目是什么版本,就要下载什么版本的镜像)
docker load -i python311.tar
3:准备requirements.txt文件,使用pipreqs 可以根据项目代码自动生成依赖文件,只包含实际使用的包
pipreqs .
这是生成pip freeze 会列出当前环境中所有已安装的包及其版本,并输出到文件(如 requirements.txt)
pip freeze > requirements.txt
4:将本地的依赖库导出到一个目录:
pip download -r requirements.txt -d ./dependencies
4-1:如果使用的系统(windows,linux)不同需要手动去下载相应的wheel,有一些wheel带有any表示全部系统通用,有一些是根据系统版本下载的

4-2:可以去pypi下载linux系统charset-normalizer

4-3:使用以下命令可以查看第三方包site-packages的安装的具体位置
root@13c34463bf20:/app
/usr/local/lib/python3.11/site-packages
4-4:使用idea链接docker生成完整的镜像直接导入linux系统,这是Dockerfile文件
FROM python:3.10-slim
# 设置工作目录
WORKDIR /app
# 复制 requirements.txt 文件
COPY requirements.txt .
# 安装依赖包
RUN pip install --no-cache-dir -r requirements.txt
# 复制应用程序代码
COPY . .
# 设置启动命令
CMD ["python", "app.py"]
4-4:也可以使用生成的镜像运行后,直接在环境里面执行如下命令,因为生成镜像运行就是默认在linux环境,再将生成的依赖库通过手动导出
pip download -r requirements.txt -d ./dependencies
4-5:如果导出的依赖库都是对的,可以修改成如下的Dockerfile去执行生成镜像
FROM python:3.11.4
WORKDIR /app
COPY ./flaskDemo/ /app
COPY ./dependencies /dependencies
RUN pip install --no-cache-dir --no-index --find-links=/dependencies -r requirements.txt
CMD ["python", "app.py"]