Self-hosting

Lunary was designed to be surprisingly simple to self-host.

The app consists of several services, orchestrated using Docker Compose (Docker Swarm support is coming soon):

  • Database (PostgreSQL)
  • Pgbouncer
  • API
  • Radar: runs evaluations on your runs in the background
  • Frontend

Steps

1

Log in to the private Container Repository

Make sure Docker and Docker Compose are installed on your host machine before running the following command:

docker login registry.lunary.ai -u USERNAME -p PASSWORD

Your organization's username and password will be provided by Lunary when your subscription is activated.

2

Docker Compose Setup

On a machine with at least 2GB of RAM allocated to Docker, create a docker-compose.yml file with the following content:

version: "3.8"
services:
db:
container_name: db
image: registry.lunary.ai/db:${APP_VERSION}
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
user: postgres
networks:
- lunary-network
volumes:
- lunary-db-data:/var/lib/postgresql/data
restart: unless-stopped
healthcheck:
test: /usr/bin/pg_isready
interval: 10s
timeout: 5s
retries: 5
pgbouncer:
container_name: pgbouncer
image: rmccaffrey/pgbouncer
environment:
DB_USER: ${POSTGRES_USER}
DB_PASSWORD: ${POSTGRES_PASSWORD}
POOL_MODE: session
DB_HOST: db
MAX_CLIENT_CONN: 1000
AUTH_TYPE: scram-sha-256
networks:
- lunary-network
ports:
- "5432:5432"
depends_on:
- db
api:
container_name: api
image: registry.lunary.ai/backend:${APP_VERSION}
depends_on:
- db
ports:
- 3333:3333
restart: unless-stopped
environment:
SKIP_EMAIL_VERIFY: "true"
DATABASE_URL: postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}
NODE_ENV: production
env_file:
- .env
networks:
- lunary-network
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3333/v1/health"]
interval: 10s
start_period: 20s
timeout: 10s
retries: 3
radar:
container_name: radar
image: registry.lunary.ai/backend:${APP_VERSION}
entrypoint: npm -w packages/backend run radar
depends_on:
- db
restart: unless-stopped
environment:
DATABASE_URL: postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}
NODE_ENV: production
env_file:
- .env
networks:
- lunary-network
cpu_quota: 20000
cpu_period: 100000
frontend:
container_name: frontend
image: registry.lunary.ai/frontend:${APP_VERSION}
depends_on:
- api
environment:
PORT: 8080
NEXT_PUBLIC_API_URL: ${API_URL}
networks:
- lunary-network
ports:
- "8080:8080"
healthcheck:
test: ["CMD", "test", "-f", "/app/packages/frontend/.build_complete"]
interval: 5s
timeout: 10s
retries: 20
autoheal:
restart: always
image: willfarrell/autoheal
environment:
- AUTOHEAL_CONTAINER_LABEL=all
volumes:
- /var/run/docker.sock:/var/run/docker.sock
networks:
lunary-network:
volumes:
lunary-db-data:
3

Set env variables

The following environment variables are required for the setup to work:

POSTGRES_USER="postgres"
POSTGRES_PASSWORD="mysecretpassword"
POSTGRES_DB="lunary"
JWT_SECRET="secret"
APP_VERSION=latest
API_URL=http://your-host-ip-or-url:3333 # replace with your host IP or URL
# optionnal (for the playground, evaluation and radar features)
LUNARY_PUBLIC_KEY=259d2d94-9446-478a-ae04-484de705b522
OPENAI_API_KEY=sk-...
OPENROUTER_API_KEY=sk-...
PALM_API_KEY=AI...

You can customize the environment variables with the values you want.

4

Run docker compose

Navigate to the directory containing docker-compose.yml and run docker-compose up -d.
This command will start all the services in detached mode. It will automatically set up the database and run migrations. Wait for the frontend service to be healthy. You can verify its status with docker ps.

5

🎉 Done!

You're all set! Open http://your-host-ip-or-url:8080 to access the app.
Make sure to export the environment variable LUNARY_API_URL=http://your-host-ip-or-url:3333 to use make your SDK send the queries to your server.

Usage with external database

You can also use an external Postgres instance with Lunary, such as AWS RDS.

To do so, comment the db service in the docker-compose.yml file and set the following environment variables:

POSTGRES_USER=postgres
POSTGRES_PASSWORD=mysecretpassword
POSTGRES_DB=lunary
POSTGRES_HOST=your-postgres-host
POSTGRES_PORT=5432

External proxy

It is recommended to use an external proxy such as Nginx or Caddy to add SSL and rate limiting to the app.

Questions? We're here to help.