diff --git a/.env.template b/.env.template index c922fe54..ed456e91 100644 --- a/.env.template +++ b/.env.template @@ -32,7 +32,7 @@ LOG_LEVEL=INFO # ============================================================================= # Connection URI for PostgreSQL database with pgvector support # Must use postgresql+psycopg prefix for SQLAlchemy compatibility -DB_CONNECTION_URI=postgresql+psycopg://testuser:testpwd@localhost:5432/honcho +DB_CONNECTION_URI=postgresql+psycopg://postgres:postgres@localhost:5432/postgres # Optional database settings # DB_SCHEMA=public diff --git a/Dockerfile b/Dockerfile index 30a8d09b..4a68d617 100644 --- a/Dockerfile +++ b/Dockerfile @@ -32,9 +32,11 @@ RUN --mount=type=cache,target=/root/.cache/uv \ # Place executables in the environment at the front of the path ENV PATH="/app/.venv/bin:$PATH" +ENV HOME=/app +ENV UV_CACHE_DIR=/tmp/uv-cache # Create non-root user and set ownership -RUN addgroup --system app && adduser --system --group app && chown -R app:app /app +RUN addgroup --system app && adduser --system --group app && mkdir -p /tmp/uv-cache && chown -R app:app /app /tmp/uv-cache COPY --chown=app:app src/ /app/src/ COPY --chown=app:app migrations/ /app/migrations/ diff --git a/docker-compose.yml.example b/docker-compose.yml.example index 39b5d8c3..8bb8507f 100644 --- a/docker-compose.yml.example +++ b/docker-compose.yml.example @@ -4,29 +4,43 @@ services: build: context: . dockerfile: Dockerfile + entrypoint: ["sh", "docker/entrypoint.sh"] depends_on: database: condition: service_healthy + redis: + condition: service_healthy ports: - 8000:8000 volumes: - .:/app - venv:/app/.venv + environment: + - DB_CONNECTION_URI=postgresql+psycopg://postgres:postgres@database:5432/postgres + - CACHE_URL=redis://redis:6379/0?suppress=true env_file: - - .env + - path: .env + required: false deriver: build: context: . dockerfile: Dockerfile - entrypoint: ["uv", "run", "python", "-m", "src.deriver"] + entrypoint: ["/app/.venv/bin/python", "-m", "src.deriver"] depends_on: database: condition: service_healthy + redis: + condition: service_healthy volumes: - .:/app - venv:/app/.venv + environment: + - DB_CONNECTION_URI=postgresql+psycopg://postgres:postgres@database:5432/postgres + - CACHE_URL=redis://redis:6379/0?suppress=true + - METRICS_ENABLED=true env_file: - - .env + - path: .env + required: false database: image: pgvector/pgvector:pg15 restart: always @@ -34,16 +48,16 @@ services: - 5432:5432 command: ["postgres", "-c", "max_connections=800"] environment: - - POSTGRES_DB=honcho - - POSTGRES_USER=testuser - - POSTGRES_PASSWORD=testpwd + - POSTGRES_DB=postgres + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=postgres - POSTGRES_HOST_AUTH_METHOD=trust - PGDATA=/var/lib/postgresql/data/pgdata volumes: - ./database/init.sql:/docker-entrypoint-initdb.d/init.sql - pgdata:/var/lib/postgresql/data/ healthcheck: - test: ["CMD-SHELL", "pg_isready -U testuser -d honcho"] + test: ["CMD-SHELL", "pg_isready -U postgres -d postgres"] interval: 5s timeout: 5s retries: 5 @@ -59,6 +73,16 @@ services: interval: 5s timeout: 5s retries: 5 + prometheus: + image: prom/prometheus:v3.2.1 + ports: + - 9090:9090 + volumes: + - ./docker/prometheus.yml:/etc/prometheus/prometheus.yml:ro + - prometheus-data:/prometheus + depends_on: + api: + condition: service_started grafana: image: grafana/grafana:11.4.0 ports: @@ -70,6 +94,11 @@ services: - GF_AUTH_ANONYMOUS_ORG_ROLE=Viewer volumes: - ./grafana-data:/var/lib/grafana + - ./docker/grafana-datasource.yml:/etc/grafana/provisioning/datasources/datasource.yml:ro + depends_on: + prometheus: + condition: service_started volumes: pgdata: venv: + prometheus-data: diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh new file mode 100755 index 00000000..bc8e3f37 --- /dev/null +++ b/docker/entrypoint.sh @@ -0,0 +1,8 @@ +#!/bin/sh +set -e + +echo "Running database migrations..." +/app/.venv/bin/python scripts/provision_db.py + +echo "Starting API server..." +exec /app/.venv/bin/fastapi run --host 0.0.0.0 src/main.py diff --git a/docker/grafana-datasource.yml b/docker/grafana-datasource.yml new file mode 100644 index 00000000..bb009bb2 --- /dev/null +++ b/docker/grafana-datasource.yml @@ -0,0 +1,9 @@ +apiVersion: 1 + +datasources: + - name: Prometheus + type: prometheus + access: proxy + url: http://prometheus:9090 + isDefault: true + editable: false diff --git a/docker/prometheus.yml b/docker/prometheus.yml new file mode 100644 index 00000000..18041391 --- /dev/null +++ b/docker/prometheus.yml @@ -0,0 +1,10 @@ +global: + scrape_interval: 15s + +scrape_configs: + - job_name: honcho-api + static_configs: + - targets: ["api:8000"] + - job_name: honcho-deriver + static_configs: + - targets: ["deriver:9090"]