# This file can be used to setup a Dgraph cluster with 6 Dgraph Alphas and 3 Dgraph Zero nodes on a
# Docker Swarm with replication. This setup ensures high availability for both Zero and Alpha.

# It expects six virtual machines with hostnames host1, host2, host3, host4, host5 and host6 to
# be part of the swarm.  There is a constraint to make sure that each Dgraph Alpha runs on a
# particular host. Dgraph Zero nodes run on host1, host2 and host3.

# Data would be persisted to a docker volume called data-volume on the virtual machines which are
# part of the swarm.
# Run `docker stack deploy -c docker-compose-ha.yml` on the Swarm leader to start the cluster.
# NOTE: whitelisting is set to private address ranges, this is ONLY for a local setup
#       please change it accordingly for your production setup
#       more here https://dgraph.io/docs/deploy/dgraph-administration/#whitelisting-admin-operations

version: "3.2"
networks:
  dgraph:
services:
  zero1:
    ulimits:
      nproc: 1048576
      nofile:
        soft: 1048576
        hard: 1048576
    image: dgraph/dgraph:latest
    volumes:
      - ./zero/zero1:/dgraph
    ports:
      - 5080:5080
      - 6080:6080
    networks:
      - dgraph
    command: dgraph zero --my=zero1:5080 --rebalance_interval 120m --replicas 3 --raft="idx=1"
  zero2:
    ulimits:
      nproc: 1048576
      nofile:
        soft: 1048576
        hard: 1048576
    image: dgraph/dgraph:latest
    volumes:
    - ./zero/zero2:/dgraph
    ports:
      - 5081:5081
      - 6081:6081
    networks:
      - dgraph
    command: dgraph zero -o 1 --my=zero2:5081 --rebalance_interval 120m --replicas 3 --peer zero1:5080 --raft="idx=2"
  zero3:
    ulimits:
      nproc: 1048576
      nofile:
        soft: 1048576
        hard: 1048576
    image: dgraph/dgraph:latest
    volumes:
        - ./zero/zero3:/dgraph
    ports:
      - 5082:5082
      - 6082:6082
    networks:
      - dgraph
    command: dgraph zero -o 2 --my=zero3:5082 --rebalance_interval 120m --replicas 3 --peer zero1:5080 --raft="idx=3"
  alpha1:
    ulimits:
      nproc: 1048576
      nofile:
        soft: 1048576
        hard: 1048576
    image: dgraph/dgraph:latest
    hostname: "alpha1"
    volumes:
      - ./alpha/alpha1:/dgraph
    ports:
      - 8080:8080
      - 9080:9080
    networks:
      - dgraph
    deploy:
      replicas: 1
    command: dgraph alpha --my=alpha1:7080 --security whitelist=0.0.0.0/0 --zero=zero1:5080,zero2:5081,zero3:5082
  alpha2:
    ulimits:
      nproc: 1048576
      nofile:
        soft: 1048576
        hard: 1048576
    image: dgraph/dgraph:latest
    hostname: "alpha2"
    volumes:
       - ./alpha/alpha2:/dgraph
    ports:
      - 8081:8081
      - 9081:9081
    networks:
      - dgraph
    deploy:
      replicas: 1
    command: dgraph alpha --my=alpha2:7081 --security whitelist=0.0.0.0/0 --zero=zero1:5080,zero2:5081,zero3:5082 -o 1
  alpha3:
    ulimits:
      nproc: 1048576
      nofile:
        soft: 1048576
        hard: 1048576
    image: dgraph/dgraph:latest
    hostname: "alpha3"
    volumes:
       - ./alpha/alpha3:/dgraph
    ports:
      - 8082:8082
      - 9082:9082
    networks:
      - dgraph
    deploy:
      replicas: 1
    command: dgraph alpha --my=alpha3:7082 --security whitelist=0.0.0.0/0 --zero=zero1:5080,zero2:5081,zero3:5082 -o 2
  alpha4:
    ulimits:
      nproc: 1048576
      nofile:
        soft: 1048576
        hard: 1048576
    image: dgraph/dgraph:latest
    hostname: "alpha4"
    volumes:
       - ./alpha/alpha4:/dgraph
    ports:
      - 8083:8083
      - 9083:9083
    networks:
      - dgraph
    command: dgraph alpha --my=alpha4:7083 --security whitelist=0.0.0.0/0 --zero=zero1:5080,zero2:5081,zero3:5082 -o 3
  alpha5:
    ulimits:
      nproc: 1048576
      nofile:
        soft: 1048576
        hard: 1048576
    image: dgraph/dgraph:latest
    hostname: "alpha5"
    volumes:
       - ./alpha/alpha5:/dgraph
    ports:
      - 8084:8084
      - 9084:9084
    networks:
      - dgraph
    command: dgraph alpha --my=alpha5:7084 --security whitelist=0.0.0.0/0 --zero=zero1:5080,zero2:5081,zero3:5082 -o 4
  alpha6:
    ulimits:
      nproc: 1048576
      nofile:
        soft: 1048576
        hard: 1048576
    image: dgraph/dgraph:latest
    hostname: "alpha6"
    volumes:
       - ./alpha/alpha6:/dgraph
    ports:
      - 8085:8085
      - 9085:9085
    networks:
      - dgraph
    command: dgraph alpha --my=alpha6:7085 --security whitelist=0.0.0.0/0 --zero=zero1:5080,zero2:5081,zero3:5082 -o 5
  ratel:
    ulimits:
      nproc: 1048576
      nofile:
        soft: 1048576
        hard: 1048576
    image: dgraph/ratel:latest
    ports:
      - 8000:8000
    command: dgraph-ratel
volumes:
  data-volume:
