<@ULVA73B9P> what is prefect? tell me about it in ...
# ask-marvin
k
@Marvin what is prefect? tell me about it in 2 sentences
m
thought for 7.4 seconds
Prefect is an open-source workflow orchestration and observability platform that helps you build, run, and monitor reliable data and ML pipelines in Python. It provides scheduling, retries, parametrization, deployments, and a UI/Cloud to manage flows across your infrastructure, integrating with tools like Docker, Kubernetes, and major cloud services (Prefect, Docs).
k
@Marvin how do I setup prefect server locally in docker compose?
m
thought for 1021.2 seconds
Great question! Assuming Prefect 3.x, here’s a minimal, working Docker Compose setup for a local Prefect Server with PostgreSQL (plus the required pg_trgm extension), along with how to point your CLI at it. 1) docker-compose.yml
Copy code
services:
  postgres:
    image: postgres:16
    container_name: prefect-postgres
    environment:
      POSTGRES_USER: prefect
      POSTGRES_PASSWORD: prefect
      POSTGRES_DB: prefect
    volumes:
      - prefect-postgres-data:/var/lib/postgresql/data
      # Creates pg_trgm on first init of the database
      - ./postgres/init.sql:/docker-entrypoint-initdb.d/init.sql:ro
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U prefect -d prefect"]
      interval: 10s
      timeout: 5s
      retries: 10
    restart: unless-stopped

  prefect:
    image: prefecthq/prefect:3
    container_name: prefect-server
    environment:
      # Prefect 3 uses asyncpg with Postgres
      PREFECT_API_DATABASE_CONNECTION_URL: <postgresql+asyncpg://prefect:prefect@postgres:5432/prefect>
      # Bind the API/UI to all interfaces so it’s reachable from your host
      PREFECT_SERVER_API_HOST: 0.0.0.0
    command: >
      bash -lc "prefect server start --host 0.0.0.0 --port 4200"
    ports:
      - "4200:4200"
    depends_on:
      postgres:
        condition: service_healthy
    restart: unless-stopped

volumes:
  prefect-postgres-data:
2) postgres/init.sql (ensures pg_trgm is enabled)
Copy code
CREATE EXTENSION IF NOT EXISTS pg_trgm;
Notes - Redis is optional in Prefect 3; the server uses an in-memory broker by default. You only need Redis for advanced/high-availability setups. - Pin the Prefect image to a specific 3.x tag for reproducibility (e.g., prefecthq/prefect:3.x.y-python3.12) if you prefer. Start and verify - Start:
docker compose up -d
- UI: open http://localhost:4200 - Health:
curl <http://localhost:4200/api/health>
Point your Prefect CLI at the local server
Copy code
prefect config set PREFECT_API_URL="<http://127.0.0.1:4200/api>"
Optional: run flows with a worker - Create a work pool:
prefect work-pool create "local-pool" --type process
- Start a worker that polls it:
Copy code
prefect worker start -p "local-pool"
Cleanup - Stop:
docker compose down
- Stop and remove data:
docker compose down -v
(drops the DB volume) Docs and references - Self-hosted with Docker Compose: Prefect 3 Docker Compose guide - Server concepts (incl. pg_trgm requirement): Prefect Server concepts - Settings reference: Settings reference If you’re on Prefect 2.x instead, let me know and I’ll adapt the compose file and steps accordingly.