在Docker运行期间在Elasticsearch中设置映射

Alb*_*bac 15 elasticsearch docker docker-compose

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

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

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

所以我不能在Dockerfile中这样做或者我可以吗?

谢谢

Alb*_*bac 6

我最终这样做了

泊坞窗,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
Run Code Online (Sandbox Code Playgroud)

这是我的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"]
Run Code Online (Sandbox Code Playgroud)

这是我的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
Run Code Online (Sandbox Code Playgroud)

我相信这并不完美,我仍然在寻找更好的解决方案.

  • 您可以在单一的官方弹性搜索图像中完成所有这些操作.您可以使用FROM elasticsearch在您自己的dockerfile中添加docker-entrypoint.sh.以下是他们现在为最新的es做的事情:https://github.com/docker-library/elasticsearch/blob/ffaaf3283e47dcb732e90288a58757b87a8439a7/5/docker-entrypoint.sh (2认同)
  • 所以你要在官方es中替换docker-entrypoint.sh.它会启动,运行你的卷曲,然后停止它,然后通过传递CMD完成.这个repo对于创建用户/复制/数据库的postgres执行相同的操作,然后在结束时停止并传递exec.看看他们的docker-entrypoint.sh和运行时/函数文件https://github.com/sameersbn/docker-postgresql (2认同)