首页 > 解决方案 > pyinstaller 有 gcc -static 之类的参数吗?

问题描述

我有一个类似的问题:有没有办法将 Python 程序编译为二进制文件并将其与 Scratch Dockerfile 一起使用

在这个页面中,我看到有人说 C 应用程序在编译时运行良好-static

所以我有一个新问题:是否pyinstaller有任何参数gcc -static可以让 python 应用程序在 Scratch Docker 映像中运行良好?

标签: pythondockerpyinstaller

解决方案


从问题Docker Minimal Image PyInstaller Binary File? 的命令,我得到了关于如何将 python 二进制转换为静态的链接,就像 go 应用程序演示,从头开始问好世界。

我做了一个简单的演示,app.py:

print("test")

然后,使用 Dockerfile 进行 docker build:

FROM bigpangl/python:3.6-slim AS complier
WORKDIR /app
COPY app.py ./app.py

RUN apt-get update \
    && apt-get install -y build-essential patchelf \
    && pip install staticx pyinstaller \ 
    && pyinstaller -F app.py \
    && staticx /app/dist/app  /tu2k1ed

FROM scratch
WORKDIR /
COPY --from=complier /tu2k1ed /
COPY --from=complier /tmp /tmp
CMD ["/tu2k1ed"]

得到下面的图片,只有 7.22M(我不确定是否能看到图片):

在此处输入图像描述

尝试通过代码运行docker run test,成功:

在此处输入图像描述

PS:

通过我的测试

  • 必须CMD由 ['xxx'] 而非 xxx 直接写入。

  • /tmp演示中需要目录。

  • 其他 python 应用程序未测试,仅包含有关打印的演示代码


推荐阅读