python-3.xdockerfiledistroless

Error with Python3 on distroless image [No such file]


I'm using this dockerfile with flask :

FROM python:3.8-slim-buster

WORKDIR /python-docker

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

COPY . .

CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]

It works fine, in my directory I have requirements.txt and a very simple app.py file :

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_geek():
    return '<h1>Hello from Flask & Docker</h2>'


if __name__ == "__main__":
    app.run(debug=True)

I'd like to use it as a distroless image, so I build on this dockerfile :

FROM python:3.8-slim-buster as build-env

ADD / /app
WORKDIR /app

RUN pip3 install --upgrade pip
RUN pip install -r ./requirements.txt

FROM gcr.io/distroless/python3-debian10

COPY --from=build-env /app /app
COPY --from=build-env /usr/local/lib/python3.8/site-packages /usr/local/lib/python3.8/site-packages
WORKDIR /app

ENV PYTHONPATH=/usr/local/lib/python3.8/site-packages

CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]

No error at build time, but when I run : sudo docker run python-dockerdistro

I get that error : /usr/bin/python3.7: can't open file 'python3': [Errno 2] No such file or directory

Do you have an idea of how resolve that error ? Thank you very much in advance


Solution

  • The ENTRYPOINT for the gcr.io/distroless/python3-debian10 image is Python3:

    $ docker image inspect -f '{{.Config.Entrypoint}}' gcr.io/distroless/python3-debian10
    [/usr/bin/python3.7]
    
    $ docker run --rm -it gcr.io/distroless/python3-debian10
    Python 3.7.3 (default, Jan 22 2021, 20:04:44) 
    [GCC 8.3.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    

    However, your CMD is [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]

    You are essentially trying to run Python twice:

    $ python3 python3
    python3: can't open file '/tmp/python3': [Errno 2] No such file or directory
    

    Simply alter your CMD to only ["-m" , "flask", "run", "--host=0.0.0.0"]