You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Document how to launch FastAPI in a container with explicit minor version, Poetry, and distroless nonroot runtime for Cloud Run (#223)
* Initial plan
* Implement FastAPI Docker improvements with Cloud Run support and PORT env var
Co-authored-by: davideme <[email protected]>
* Fix Dockerfile to use Poetry in build stage and distroless runtime
Co-authored-by: davideme <[email protected]>
* Use nonroot distroless image for improved security
Co-authored-by: davideme <[email protected]>
* Refactor launcher.py for style and consistency
Updated string quotes to double quotes, improved command formatting for readability, and standardized the main entry point check. Removed unused subprocess import.
---------
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: davideme <[email protected]>
Co-authored-by: Davide Mendolia <[email protected]>
Co-authored-by: Davide Mendolia <[email protected]>
Copy file name to clipboardExpand all lines: src/python/README.md
+51-1Lines changed: 51 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,7 +16,7 @@ To run the server, please execute the following from the root directory:
16
16
poetry install
17
17
18
18
# Run the server
19
-
poetry run uvicorn openapi_server.main:app --host 0.0.0.0 --port 8080
19
+
poetry run fastapi run src/openapi_server/main.py --port 8080 --host 0.0.0.0
20
20
```
21
21
22
22
and open your browser at `http://localhost:8080/docs/` to see the docs.
@@ -35,6 +35,56 @@ To run the server on a Docker container, please execute the following from the r
35
35
docker-compose up --build
36
36
```
37
37
38
+
### Docker Image Features
39
+
40
+
The Docker image uses:
41
+
-**Multi-stage build**: Build stage with Poetry dependency installation, production stage with Google's distroless Python image
42
+
-**Poetry dependency management**: Uses Poetry in build stage for reproducible dependency installation
43
+
-**Distroless runtime**: Production stage uses `gcr.io/distroless/python3-debian12:nonroot` for minimal attack surface and non-root execution
44
+
-**Port Configuration**: Supports PORT environment variable for Cloud Run compatibility via launcher script
45
+
-**FastAPI CLI**: Uses the modern `fastapi run` command for production deployment
46
+
47
+
#### Building Docker Image
48
+
49
+
```bash
50
+
docker build -t lamp-control-api-python .
51
+
```
52
+
53
+
#### Running Docker Container
54
+
55
+
```bash
56
+
# Run with default port (80)
57
+
docker run -p 8080:80 lamp-control-api-python
58
+
59
+
# Run with custom port via environment variable (Cloud Run style)
60
+
docker run -p 8080:8080 -e PORT=8080 lamp-control-api-python
61
+
```
62
+
63
+
## Cloud Run Deployment
64
+
65
+
This application is optimized for Google Cloud Run deployment:
66
+
67
+
### Environment Variables
68
+
69
+
-`PORT` - The port for the HTTP server. Cloud Run automatically sets this variable for the ingress container. Defaults to 80 if not set.
70
+
71
+
### Cloud Run Notes
72
+
73
+
- The following environment variables are automatically added to all running containers except `PORT`. The `PORT` variable is only added to the ingress container.
74
+
- The application will bind to `0.0.0.0:${PORT}` to accept traffic from Cloud Run's load balancer.
75
+
- The Docker image uses a multi-stage build for optimal size and security.
0 commit comments