docker多阶段构建并编译python项目为so(保护源码)

项目结构

src 为源码路径

project_name:
./Dockerfile-compile
./compile.py
./src

compile脚本

# -*- coding: utf-8 -*-
# @Author  : llc
# @Time    : 2021/11/15 15:13
import os

from Cython.Build import cythonize
from setuptools import setup, Extension


def c_compile(name, modules):
    """
    使用Cython进行编译
    :param str name: 项目名称
    :param list modules: [(module_name, source_file_path), (..., ...)]
    :return:
    """
    module_list = []
    for m in modules:
        module_list.append(
            Extension(name=m[0], sources=[m[1]])
        )
    setup(
        name=name,
        ext_modules=cythonize(
            module_list=module_list,
            language_level=3,  # python3
        ),
        zip_safe=False,
        install_requires=[
            'Cython',
        ]
    )


modules = []
# 遍历找到需要编译的py文件
for root, dirs, files in os.walk('./src'):
    for file in files:
        if not file.endswith('.py'):
            continue
        dir_list = root.split(os.path.sep)
        name = '.'.join(dir_list).lstrip('./')
        modules.append((name + '.' + os.path.splitext(file)[0], os.path.join(root, file)))

print(modules)
# 开始编译
c_compile('compile', modules)
# 删除py文件和c文件
for root, dirs, files in os.walk('./src'):
    for file in files:
        if file.endswith('.so'):
            continue
        file_path = os.path.join(root, file)
        os.remove(file_path)

Dockerfile-compile

FROM intellegensdocker/cython:3.9 as compile

COPY compile.py /tmp/compile.py
COPY src /tmp/src
# 执行编译程序
RUN cd /tmp && python compile.py build_ext --inplace -j8


FROM python:3.9-slim-bullseye

MAINTAINER LLC

# 使用上海时区
ENV TZ=Asia/Shanghai

# 拷贝依赖包文件
COPY requirements.txt /tmp/requirements.txt

# 基础环境安装
RUN \
    echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free" > /etc/apt/sources.list && \
    echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free">> /etc/apt/sources.list  && \
    echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free">> /etc/apt/sources.list  && \
    echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free">> /etc/apt/sources.list  && \
    \
    apt-get update && \
    apt-get install -y gcc python3-dev --no-install-recommends && \
    \
    python -m pip install -U pip && \
    python -m pip install -r /tmp/requirements.txt && \
    python -m pip install supervisor uwsgi && \
    \
    echo_supervisord_conf > /etc/supervisord.conf && \
    echo "[include]" >> /etc/supervisord.conf && \
    echo "files = /etc/supervisord.d/*.ini" >> /etc/supervisord.conf && \
    \
    apt-get purge -y gcc  python3-dev && \
    apt-get autoremove -y && \
    rm -rf /var/lib/apt/lists/* && \
    rm -rf ~/.cache/pip/*


# 工作空间
WORKDIR /work/src

# 添加pythonpath
ENV PYTHONPATH=/work/src

# 程序部署
COPY conf/uwsgi.ini /work/conf/uwsgi.ini
COPY conf/supervisor.ini /etc/supervisord.d/supervisor.ini
# 拷贝编译后的结果
COPY --from=compile /tmp/src /work/src


ENTRYPOINT ["supervisord", "-n","-c", "/etc/supervisord.conf"]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

llc的足迹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值