[Python] Is there an issue with starting a FastAPI backend within Docker using uv run main.py?

Discussão em 'Python' iniciado por Stack, Agosto 11, 2025.

  1. Stack

    Stack Membro Participativo

    I'm setting up a FastAPI backend with Docker on my machine (will be deployed to Azure later as well) right now. All examples I have seen including the official uv docker example start the backend like this in Docker:

    FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim
    # ...
    CMD ["fastapi", "dev", "--host", "0.0.0.0", "src/uv_docker_example"]


    They use the fastapi cli to start the backend with the starting configuration being specified within Docker.

    However I use variables for my starting configuration that I effectively get from a .env file (which gets validated using pydantic-settings first)

    # ./main.py
    import uvicorn
    from fastapi import FastAPI
    # ...
    if __name__ == "__main__":
    uvicorn.run("main:app", host="0.0.0.0", port=config.fastapi_port, workers=config.workers,
    log_level="info", reload=True)


    I therefore tried to start the backend in docker the same way by invoking the main.py file itself. This way all .env variables get read and validated first and then the startup config gets filled dynamically.

    FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim
    # ...
    CMD ["uv", "run", "main.py"]


    This seems to work fine at first glance. At least I could not find anything wrong with it. However since I did not see anyone else doing it like this I'd like to know if there is any major downside of doing it this way that I'm missing right now? In theory this should also work on properly on Azure since I pass the env variables using the Azure Environment Variables page in the web app configuration but I'm not entirely sure. Can someone please clarify this?

    Continue reading...

Compartilhe esta Página