A Pygame application that can run both as a desktop app and in the browser via pygbag, with containerized deployment support using Docker and Kubernetes.
PyGame-Docker/
├── main.py # Main Pygame application
├── serve.py # Development server with CORS and CDN proxy support
├── Dockerfile # Container configuration using Nginx
├── k8s-deployment.yaml # Kubernetes deployment and service configuration
├── nginx.conf # Nginx configuration for container deployment
└── build/
├── web/ # Pygbag web build output
│ ├── index.html
│ ├── pygame-docker.apk
│ └── favicon.png
└── web-cache/ # Cached assets
- Python 3.7+
- pygame
- pygbag (for web builds)
- Docker (for containerized deployment)
- Kubernetes (for cluster deployment)
First, generate the web build using pygbag:
# Create a virtual environment (recommended)
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install pygame pygbag
# Build for web (generates build/web/ directory)
python -m pygbag .The Docker setup uses Nginx to serve the pygbag build with proper CORS headers and CDN proxying:
# Build the Docker image
docker build -t acteus/pygame-web:latest .
# Run locally
docker run -p 8081:80 acteus/pygame-web:latest
# Push to registry (if needed)
docker push acteus/pygame-web:latestAccess at: http://localhost:8081
Deploy to a Kubernetes cluster:
# Apply the deployment
kubectl apply -f k8s-deployment.yaml
# Check deployment status
kubectl get pods -l app=pygame-web
# Access via NodePort (default: 30080)
# Or set up port forwarding:
kubectl port-forward svc/pygame-web-service 8080:80The Kubernetes deployment includes:
- 3 replicas for high availability
- Resource limits and requests
- NodePort service for external access
Use the custom Python server with CORS and CDN proxy support:
python3 serve.pyAccess at: http://localhost:8000
Basic local serving (may have CORS issues):
cd build/web
python3 -m http.server 8000Run directly as a desktop Pygame app:
pip install pygame
python3 main.pyIf you see network errors in the browser console, check for:
pygame-docker.apk(should be served from build/web)- CDN assets from
https://pygame-web.github.io/archives/0.9/
If corporate networks block the CDN, use the Docker deployment or local development server which includes CDN proxying.
- Click the page once if it says "waiting for media user engagement"
- Ensure the browser supports WebAssembly
- Use HTTPS in production environments
The nginx.conf file includes:
- CORS headers for cross-origin requests
- Proxy settings for CDN assets
- Gzip compression for better performance
The k8s-deployment.yaml includes:
- Horizontal scaling (3 replicas)
- Resource management (CPU/memory limits)
- NodePort service for external access
The application is built with async/await support for web compatibility:
async def main():
# Game loop with async support
while running:
await asyncio.sleep(0) # Yield control for webThis ensures the game runs smoothly in both desktop and web environments.## Notes
- The Docker setup uses Nginx to serve static files and proxy CDN requests with proper CORS headers
- The web build expects
index.htmlandpygame-docker.apkto live together inbuild/web/ - Click the page once if it says it's waiting for media user engagementb build exported by pygbag.
The Docker setup includes proper CORS handling and CDN proxying:
# Build the Docker image
docker build -t pygame-web .
# Run the container
docker run -p 8081:80 pygame-webThen open: http://localhost:8081
Browsers block fetch() from file://. Serve the build/web folder over HTTP:
# From the repo root
cd build/web
python3 -m http.server 8000Then open:
If you still see a network error, open DevTools → Network and check which request failed. Common ones:
pygame-docker.apk(should be served locally from build/web)https://pygame-web.github.io/archives/0.9/pythons.jshttps://pygame-web.github.io/archives/0.9/browserfs.min.js
If corporate network blocks the CDN, try another network or host those assets locally.
python3 -m pip install pygame
python3 main.py- The web build expects
index.htmlandpygame-docker.apkto live together inbuild/web/. - Click the page once if it says it’s waiting for media user engagement.