ElasticSearch使用Docker合成创建索引

xfb7svmp  于 2023-03-17  发布在  ElasticSearch
关注(0)|答案(1)|浏览(157)

Elasticsearch使用Docker合成创建索引

我想使用默认索引为productsdocker-compose.yaml文件在本地设置ElasticSearch。
我的docker-compose.yaml文件看起来像这样:

version: '3.1'

services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.17.0
    container_name: es
    environment:
      - discovery.type=single-node
    ports:
      - 9200:9200
    deploy:
      resources:
        limits:
          memory: 2GB

如何在运行docker compose up -d期间在elasticsearch上创建索引,而不是手动创建?

wqlqzqxt

wqlqzqxt1#

为了补充@FabioStein关于相关stackoverflow question的评论,如果出于某种原因,您不需要额外的dockerfile,而是将所有内容都保留在docker-compose文件中,您可以使用command选项。

version: '3.1'

services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.17.0
    container_name: es
    environment:
      - discovery.type=single-node
    ports:
      - 9200:9200
    command: >
      bash -c '
        until curl -sS "http://localhost:9200/_cat/health?h=status" | grep -q "green\|yellow"; do
          sleep 1
        done
        curl -X PUT "http://localhost:9200/products" -H "Content-Type: application/json" -d'
        {
          "mappings": {
            "properties": {
              "name": { "type": "text" },
              "quantity": { "type": "integer" }
            }
          }
        }'
      '
    deploy:
      resources:
        limits:
          memory: 2GB

无论如何,这不如一个单独的docker文件优雅和可维护,但是,您知道,只是为了记录

相关问题