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环境的容器,再构建容器。(注:内网环境时,需要在有网环境进行此操作)

相关推荐
SpikeKing9 分钟前
LLM - 集成 Hermes Agent 与 WebUI 至同一个 Docker 镜像配置
docker·webui·vibecoding·hermes agent
Oo92019 分钟前
Prompt工程核心与Python 字典
python·ai编程
大明者省22 分钟前
IIS 端口绑定正常访问的原理说明与常见误区澄清
运维·服务器·笔记
tedcloud12324 分钟前
ai-engineering-from-scratch部署教程:从零搭建AI应用环境
服务器·前端·人工智能·系统架构·edge
feeday29 分钟前
gpt4o 图像反推提示词
开发语言·人工智能·python
mounter62538 分钟前
迈向硬件级无缝热升级:Linux 内核 VFIO 与 IOMMU 持久化技术的演进之路
linux·服务器·内存管理·kernel
晚风吹红霞39 分钟前
Linux软件包管理器详解 —— yum与apt的使用及软件生态
linux·运维·服务器
沈浩(种子思维作者)1 小时前
没有错误,正确将一文不值
人工智能·python·算法·量子计算
smith成长之旅1 小时前
06 | Mem0 框架分析:为什么要从记忆中提取实体?——Entity Store 的设计动机与工程实现
人工智能·python
smith成长之旅1 小时前
07 | Mem0 框架分析:三路信号融合——语义 + BM25 + Entity Boost 的混合检索
python·算法