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

2024-03-29

我有一个类似的问题:有没有办法将 Python 程序编译为二进制并将其与 Scratch Dockerfile 一起使用 https://stackoverflow.com/questions/62581924/is-there-a-way-to-compile-a-python-program-to-binary-and-use-it-with-a-scratch-d?

In this page https://hub.docker.com/_/scratch/?tab=reviews,我看到有人说C应用程序用编译时运行得很好-static.

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


从问题来看Docker 最小映像 PyInstaller 二进制文件? https://stackoverflow.com/questions/61262156/docker-minimal-image-pyinstaller-binary-file?noredirect=1#comment111416388_61262156的命令,我得到了links https://github.com/pyinstaller/pyinstaller/wiki/FAQ#gnulinux关于如何将python二进制文件变成静态文件,就像go应用程序演示一样,从头开始说你好世界。

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

print("test")

然后,使用 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"]

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

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

PS:

通过我的测试

  • the CMD必须用['xxx']写,而不是直接写xxx。

  • /tmp演示中需要目录。

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

pyinstaller 是否有像 gcc -static 这样的参数? 的相关文章

随机推荐