docker-compose 中 ELASTICSEARCH_HOSTS 的语法是什么?

What is the syntax for ELASTICSEARCH_HOSTS in docker-compose?

试图找出 Kibana 的 ELASTICSEARCH_HOSTS 语法,但我收到:

kib01     |  FATAL  Error: [config validation of [elasticsearch].hosts]: types that failed validation:
kib01     | - [config validation of [elasticsearch].hosts.0]: expected URI with scheme [http|https].
kib01     | - [config validation of [elasticsearch].hosts.1]: could not parse array value from json input

来自 Kibana 本身或:

ERROR: The Compose file './docker-compose.yml' is invalid because:
services.kibana.environment.ELASTICSEARCH_HOSTS contains ["http://es01:9200", "http://es02:9200", "http://es03:9200", "http://es04:9200"], which is an invalid type, it should be a string, number, or a null

来自Docker撰写。

我的最新版本是:

environment:
  ELASTICSEARCH_HOSTS=["http://es01:9200", "http://es02:9200", "http://es03:9200", "http://es04:9200"]

我也试过:

environment:
  - ELASTICSEARCH_HOSTS: '["http://es01:9200", "http://es02:9200", "http://es03:9200", "http://es04:9200"]'

我试着把它变成一个列表:

environment:
  ELASTICSEARCH_HOSTS:
    - "<host1>"
    - "<host2>"

我尝试删除上述变体和列表中各种组合中不同位置的引号。

我还使用 :=

尝试了 official documentation 的组合

ELASTICSEARCH_HOST = http://es01:9200, http://es02:9200...

全部被拒绝。有谁知道使它起作用的神奇语法?

更新失败列表:

  ELASTICSEARCH_HOSTS: ['http://es01:9200','http://es02:9200','http://es03:9200','http://es04:9200']
  ELASTICSEARCH_HOSTS="['http://es01:9200','http://es02:9200','http://es03:9200','http://es04:9200']"
  - ELASTICSEARCH_HOSTS=['http://es01:9200','http://es02:9200','http://es03:9200','http://es04:9200']
  - ELASTICSEARCH_HOSTS="["http://es01:9200","http://es02:9200","http://es03:9200","http://es04:9200"]"
  ELASTICSEARCH_HOSTS:
    - "<host1>"
    - "<host2>"

解决您的问题需要注意以下几点:

  • 在docker-compose中,你的环境变量必须写成对象,或者数组:
# A native yaml approach to define key-value objects
environment:  
  KEY1: VAL1
  KEY2: VAL2
# OR
# Some special way for compose yaml parser
# that can split key and value from a "KEY=VAL" string
environment:  
  - KEY1=VAL1
  - KEY2=VAL2
  • 环境变量的值必须是字符串(上例)、数字或null(空值)。注意 [foo, bar] 在第一种格式中被解析为一个列表(如果你希望它被解析为一个字符串,你需要将它包裹在引号内),但是第二种格式将它解析为一个字符串.

  • this 论坛问题中,有一个示例如何将多个 elasticsearch 主机作为环境变量传递 (ELASTICSEARCH_HOSTS)。

所以这一定是一个 docker-compose 和 Kibana 都能理解的有效示例:

environment:
  ELASTICSEARCH_HOSTS: '["http://es01:9200","http://es02:9200","http://es03:9200","http://es04:9200"]'
# OR
environment:
  - ELASTICSEARCH_HOSTS=["http://es01:9200","http://es02:9200","http://es03:9200","http://es04:9200"]