Nakama with YugabyteDB
Tags: yugabytedb, docker, compose, nakama
Published: 2022-09-06 09:22:32

A YugabyteDB users asked about running [Nakama](https://heroiclabs.com/docs/nakama) ((server for realtime social and web & mobile game apps)) on YugabyteDB

As YugabyteDB is PostgreSQL-compatible, this is as easy as pointing the connection to the new database. The [Nakama documentation](https://heroiclabs.com/docs/nakama/getting-started/install/docker/) shows an example of docker-compose.yaml for PostgreSQL and CockroachDB. 

The idea is:
- put `CREATE DATABASE` in an itit script directory
- start YugabyteDB with `bin/yugabyted start --daemon=false --listen=0.0.0.0 --initial_scripts_dir=/tmp/sql --base_dir=/var/lib/yugabytedb`
- and before connecting, what to be sure that port 5433 is up:
```sh
       until echo > /dev/tcp/yugabytedb/5433 ;
        do
         echo "Waiting for YugabyteDB to be up..." ; sleep 1 ;
        done ;
        echo "YugabyteDB is up 🚀 - check http://localhost:7000" ; sleep 5 &&

```

Here is my docker-compose-yaml:

```yaml
version: '3'
services:
  yugabytedb:
    image: yugabytedb/yugabyte:2.14.1.0-b36
    environment:
      - POSTGRES_DB=nakama
      - POSTGRES_PASSWORD=localdb
    entrypoint: |
     bash -c '
     mkdir -p /var/lib/yugabytedb # initialization scripts
     echo "create database $${POSTGRES_DB:-$${POSTGRES_USER}}" > /var/lib/yugabytedb/01-db.sql
     bin/yugabyted start --daemon=false --listen=0.0.0.0 --initial_scripts_dir=/tmp/sql --base_dir=/var/lib/yugabytedb
     '
    restart: "no"
    volumes:
      - data:/var/lib/yugabytedb
    expose:
      - "7000"
      - "5433"
    ports:
      - "5433:5433"
      - "7000:7000"
  nakama:
    image: registry.heroiclabs.com/heroiclabs/nakama:3.13.1
    entrypoint:
      - "/bin/bash"
      - "-ecx"
      - >
        until echo > /dev/tcp/yugabytedb/5433 ;
        do
         echo "Waiting for YugabyteDB to be up..." ; sleep 1 ;
        done ;
        echo "YugabyteDB is up 🚀 - check http://localhost:7000" ; sleep 5 &&
        /nakama/nakama migrate up          --database.address postgres:localdb@yugabytedb:5433/nakama &&
        exec /nakama/nakama --name nakama1 --database.address postgres:localdb@yugabytedb:5433/nakama --logger.level DEBUG --session.token_expiry_sec 7200
   restart: "no"
    links:
      - "yugabytedb:db"
    depends_on:
      - yugabytedb
      - prometheus
    volumes:
      - ./:/nakama/data
    expose:
      - "7349"
      - "7350"
      - "7351"
      - "9100"
    ports:
      - "7349:7349"
      - "7350:7350"
      - "7351:7351"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:7350/"]
      interval: 10s
      timeout: 5s
      retries: 5
  prometheus:
    image: prom/prometheus
    entrypoint: /bin/sh -c
    command: |
      'sh -s <<EOF
        cat > ./prometheus.yml <<EON
      global:
        scrape_interval:     15s
        evaluation_interval: 15s
      scrape_configs:
        - job_name: prometheus
          static_configs:
          - targets: ['localhost:9090']
        - job_name: nakama
          metrics_path: /
          static_configs:
          - targets: ['nakama:9100']
      EON
      prometheus --config.file=./prometheus.yml
      EOF'
    ports:
      - '9090:9090'
volumes:
  data:
```