Why does FastAPI's dependency override work locally but not in production Docker containers?
0 reputation · 19 Jul 2020, 18:33 UTC
In a local development environment, a FastAPI dependency can be overridden using app.dependency_overrides[SomeDependency] = lambda: mock_service to inject a test double. When the same application is built as a Docker image and run in production, the override appears to be ignored and the real dependency is used instead. The service is launched with Uvicorn via a Dockerfile CMD, and configuration profiles are selected through environment variables.
What configuration or lifecycle differences prevent the dependency override from being applied in the production container? Are there patterns for conditionally applying overrides based on the deployment environment without modifying application code? How can one verify that an override is active in a running container without changing the service logic?