• docker构建python3容器、压缩python镜像大小


    需求

            1、使用Docker构建一个python容器,用于运行python相关应用

            2、基于Dockerfile构建python容器

            3、镜像的体积需要尽量的小(OS和python采用最小化安装)

    Python版本:3.8.13

    OS版本:Debian GNU/Linux 11 \n \l

    解决方案

    编写Dockerfile

    这里是基于python:3.8.13-slim构建的镜像,需要切换版本可以在Docker Hub中查看python Tags | Docker Hub

     需要特别注意的地方:

    1. 不同的python的版本大小都不相同,例如有:slim-bullseye、slim、bullseye、buster、slim-buster等等的版本,每个版本的镜像大小不相同,包含的python库、工具也有所区别,例如下面从Docker Hub截图的python tag的完整python镜像和slim镜像大小差距将近300MB,各版本大致含义各位可以参考文末的补充内容。

    话不多说,开始编写Dockerfile

    vi Dockerfile

    1. FROM python:3.8.13-slim
    2. USER root
    3. ENV TZ=UTC
    4. RUN echo 'deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free' > /etc/apt/sources.list && \
    5. echo 'deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free' >> /etc/apt/sources.list && \
    6. echo 'deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free' >> /etc/apt/sources.list && \
    7. echo 'deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free' >> /etc/apt/sources.list && \
    8. ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && \
    9. apt update && \
    10. apt install -y iptables iproute2 procps
    11. COPY ./demo-python /opt/demo-python/
    12. WORKDIR /opt/demo-python
    13. RUN pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
    14. ENTRYPOINT [ "/opt/demo-python/docker-entrypoint.sh" ]

    代码解释:

    1、上述内容中RUN由多条命令组合而成:

    1. python:3.8.13-slim是基于Debian的,我们首先需要配置apt源为清华的镜像源
    2. 配置镜像的时区
    3. apt检查更新软件列表(不会实际更新软件)
    4. 通过apt安装项目中需要使用到的软件组件(我这里使用到了iptables、iproute、ps相关命令,因此安装,各位可以根据实际项目情况选择需要安装的组件)

    2、将python的程序包复制到镜像指定目录中,并指定工作目录

    3、通过pip安装python程序中所需的依赖

    4、配置镜像的切入点(我这里的切入点是一个sh程序,内容如下所示,sh中含有其他的具体命令,我这里就省略了,各位可以自行根据项目编写,如果没有额外的处理逻辑,可以不配置具体的切入点,直接通过Dockerfile CMD启动python)

    1. #!/bin/bash
    2. # 额外的代码逻辑略
    3. # main.
    4. echo '#~ demo-python starting...'
    5. python bin/app.py

    至此Dockerfile就编写完成了,在Dockerfile和demo-python的同级目录下执行:

    > docker build -t demo-python:1.0.0 .

    Tips: 注意命令最后有一个点,代表当前路径(具体docker build目录,Dockerfile构建可以自行查看相关资料)

    编写Docker Compose

    vi docker-compose.yml

    1. version: '1.0.0'
    2. services:
    3. sinkhole:
    4. container_name: demo-python
    5. image: demo-python:1.0.0
    6. restart: unless-stopped
    7. privileged: true
    8. volumes:
    9. - /opt/demo-python/config:/opt/demo-python/config
    10. - /opt/demo-python/logs:/opt/demo-python/logs
    11. cap_add:
    12. - ALL
    13. stdin_open: true
    14. tty: true
    15. healthcheck:
    16. test: ["CMD", "ps -ef|grep demo-python |grep -v grep |grep -v 'vi ' |grep -v 'vim ' |wc -l"]
    17. interval: 1m
    18. timeout: 3s # 设置检测超时时间
    19. retries: 3 # 设置重试次数
    20. start_period: 10s # 启动后,多少秒开始启动检测程序

    代码解释:

    1、image指向通过Dockerfile构建的镜像

    2、privileged 指容器使用root权限

    3、cap_add 添加或删除容器拥有的宿主机的内核功能。配置ALL指开启全部权限

    4、stdin_open、tty相当于docker run -it

    5、healthcheck 用于检测 docker 服务是否健康运行

    然后在docker-compose.yml同级目录下执行:

    > docker-compose up -d

    即可完成容器的创建和运行。

  • 相关阅读:
    JWT 使用入门(三)请求流程
    C现代方法(第8章)笔记——数组
    Qt5 Python-docx库的使用,Qt python混合编程,qt 读写word,不依赖office
    JUC并发编程(5)(自定义线程池 + 共享模型之工具2)
    什么是冲突域,什么是广播域?区别又是什么
    个人项目(玩具)列表(可能会更新)
    安卓gradle使用
    【密评】商用密码应用安全性评估从业人员考核题库(十四)
    bean的作用域解析
    iOS 添加震动效果
  • 原文地址:https://blog.csdn.net/qq_32352777/article/details/125622110