Skip to content

A simple PyGame deployed on Docker that can be accessed locally by multiple devices

Notifications You must be signed in to change notification settings

Acteus/PyGame-Docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PyGame-Docker

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.

Project Structure

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

Prerequisites

  • Python 3.7+
  • pygame
  • pygbag (for web builds)
  • Docker (for containerized deployment)
  • Kubernetes (for cluster deployment)

Building the Web Version

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 .

Deployment Options

1. Docker Deployment (Recommended)

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:latest

Access at: http://localhost:8081

2. Kubernetes Deployment

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:80

The Kubernetes deployment includes:

  • 3 replicas for high availability
  • Resource limits and requests
  • NodePort service for external access

3. Local Development Server

Use the custom Python server with CORS and CDN proxy support:

python3 serve.py

Access at: http://localhost:8000

4. Simple HTTP Server

Basic local serving (may have CORS issues):

cd build/web
python3 -m http.server 8000

5. Desktop Application

Run directly as a desktop Pygame app:

pip install pygame
python3 main.py

Troubleshooting

Network Issues

If 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.

Browser Compatibility

  • Click the page once if it says "waiting for media user engagement"
  • Ensure the browser supports WebAssembly
  • Use HTTPS in production environments

Configuration

Nginx Configuration

The nginx.conf file includes:

  • CORS headers for cross-origin requests
  • Proxy settings for CDN assets
  • Gzip compression for better performance

Kubernetes Configuration

The k8s-deployment.yaml includes:

  • Horizontal scaling (3 replicas)
  • Resource management (CPU/memory limits)
  • NodePort service for external access

Development

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 web

This 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.html and pygame-docker.apk to live together in build/web/
  • Click the page once if it says it's waiting for media user engagementb build exported by pygbag.

Run with Docker (Recommended)

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-web

Then open: http://localhost:8081

Run the web build locally (Alternative)

Browsers block fetch() from file://. Serve the build/web folder over HTTP:

# From the repo root
cd build/web
python3 -m http.server 8000

Then 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.js
  • https://pygame-web.github.io/archives/0.9/browserfs.min.js

If corporate network blocks the CDN, try another network or host those assets locally.

Run the desktop app

python3 -m pip install pygame
python3 main.py

Notes

  • The web build expects index.html and pygame-docker.apk to live together in build/web/.
  • Click the page once if it says it’s waiting for media user engagement.

About

A simple PyGame deployed on Docker that can be accessed locally by multiple devices

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published