簡體   English   中英

有沒有辦法將 Python 程序編譯為二進制並將其與 Scratch Dockerfile 一起使用?

[英]Is there a way to compile a Python program to binary and use it with a Scratch Dockerfile?

我最近一直在玩 docker 圖像。 我在使用FROM scratch時看到了這個 docker 文檔 我想看看我能走多遠只是為了好玩。 我在 python 中編程。 該文檔說要將示例 C 程序編譯為二進制文件,將其復制到容器中,然后運行它。 在容器中,我無法運行python <program_file>. 我看到了這篇關於將 python 文件編譯為二進制文件的堆棧交換帖子,這符合我們的測試用例。 它提到使用pyinstaller 因此,我在一個測試hello.py文件上運行它,該文件使用pyinstaller hello.py hello.py 打印 Hello,我收到一堆關於構建項目的消息。 好的。 我可以通過運行“dist/hello”在我的本地機器上運行二進制文件(這是帖子中提到的二進制程序。所以我寫了我的 Dockerfile 來復制這個程序並運行它。我的 Dockerfile 是

FROM scratch

ADD dist/hello /
CMD ["./hello"]

我運行docker build. -t "hello:1.0" docker build. -t "hello:1.0"然后docker run hello:1.0和....我收到一條錯誤消息:

standard_init_linux.go:211: exec user process caused "no such file or directory"

是什么賦予了? 我 go 做錯了什么? 是否可以讓 pyinstaller 編譯二進制 python 項目(多個文件,而不僅僅是這個),然后使用臨時映像運行它。 如果可能,是否有任何警告?

所以答案是使用 Google 的 distroless 鏡像。 他們在他們的 github 上有一個例子,我稍微編輯如下:

# Build a virtualenv using the appropriate Debian release
# * Install python3-venv for the built-in Python3 venv module (not installed by default)
# * Install gcc libpython3-dev to compile C Python modules
# * Update pip to support bdist_wheel
FROM debian:buster-slim AS build
RUN apt-get update && \
    apt-get install --no-install-suggests --no-install-recommends --yes python3-venv gcc libpython3-dev && \
    python3 -m venv /venv && \
    /venv/bin/pip install --upgrade pip

# Build the virtualenv as a separate step: Only re-execute this step when requirements.txt changes
FROM build AS build-venv
COPY requirements.txt /requirements.txt
RUN /venv/bin/pip install --disable-pip-version-check -r /requirements.txt

# Copy the virtualenv into a distroless image
FROM gcr.io/distroless/python3-debian10
COPY --from=build-venv /venv /venv
COPY . /app
WORKDIR /app
ENTRYPOINT ["/venv/bin/python3", "hello.py"]

只是發布這個以防有人想知道。 絕對是一件很酷的事情。

鏈接到 google distroless 圖片

對我幫助很大的視頻

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM