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

相关推荐
ZHW_AI课题组20 分钟前
腾讯云调用IP定位
人工智能·python·机器学习
zhaoshuzhaoshu37 分钟前
Python文件操作详细解析带例子
python
醒醒该学习了!38 分钟前
Anaconda安装教程+第一个python例子
开发语言·python
linyanRPA1 小时前
影刀RPA+Python店群自动化实战:自研环境隔离引擎,200店铺并发不卡不串号
python·自动化·rpa
茫忙然1 小时前
U 盘搭建免驱 Linux 便携系统教程
linux·服务器
lihao lihao2 小时前
linux匿名管道
linux·运维·服务器
STDD2 小时前
Glances:跨平台系统资源监控,浏览器实时查看服务器状态
运维·服务器
郑洁文3 小时前
面向Web安全的Python渗透测试系统设计与实现
python·安全·web安全
情绪总是阴雨天~3 小时前
智能语音分析Agent项目
python·自动化·fastapi·langgraph