Deploying a PostHog App
Introduction
PostHog is an open-source product analytics platform with event capture, feature flags, and session replay. Deploying PostHog with a Dockerfile on Klutch.sh provides reproducible builds, managed secrets, and persistent storage for logs and artifacts—all configured from klutch.sh/app. This guide covers installation, repository prep, a production-ready Dockerfile, deployment steps, Nixpacks overrides, sample API usage, and production tips.
Prerequisites
- A Klutch.sh account (sign up)
- A GitHub repository containing your PostHog Dockerfile (GitHub is the only supported git source)
- External services (recommended): PostgreSQL (deploy as a Klutch.sh TCP app on port
8000, connect on5432), Redis (6379), Kafka (for ingestion), and ClickHouse (analytics database) - Domain and TLS for secure access
For onboarding, see the Quick Start.
Architecture and ports
- PostHog web/API serves HTTP on internal port
8000; choose HTTP traffic and set the internal port to8000. - Persistent storage is recommended for logs and any locally stored artifacts. Primary data resides in external Postgres/ClickHouse.
Repository layout
posthog/├── Dockerfile # Must be at repo root for auto-detection└── README.mdKeep secrets out of Git; store them in Klutch.sh environment variables.
Installation (local) and starter commands
Validate locally before pushing to GitHub (ensure Postgres/Redis/ClickHouse/Kafka are reachable):
docker build -t posthog-local .docker run -p 8000:8000 \ -e DATABASE_URL=postgres://user:pass@localhost:5432/posthog \ -e REDIS_URL=redis://localhost:6379 \ -e CLICKHOUSE_HOST=localhost \ -e CLICKHOUSE_USER=default \ -e CLICKHOUSE_PASSWORD= \ -e KAFKA_BROKER=localhost:9092 \ posthog-localDockerfile for PostHog (production-ready)
Place this Dockerfile at the repo root; Klutch.sh auto-detects it (no Docker selection in the UI):
FROM posthog/posthog:latest
ENV PORT=8000
EXPOSE 8000CMD ["bash", "-lc", "export PORT=${PORT}; /entrypoint.sh"]Notes:
- Pin the image tag (e.g.,
posthog/posthog:1.51.x) for stability; update intentionally. - The image expects external Postgres, Redis, Kafka, and ClickHouse endpoints via environment variables.
Environment variables (Klutch.sh)
Set these in Klutch.sh before deploying:
PORT=8000DATABASE_URL=postgres://<user>:<password>@<db-host>:5432/<db>REDIS_URL=redis://:<password>@<redis-host>:6379CLICKHOUSE_HOST=<clickhouse-host>CLICKHOUSE_PORT=9000CLICKHOUSE_USER=<user>CLICKHOUSE_PASSWORD=<password>KAFKA_BROKER=<kafka-host>:9092SECRET_KEY=<secure-random>- Optional:
EMAIL_HOST,EMAIL_USER,EMAIL_PASSWORD,EMAIL_PORT,DEFAULT_FROM_EMAIL
If you deploy without the Dockerfile and need Nixpacks overrides:
NIXPACKS_START_CMD=/entrypoint.shNIXPACKS_NODE_VERSION=18
Attach persistent volumes
In Klutch.sh storage settings, add mount paths and sizes (no names required) if you store logs or artifacts locally:
/var/log/posthog— logs (optional)./data— optional local artifact storage (most data lives in external DBs).
Ensure these paths are writable inside the container.
Deploy PostHog on Klutch.sh (Dockerfile workflow)
- Push your repository—with the Dockerfile at the root—to GitHub.
- Open klutch.sh/app, create a project, and add an app.
- Select HTTP traffic and set the internal port to
8000. - Add the environment variables above, including Postgres, Redis, Kafka, ClickHouse, and secret key.
- Attach persistent volumes for
/var/log/posthog(and/dataif used) sized for your retention needs. - Deploy. Your PostHog instance will be reachable at
https://example-app.klutch.sh; attach a custom domain if desired.
Sample API usage
Capture an event:
curl -X POST "https://example-app.klutch.sh/capture/" \ -H "Content-Type: application/json" \ -d '{"api_key":"<project_api_key>","event":"test_event","properties":{"source":"klutch-guide"}}'List events (replace token with personal API token):
curl -X GET "https://example-app.klutch.sh/api/event/" \ -H "Authorization: Bearer <token>"Health checks and production tips
- Add an HTTP probe to
/healthor/api/feature_flag/(simple read) for readiness. - Enforce HTTPS at the edge; forward internally to port
8000. - Keep DB, Redis, Kafka, and ClickHouse credentials plus
SECRET_KEYin Klutch.sh secrets; rotate regularly. - Monitor external service health (Kafka/ClickHouse/Postgres) and storage usage on any attached volumes; resize before they fill.
- Pin image versions and test upgrades in staging; back up databases before updates.
PostHog on Klutch.sh combines reproducible Docker builds with managed secrets, persistent storage, and flexible HTTP/TCP routing. With the Dockerfile at the repo root, port 8000 configured, and external services connected, you can deliver product analytics without extra YAML or workflow overhead.