x-defaults: &defaults
  restart: unless-stopped
  logging:
    driver: json-file
    options:
      max-size: 100m
      max-file: '3'

services:
  consul:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}consul:${CONSUL_VERSION:-1.20.3}
    ports:
      - '${CONSUL_HTTP_PORT_OVERRIDE:-8500}:8500'
      - '${CONSUL_DNS_PORT_OVERRIDE:-8600}:8600/udp'
      - '${CONSUL_SERF_LAN_PORT_OVERRIDE:-8301}:8301'
      - '${CONSUL_SERF_WAN_PORT_OVERRIDE:-8302}:8302'
      - '${CONSUL_SERVER_RPC_PORT_OVERRIDE:-8300}:8300'
    volumes:
      - consul_data:/consul/data
      - consul_config:/consul/config

      # Custom configuration
      # - ./consul.json:/consul/config/consul.json:ro
    environment:
      - TZ=${TZ:-UTC}
      - CONSUL_BIND_INTERFACE=${CONSUL_BIND_INTERFACE:-eth0}
      - CONSUL_CLIENT_INTERFACE=${CONSUL_CLIENT_INTERFACE:-eth0}
      - CONSUL_LOCAL_CONFIG=${CONSUL_LOCAL_CONFIG:-'{"datacenter":"dc1","server":true,"ui_config":{"enabled":true},"bootstrap_expect":1,"log_level":"INFO"}'}
    command:
      - consul
      - agent
      - -server
      - -bootstrap-expect=1
      - -ui
      - -client=0.0.0.0
      - -bind={{ GetInterfaceIP "eth0" }}
    deploy:
      resources:
        limits:
          cpus: ${CONSUL_CPU_LIMIT:-1.00}
          memory: ${CONSUL_MEMORY_LIMIT:-512M}
        reservations:
          cpus: ${CONSUL_CPU_RESERVATION:-0.25}
          memory: ${CONSUL_MEMORY_RESERVATION:-128M}
    healthcheck:
      test: [CMD-SHELL, consul members]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 10s

volumes:
  consul_data:
  consul_config:
