# docker compose file for running a 3-node PostgreSQL cluster
# with 3-node etcd cluster as the DCS and one haproxy node
#
# requires a patroni image build from the Dockerfile:
# $ docker build -t patroni .
# The cluster could be started as:
# $ docker-compose up -d
# You can read more about it in the:
# https://github.com/zalando/patroni/blob/master/docker/README.md
version: "2"

networks:
    demo:

services:
    etcd1: &etcd
        image: ${PATRONI_TEST_IMAGE:-patroni}
        networks: [ demo ]
        environment:
            ETCD_LISTEN_PEER_URLS: http://0.0.0.0:2380
            ETCD_LISTEN_CLIENT_URLS: http://0.0.0.0:2379
            ETCD_INITIAL_CLUSTER: etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380
            ETCD_INITIAL_CLUSTER_STATE: new
            ETCD_INITIAL_CLUSTER_TOKEN: tutorial
            ETCD_UNSUPPORTED_ARCH: arm64
        container_name: demo-etcd1
        hostname: etcd1
        command: etcd -name etcd1 -initial-advertise-peer-urls http://etcd1:2380

    etcd2:
        <<: *etcd
        container_name: demo-etcd2
        hostname: etcd2
        command: etcd -name etcd2 -initial-advertise-peer-urls http://etcd2:2380

    etcd3:
        <<: *etcd
        container_name: demo-etcd3
        hostname: etcd3
        command: etcd -name etcd3 -initial-advertise-peer-urls http://etcd3:2380

    haproxy:
        image: ${PATRONI_TEST_IMAGE:-patroni}
        networks: [ demo ]
        env_file: docker/patroni.env
        hostname: haproxy
        container_name: demo-haproxy
        ports:
            - "5000:5000"
            - "5001:5001"
        command: haproxy
        environment: &haproxy_env
            ETCDCTL_ENDPOINTS: http://etcd1:2379,http://etcd2:2379,http://etcd3:2379
            PATRONI_ETCD3_HOSTS: "'etcd1:2379','etcd2:2379','etcd3:2379'"
            PATRONI_SCOPE: demo

    patroni1:
        image: ${PATRONI_TEST_IMAGE:-patroni}
        networks: [ demo ]
        env_file: docker/patroni.env
        hostname: patroni1
        container_name: demo-patroni1
        environment:
            <<: *haproxy_env
            PATRONI_NAME: patroni1

    patroni2:
        image: ${PATRONI_TEST_IMAGE:-patroni}
        networks: [ demo ]
        env_file: docker/patroni.env
        hostname: patroni2
        container_name: demo-patroni2
        environment:
            <<: *haproxy_env
            PATRONI_NAME: patroni2

    patroni3:
        image: ${PATRONI_TEST_IMAGE:-patroni}
        networks: [ demo ]
        env_file: docker/patroni.env
        hostname: patroni3
        container_name: demo-patroni3
        environment:
            <<: *haproxy_env
            PATRONI_NAME: patroni3
