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

相关推荐
明月_清风2 小时前
Python 内存手术刀:sys.getrefcount 与引用计数的生死时速
后端·python
明月_清风2 小时前
Python 消失的内存:为什么 list=[] 是新手最容易踩的“毒苹果”?
后端·python
Sheffield16 小时前
Docker的跨主机服务与其对应的优缺点
linux·网络协议·docker
Flittly16 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(3)TodoWrite (待办写入)
python·agent
千寻girling20 小时前
一份不可多得的 《 Django 》 零基础入门教程
后端·python·面试
Sheffield1 天前
Alpine是什么,为什么是Docker首选?
linux·docker·容器
databook1 天前
探索视觉的边界:用 Manim 重现有趣的知觉错觉
python·动效
马艳泽1 天前
win10下运行Start Broker and Proxy报错解决
docker
明月_清风1 天前
Python 性能微观世界:列表推导式 vs for 循环
后端·python
明月_清风1 天前
Python 性能翻身仗:从 O(n) 到 O(1) 的工程实践
后端·python