pyinstaller 是否有像 gcc -static 这样的参数?

big*_*ang 4 python pyinstaller docker

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

在此页面中,我看到有人说 C 应用程序用-static.

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

big*_*ang 5

来自问题Docker Minimal Image PyInstaller Binary File? 的命令,我得到了有关如何将 python 二进制文件变为静态的链接,就像 go 应用程序演示一样,从头开始说你好世界。

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

print("test")
Run Code Online (Sandbox Code Playgroud)

然后,使用 Dockerfile 进行 docker 构建:

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"]
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

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

在此输入图像描述

附:

通过我的测试

  • 必须CMD通过 ['xxx'] 写入,而不是直接 xxx 写入。

  • /tmp演示中需要目录。

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