Python项目Docker服务器部署

Python项目Docker服务器部署

Python项目Docker服务器部署

准备工作

1.准备基础镜像

shell 复制代码
# 指定拉取arm架构镜像
docker pull --platform linux/arm64 python:3.11
# 指定拉取amd架构镜像
docker pull --platform linux/amd64 python:3.11

# 保存基础镜像
docker save -o your-image-name.tar python:3.11

2.准备代码

shell 复制代码
# python 项目 requirements.txt 生成
pip freeze > requirements.txt

3.Dockerfile文件样例

shell 复制代码
FROM python:3.11
WORKDIR /app

# 安装依赖
RUN echo 'Asia/Shanghai' >/etc/timezone
COPY requirements.txt .
RUN pip install --progress-bar off --no-cache-dir -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

# 打包字体 (无使用字体时 可跳过)
#COPY ./zsl0_fonts /usr/share/fonts/zsl0_fonts
#RUN cd /usr/share/fonts/zsl0_fonts && fc-cache

# 打包代码(注释:采用运行时挂载代码目录的方式出现在容器中)
#COPY ./code /app

CMD ["tail -f /dev/null"]

(纯内网环境)需要提前构建好镜像,并下载相关依赖

shell 复制代码
# 构建镜像
docker build -t qsh-python:v0.0.1 .

# 保存镜像
docker save -o your-image-name.tar qsh-python:v0.0.1

4.上述文件打包好,copy到服务器

部署

1.加载镜像

shell 复制代码
docker load -i your-image-name.tar

2.构建项目镜像(若是已构建好的python环境,则跳过)

shell 复制代码
docker build -t qsh-python:v0.0.1 .

3.运行容器

shell 复制代码
docker run -d --name qsh-python \
--privileged=true \
-v /home/datacenter_zsl0/qsh-python/code:/app \
-v /ossfs/Files/data_throw/:/ossfs/Files/data_throw/  \
qsh-python:v0.0.1 tail -f /dev/null

4.配置crontab定时

其他问题

1.出现docker build构建镜像时,出现报错导致构建失败:

可将Dockerfile中RUN pip install注释掉,直接docker exec运行容器,在容器内手动执行pip install下载依赖,出现问题时逐个攻破,最终是一个正常python环境的容器,再构建容器。(注:内网环境时,需要在有网环境进行此操作)

相关推荐
呱呱复呱呱1 小时前
Django CBV 源码解读:一个请求是怎么找到你的 get() 方法的
python·django
WangMingHua1112 小时前
LM Studio Docker 部署——本地大模型一键启动
docker
曲幽6 小时前
刚部署的 LibreTranslate 频频翻车?我掏出了 20 年前的 StarDict 词典,用 FastAPI 搭了个本地词典翻译 API
python·fastapi·web·translate·goldendict·libretranslate·stardict·pystardict
荣码6 小时前
用Streamlit给AI应用套个界面,10行代码出Web页面
java·python
兵慌码乱16 小时前
基于Python+PyQt5+SQLite的药房管理系统实现:事务一致性与界面解耦全流程解析
python·sqlite·信号与槽·pyqt5·数据库设计·桌面应用开发·事务处理
金銀銅鐵17 小时前
[Python] 体验用欧几里得算法计算最大公约数的过程
python·数学
FreakStudio21 小时前
W55MH32L-EVB 上手测评:硬件 TCP/IP 加持的以太网单片机,MicroPython 零门槛开发
python·单片机·嵌入式·大学生·面向对象·并行计算·电子diy·电子计算机
用户0332126663671 天前
使用 Python 从零创建 Word 文档
python
Csvn1 天前
Python 两大经典坑点 —— 可变默认参数 & 闭包延迟绑定
后端·python
曲幽1 天前
别再用网页翻译看源码了!你的私人翻译神器LibreTranslate,部署避坑指南来了
python·docker·web·pot·translate·libretranslate·arogstranslate