version: '3.1'

services:
  localstack:
    container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}"
    image: "localstack/localstack:0.14.5"
    network_mode: bridge
    environment:
      - SERVICES=s3
    ports:
      - "4566:4566"
    volumes:
      - "${TMPDIR:-/tmp/localstack}:/tmp/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"
    healthcheck:
      test: "bash -c 'AWS_ACCESS_KEY_ID=test AWS_SECRET_ACCESS_KEY=test aws --endpoint-url=http://localhost:4566 s3 ls'"
      retries: 5
      interval: 10s
  mongo:
    container_name: "${MONGO_DOCKER_NAME-mongo_main}"
    image: mongo
    network_mode: bridge
    ports:
      - "27017:27017"
    volumes:
      - "${TMPDIR:-/tmp/mongo}:/db/data"
    healthcheck:
      test: "bash -c 'mongo --host localhost:27017 --eval \"printjson(rs.status())\"'"
      retries: 5
      interval: 10s

  redis_1:
    image: 'redis:7-alpine'
    container_name: redis_1
    ports:
      - "6361:6379"
    volumes:
      - redis_1_data:/data
      - ./redis/redis.conf:/usr/local/etc/redis/redis.conf
    command: [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
    networks:
      redis_cluster_net:
        ipv4_address: 173.18.0.2

  redis_2:
    image: 'redis:7-alpine'
    container_name: redis_2
    ports:
      - "6362:6379"
    volumes:
      - redis_2_data:/data
      - ./redis/redis.conf:/usr/local/etc/redis/redis.conf
    command: [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
    networks:
      redis_cluster_net:
        ipv4_address: 173.18.0.3

  redis_3:
    image: 'redis:7-alpine'
    container_name: redis_3
    ports:
      - "6363:6379"
    volumes:
      - redis_3_data:/data
      - ./redis/redis.conf:/usr/local/etc/redis/redis.conf
    command: [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
    networks:
      redis_cluster_net:
        ipv4_address: 173.18.0.4

  redis_4:
    image: 'redis:7-alpine'
    container_name: redis_4
    ports:
      - "6364:6379"
    volumes:
      - redis_4_data:/data
      - ./redis/redis.conf:/usr/local/etc/redis/redis.conf
    command: [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
    networks:
      redis_cluster_net:
        ipv4_address: 173.18.0.5

  redis_5:
    image: 'redis:7-alpine'
    container_name: redis_5
    ports:
      - "6395:6379"
    volumes:
      - redis_5_data:/data
      - ./redis/redis.conf:/usr/local/etc/redis/redis.conf
    command: [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
    networks:
      redis_cluster_net:
        ipv4_address: 173.18.0.6

  redis_6:
    image: 'redis:7-alpine'
    container_name: redis_6
    ports:
      - "6396:6379"
    volumes:
      - redis_6_data:/data
      - ./redis/redis.conf:/usr/local/etc/redis/redis.conf
    command: [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
    networks:
      redis_cluster_net:
        ipv4_address: 173.18.0.7

  # Ephemeral container to create the Redis cluster connections.
  # Once the setup is done, this container shuts down
  # and the cluster can be used by the service app container
  redis_cluster_init_1:
    image: 'redis:7-alpine'
    command: redis-cli --cluster create 173.18.0.2:6379 173.18.0.3:6379 173.18.0.4:6379 173.18.0.5:6379 173.18.0.6:6379 173.18.0.7:6379 --cluster-replicas 1 --cluster-yes
    networks:
      redis_cluster_net:
        ipv4_address: 173.18.0.10
    depends_on:
      - redis_1
      - redis_2
      - redis_3
      - redis_4
      - redis_5
      - redis_6

  # Web UI to browse through our Redis data across all nodes
  redis_commander:
    image: rediscommander/redis-commander:latest
    container_name: redis_web
    environment:
      REDIS_HOSTS: "local:redis_1:6379,local:redis_2:6379,local:redis_3:6379,local:redis_4:6379,local:redis_5:6379,local:redis_6:6379,local:redis_queue_1:6379,local:redis_queue_2:6379,local:redis_queue_3:6379"
    ports:
      - "5001:8081"
    depends_on:
      - redis_1
      - redis_2
      - redis_3
      - redis_4
      - redis_5
      - redis_6
      - redis_cluster_init_1
      - redis-master
      - redis_queue_2
      - redis_queue_3
    networks:
      redis_cluster_net:
        ipv4_address: 173.18.0.9
      redis_queue_cluster_net:
        ipv4_address: 173.19.0.9

  redis-master:
    image: 'redis:7-alpine'
    container_name: redis_queue_1
    ports:
      - "6381:6379"
    volumes:
      - redis_sentinel_data:/data
    #command: [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
    networks:
      redis_queue_cluster_net:
        ipv4_address: 173.19.0.2

  redis_queue_2:
    image: 'redis:7-alpine'
    container_name: redis_queue_2
    ports:
      - "6382:6379"
    volumes:
      - redis_sentinel_data:/data
    command: [ "redis-server", "--slaveof", "redis-master", "6379" ]
    networks:
      redis_queue_cluster_net:
        ipv4_address: 173.19.0.3

  redis_queue_3:
    image: 'redis:7-alpine'
    container_name: redis_queue_3
    ports:
      - "6383:6379"
    volumes:
      - redis_sentinel_data:/data
    command: [ "redis-server", "--slaveof", "redis-master", "6379" ]
    networks:
      redis_queue_cluster_net:
        ipv4_address: 173.19.0.4

  # Instance 1
  redis-sentinel:
    build:
      context: ./redis-sentinel
    links:
      - redis-master
    networks:
      redis_queue_cluster_net:
        ipv4_address: 173.19.0.5

  # Instance 2
  redis-sentinel2:
    build:
      context: ./redis-sentinel
    links:
      - redis-master
    networks:
      redis_queue_cluster_net:
        ipv4_address: 173.19.0.6

  # Instance 3
  redis-sentinel3:
    build:
      context: ./redis-sentinel
    links:
      - redis-master
    networks:
      redis_queue_cluster_net:
        ipv4_address: 173.19.0.7

# Rename the default network, so we can easily identify it
# Across all containers
networks:
  redis_queue_cluster_net:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 173.19.0.0/16

  redis_cluster_net:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 173.18.0.0/16

volumes:
  redis_1_data: {}
  redis_2_data: {}
  redis_3_data: {}
  redis_4_data: {}
  redis_5_data: {}
  redis_6_data: {}
  redis_sentinel_data: { }
