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

相关推荐
one year.17 小时前
Linux:库制作与原理
linux·运维·服务器
查士丁尼·绵17 小时前
笔试-精准核酸检测
python
我命由我1234518 小时前
PDFBox - PDFBox 加载 PDF 异常清单(数据为 null、数据为空、数据异常、文件为 null、文件不存在、文件异常)
java·服务器·后端·java-ee·pdf·intellij-idea·intellij idea
蓝色土耳其love18 小时前
centos 7.9 安装单机版k8s
linux·运维·服务器·kubernetes·centos
tokepson18 小时前
记录 | 个人开发库推送至PyPi流程梳理(ChatGPT to Markdown 工具发布完整流程)
python·github·技术·记录
道之极万物灭18 小时前
Python操作word实战
开发语言·python·word
郝学胜-神的一滴19 小时前
使用Linux系统函数递归遍历指定目录
linux·运维·服务器·开发语言·c++·软件工程
Python私教19 小时前
DRF:Django REST Framework框架介绍
后端·python·django
pusue_the_sun19 小时前
操作系统:进程的短程调度
运维·服务器