-1

I'm experimenting with distroless Docker images from Google and I'd like to build an elegant FastAPI (or Flask) web API. I've found example articles here and here but I didn't find them particularly "pythonic".

This is my main.py

from fastapi import FastAPI

app = FastAPI()

@app.get('/')
async def root():
    return {'message': 'Hello World'}

requirements.txt

fastapi==0.95.0
uvicorn==0.21.1

and Dockerfile

FROM python:3.9.16-slim as build

WORKDIR /app
RUN python -m venv /app/venv
ENV PATH="/app/venv/bin:$PATH"

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

FROM gcr.io/distroless/python3

WORKDIR /app

COPY --from=build /app/venv ./venv
COPY ./main.py .

ENV PATH="/app/venv/bin:$PATH"
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8080"]

Building and running the image

  • docker build -t webapp .
  • docker run --rm -it -p 8080:8080 webapp

results in /usr/bin/python3.9: can't open file '/app/uvicorn': [Errno 2] No such file or directory

I don't quite understand why the uvicorn is not found and whether there is a more pythonic way of doing this?

Patimir
  • 19
  • 5

1 Answers1

0

I'm not sure but you venv is not activated. So the binary uvicorn is not found.

You can delete your venv and use the docker container without it.

Or use CMD like:

/app/venv/bin/activate & uvicorn main:app --host 0.0.0.0