【问题标题】:Set up mapping in Elasticsearch during Docker run在 Docker 运行期间在 Elasticsearch 中设置映射
【发布时间】:2017-01-31 20:06:12
【问题描述】:

我将使用官方的 Elasticsearch 图像。我要做的是在 Docker 运行期间设置索引的映射。

所以我需要在容器启动后立即执行。

curl -XPUT localhost:9200/_template/http_request -d {.....}

所以我不能在 Dockerfile 中这样做还是可以?

谢谢

【问题讨论】:

    标签: elasticsearch docker docker-compose


    【解决方案1】:

    我最终这样做了

    docker-composer.yml

    version: '2'
    services:
      elasticsearch:
        image: elasticsearch:2.3
        command: elasticsearch -Des.network.host=0.0.0.0
        ports:
          - "9200:9200"
          - "9300:9300"
      elasticsearch-mapping-init:
        build: elasticsearch-mapping-init
        links:
          - elasticsearch
        depends_on:
          - elasticsearch
    

    这是我的 elasticsearch-mapping-init/Dockerfile:

    FROM ubuntu
    
    # Install packages
    RUN apt-get update && \
    apt-get install -y curl
    
    COPY docker-entrypoint.sh /
    
    ENTRYPOINT ["/docker-entrypoint.sh"]
    

    这是我的 elasticsearch-mapping-init/docker-entrypoint.sh

    #!/bin/bash
    
    for i in {30..0}; do
        if curl elasticsearch:9200; then
            curl -XPUT elasticsearch:9200/_template/log -d '
                {
                    "template" : "log-*",
                    "settings": {
                       "number_of_shards": 1
                    },
                    "mappings" : {
    
    
                    }
                }';
                break;
        fi
        sleep 2
    done
    

    我认为这并不完美,我仍在寻找更好的解决方案。

    【讨论】:

    • 您可以在单个官方 elasticsearch 映像中完成所有这些操作。您可以使用 FROM elasticsearch 在您自己的 dockerfile 中添加他们的 docker-entrypoint.sh。这是他们现在为最新的 es 所做的:github.com/docker-library/elasticsearch/blob/…
    • @BretFisher 你能给我更多的信息吗?我仍然不知道如何在官方 ES 映像中进行操作。
    • 这可能不是完美的答案,因为“depend_on”不会等待弹性搜索服务准备好。它将等到弹性搜索启动。因此 init-service 可以在 elasticsearch 午餐时启动,但尚未完全运行。
    • 这样你就可以替换官方 es 中的 docker-entrypoint.sh。它会启动 es,运行你的 curl,然后停止它,然后通过传递 CMD 完成。这个 repo 对 postgres 做同样的事情,它创建用户/复制/db,然后停止并在最后传递 exec。查看他们的 docker-entrypoint.sh 和 runtime/functions 文件github.com/sameersbn/docker-postgresql
    • 这里是 es 官方 2.4 中的入口点脚本。你会想要,在 exec 之前的底部,1. 启动 es,2. 运行上面的脚本,3. 停止 es。 github.com/docker-library/elasticsearch/blob/…
    猜你喜欢
    • 1970-01-01
    • 2014-11-27
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    • 2018-04-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-18
    相关资源
    最近更新 更多