Search code examples
pythondocker

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


I've been playing around with docker images recently. I saw this docker doc on using FROM scratch. I am trying to see how far I can take this just for fun. I program in python. The doc says to compile the example C program to a binary, copy it to the container and then run it. In the container, I can not run python <program_file>. I saw this stack exchange post about compiling a python file to a binary, which meets our test use case here. It mentions using pyinstaller. So I run it on a test hello.py file which justs prints Hello with pyinstaller hello.py and I get a bunch of messages about building the projects. Okay, good. I can run the binary in my local machine by running "dist/hello" (this is the binary program mentioned by the post. So I write my Dockerfile to copy this program over and run it. My Dockerfile is

FROM scratch

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

I run docker build . -t "hello:1.0" and then docker run hello:1.0 and.... I get an error messgage:

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

What gives? What did I go wrong? Is it possible to get pyinstaller to compile a binary python project (multiple files, instead of just this one), then use the scratch image to run it. Are there any caveats if this is possible?


Solution

  • So the answer is to use Google's distroless image. They have an example on their github that I have slightly edited as follows.:

    # 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"]
    

    Just posting this in case someone is wondering. Definitely a cool thing.

    Link to google distroless image

    The video that helped me a lot