version: '3.7'

services:

  db:
    image: library/postgres:9.6.17-alpine
    ports:
      - "5432:5432"
    environment:
      - "POSTGRES_DB=openfire"
      - "POSTGRES_USER=openfire"
      - "POSTGRES_PASSWORD=hunter2"
    volumes:
      - ./sql/clustered:/docker-entrypoint-initdb.d
    networks:
      openfire-clustered-net:
        ipv4_address: 172.60.0.11

  lb:
    image: nginx:stable
    ports:
      - "55222:55222"
      - "57070:57070"
      - "57443:57443"
    volumes:
      - ./nginx/clustered/nginx.conf:/etc/nginx/nginx.conf
    networks:
      openfire-clustered-net:
        ipv4_address: 172.60.0.99
  
  xmpp1:
    image: "openfire:${OPENFIRE_TAG}"
    ports:
      - "5221:5222"
      - "5261:5269"
      - "7071:7070"
      - "7441:7443"
      - "9091:9090"
    depends_on:
      - "db"
    volumes:
      - ./_data/xmpp/clustered/1/conf:/var/lib/openfire/conf
      - ./_data/plugins/hazelcast.jar:/var/lib/openfire/plugins/hazelcast.jar
      - ./wait-for-it.sh:/wait-for-it.sh
    command: ["/wait-for-it.sh", "-s", "db:5432", "--", "/sbin/entrypoint.sh"]
    networks:
      openfire-clustered-net:
        ipv4_address: 172.60.0.10
    extra_hosts:
      - "xmpp1.localhost.example:172.60.0.10"
      - "conference.xmpp1.localhost.example:172.60.0.10"
      - "xmpp2.localhost.example:172.60.0.20"
      - "conference.xmpp2.localhost.example:172.60.0.20"
      - "xmpp3.localhost.example:172.60.0.30"
      - "conference.xmpp3.localhost.example:172.60.0.30"

  xmpp2:
    image: "openfire:${OPENFIRE_TAG}"
    ports:
      - "5222:5222"
      - "5262:5269"
      - "7072:7070"
      - "7442:7443"
      - "9092:9090"
    depends_on:
      - "db"
    volumes:
      - ./_data/xmpp/clustered/2/conf:/var/lib/openfire/conf
      - ./_data/plugins/hazelcast.jar:/var/lib/openfire/plugins/hazelcast.jar
      - ./wait-for-it.sh:/wait-for-it.sh
    command: ["/wait-for-it.sh", "-s", "db:5432", "--", "/sbin/entrypoint.sh"]
    networks:
      openfire-clustered-net:
        ipv4_address: 172.60.0.20
    extra_hosts:
      - "xmpp1.localhost.example:172.60.0.10"
      - "conference.xmpp1.localhost.example:172.60.0.10"
      - "xmpp2.localhost.example:172.60.0.20"
      - "conference.xmpp2.localhost.example:172.60.0.20"
      - "xmpp3.localhost.example:172.60.0.30"
      - "conference.xmpp3.localhost.example:172.60.0.30"

  xmpp3:
    image: "openfire:${OPENFIRE_TAG}"
    ports:
      - "5223:5222"
      - "5263:5269"
      - "7073:7070"
      - "7443:7443"
      - "9093:9090"
    depends_on:
      - "db"
    volumes:
      - ./_data/xmpp/clustered/3/conf:/var/lib/openfire/conf
      - ./_data/plugins/hazelcast.jar:/var/lib/openfire/plugins/hazelcast.jar
      - ./wait-for-it.sh:/wait-for-it.sh
    command: ["/wait-for-it.sh", "-s", "db:5432", "--", "/sbin/entrypoint.sh"]
    networks:
      openfire-clustered-net:
        ipv4_address: 172.60.0.30
    extra_hosts:
      - "xmpp1.localhost.example:172.60.0.10"
      - "conference.xmpp1.localhost.example:172.60.0.10"
      - "xmpp2.localhost.example:172.60.0.20"
      - "conference.xmpp2.localhost.example:172.60.0.20"
      - "xmpp3.localhost.example:172.60.0.30"
      - "conference.xmpp3.localhost.example:172.60.0.30"

networks:
  openfire-clustered-net:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 172.60.0.0/24
