Reporting a python fastAPI bug

How to reproduce:

  1. Use a MacBook Pro M2 (or possibly other models).

  2. Clone the example project from here:

  1. Run poetry install first:
   poetry install
  1. Then, use fly launch
    You’ll see logs like the following on your deployed machine:

2024-07-11T07:41:07.742 proxy[e7843279ae7338] iad [info] Starting machine

2024-07-11T07:41:07.823 app[e7843279ae7338] iad [info] 2024-07-11T07:41:07.823707911 [01J2GBK36MXY9B2V375AT0MRJA:main] Running Firecracker v1.7.0

2024-07-11T07:41:08.186 app[e7843279ae7338] iad [info] [ 0.303794] PCI: Fatal: No config space access function found

2024-07-11T07:41:08.578 app[e7843279ae7338] iad [info] INFO Starting init (commit: ad092ccf)...

2024-07-11T07:41:08.628 app[e7843279ae7338] iad [info] INFO Preparing to run: `/app/.venv/bin/fastapi run` as root

2024-07-11T07:41:08.632 app[e7843279ae7338] iad [info] ERROR Error: failed to spawn command: /app/.venv/bin/fastapi run: No such file or directory (os error 2)

2024-07-11T07:41:08.633 app[e7843279ae7338] iad [info] does `/app/.venv/bin/fastapi` exist and is it executable?

2024-07-11T07:41:08.633 app[e7843279ae7338] iad [info] [ 0.752591] reboot: Restarting system

2024-07-11T07:41:08.733 app[e7843279ae7338] iad [warn] Virtual machine exited abruptly

2024-07-11T07:41:08.789 runner[e7843279ae7338] iad [info] machine exited with exit code 0, not restarting

2024-07-11T07:41:10.155 proxy[e7843279ae7338] iad [error] [PM01] machines API returned an error: "machine exited abruptly"

Why it happened?

If the user runs poetry install first, your default Dockerfile will copy everything, including Poetry’s .venv folder, into the Docker image without the correct permissions or platform architecture. This skips the poetry install step in the Dockerfile and results in /app/.venv/bin/fastapi being unable to run on the deployed machine.

FROM python:3.12 AS builder

ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1

WORKDIR /app
COPY . .

RUN pip install poetry
RUN poetry config virtualenvs.in-project true
RUN poetry install

FROM python:3.12-slim

WORKDIR /app
COPY --from=builder /app .

CMD ["/app/.venv/bin/fastapi", "run"]

How to fix

Just add .venv in your template .dockerignore

fly.toml
.git/
__pycache__/
.envrc
.venv
.idea/
.vscode/

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.