services:
  pd0:
    container_name: surrealdb-pd0
    hostname: pd0
    image: pingcap/pd:v8.5.0
    ports:
      - "2379"
    volumes:
      - data:/data
      - logs:/logs
    command:
      - --name=pd0
      - --client-urls=http://0.0.0.0:2379
      - --peer-urls=http://0.0.0.0:2380
      - --advertise-client-urls=http://pd0:2379
      - --advertise-peer-urls=http://pd0:2380
      - --initial-cluster=pd0=http://pd0:2380,pd1=http://pd1:2380,pd2=http://pd2:2380
      - --data-dir=/data/pd0
      - --log-file=/logs/pd0.log
    restart: on-failure
    healthcheck:
      test: /pd-ctl health | jq -e ".[] | select(.name == \"$(hostname)\").health"
      start_period: 5s
      retries: 5
      timeout: 10s

  pd1:
    container_name: surrealdb-pd1
    hostname: pd1
    image: pingcap/pd:v8.5.0
    ports:
      - "2379"
    volumes:
      - data:/data
      - logs:/logs
    command:
      - --name=pd1
      - --client-urls=http://0.0.0.0:2379
      - --peer-urls=http://0.0.0.0:2380
      - --advertise-client-urls=http://pd1:2379
      - --advertise-peer-urls=http://pd1:2380
      - --initial-cluster=pd0=http://pd0:2380,pd1=http://pd1:2380,pd2=http://pd2:2380
      - --data-dir=/data/pd1
      - --log-file=/logs/pd1.log
    restart: on-failure
    healthcheck:
      test: /pd-ctl health | jq -e ".[] | select(.name == \"$(hostname)\").health"
      start_period: 5s
      retries: 5
      timeout: 10s

  pd2:
    container_name: surrealdb-pd2
    hostname: pd2
    image: pingcap/pd:v8.5.0
    ports:
      - "2379"
    volumes:
      - data:/data
      - logs:/logs
    command:
      - --name=pd2
      - --client-urls=http://0.0.0.0:2379
      - --peer-urls=http://0.0.0.0:2380
      - --advertise-client-urls=http://pd2:2379
      - --advertise-peer-urls=http://pd2:2380
      - --initial-cluster=pd0=http://pd0:2380,pd1=http://pd1:2380,pd2=http://pd2:2380
      - --data-dir=/data/pd2
      - --log-file=/logs/pd2.log
    restart: on-failure
    healthcheck:
      test: /pd-ctl health | jq -e ".[] | select(.name == \"$(hostname)\").health"
      start_period: 5s
      retries: 5
      timeout: 10s

  tikv0:
    container_name: surrealdb-tikv0
    hostname: tikv0
    image: pingcap/tikv:v8.5.0
    volumes:
      - data:/data
      - logs:/logs
    command:
      - --addr=0.0.0.0:20160
      - --advertise-addr=tikv0:20160
      - --status-addr=0.0.0.0:20180
      - --data-dir=/data/tikv0
      - --pd=pd0:2379,pd1:2379,pd2:2379
      - --log-file=/logs/tikv0.log
    depends_on:
      pd0:
        condition: service_healthy
      pd1:
        condition: service_healthy
      pd2:
        condition: service_healthy
    restart: on-failure
    healthcheck:
      test: /tikv-ctl --host $(hostname):20160 metrics
      start_period: 5s
      retries: 5
      timeout: 10s

  tikv1:
    container_name: surrealdb-tikv1
    hostname: tikv1
    image: pingcap/tikv:v8.5.0
    volumes:
      - data:/data
      - logs:/logs
    command:
      - --addr=0.0.0.0:20160
      - --advertise-addr=tikv1:20160
      - --status-addr=0.0.0.0:20180
      - --data-dir=/data/tikv1
      - --pd=pd0:2379,pd1:2379,pd2:2379
      - --log-file=/logs/tikv1.log
    depends_on:
      pd0:
        condition: service_healthy
      pd1:
        condition: service_healthy
      pd2:
        condition: service_healthy
    restart: on-failure
    healthcheck:
      test: /tikv-ctl --host $(hostname):20160 metrics
      start_period: 5s
      retries: 5
      timeout: 10s

  tikv2:
    container_name: surrealdb-tikv2
    hostname: tikv2
    image: pingcap/tikv:v8.5.0
    volumes:
      - data:/data
      - logs:/logs
    command:
      - --addr=0.0.0.0:20160
      - --advertise-addr=tikv2:20160
      - --status-addr=0.0.0.0:20180
      - --data-dir=/data/tikv2
      - --pd=pd0:2379,pd1:2379,pd2:2379
      - --log-file=/logs/tikv2.log
    depends_on:
      pd0:
        condition: service_healthy
      pd1:
        condition: service_healthy
      pd2:
        condition: service_healthy
    restart: on-failure
    healthcheck:
      test: /tikv-ctl --host $(hostname):20160 metrics
      start_period: 5s
      retries: 5
      timeout: 10s

  surrealdb:
    image: surrealdb/surrealdb:latest
    container_name: surrealdb
    ports:
      - "8000:8000"
    command:
      - start
      - --log=trace
      - --user=root
      - --pass=root
      - tikv://pd0:2379
    depends_on:
      tikv0:
        condition: service_healthy
      tikv1:
        condition: service_healthy
      tikv2:
        condition: service_healthy
    restart: always

volumes:
  data: {}
  logs: {}
