Upload 2 files
Browse filesFiles changed (2)
- Dockerfile+17-5
- web_app.py+6
12345678910111213141516171819201213142122232425262716172829303132# Use a lightweight Python imageFROM python:3.9-slim
# Set environment variables# PYTHONUNBUFFERED: Forces stdout/stderr to be flushed immediately (critical for logs)# PYTHONDONTWRITEBYTECODE: Prevents Python from writing .pyc filesENV PYTHONUNBUFFERED=1 \ PYTHONDONTWRITEBYTECODE=1
# Install pngquant, jpegoptim and zipRUN apt-get update && \ apt-get install -y pngquant jpegoptim zip && \ rm -rf /var/lib/apt/lists/*
# Create a non-root user (Security Best Practice for HF Spaces)RUN useradd -m -u 1000 user
# Set working directoryWORKDIR /app
# Copy application filesCOPY web_app.py .COPY index.html .# Copy application files with correct ownershipCOPY --chown=user:user web_app.py .COPY --chown=user:user index.html .
# Switch to the non-root userUSER user
# Expose the port (optional documentation)EXPOSE 8989# Expose the port (Hugging Face Spaces defaults to 7860)EXPOSE 7860
# Start the applicationCMD ["python", "web_app.py"]221 unmodified lines222223224225226227228229230231232233234235236237238221 unmodified lines
def run(): print(f"🐼 TinyPNG Local Server starting at port {PORT}") # Allow address reuse to prevent "Address already in use" errors on restart socketserver.TCPServer.allow_reuse_address = True if not PNGQUANT_PATH: print("⚠️ Warning: pngquant not found. PNG compression will be disabled.") if not JPEGOPTIM_PATH: print("⚠️ Warning: jpegoptim not found. JPG compression will be disabled.") # Ensure logs are flushed immediately sys.stdout.flush() with socketserver.TCPServer(("0.0.0.0", PORT), TinyHandler) as httpd: try: httpd.serve_forever()Dockerfile+17-5
web_app.py+6