如何在Docker构建期间添加Elasticsearch索引 [英] How to add an elasticsearch index during docker build

查看:293
本文介绍了如何在Docker构建期间添加Elasticsearch索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了官方的Elasticsearch泊坞镜映像,想知道在构建自定义索引的过程中如何包括在内,以便在启动容器时该索引已经存在。

I use the official elasticsearch docker image and wonder how can I include also during building a custom index, so that the index is already there when I start the container.

我的尝试是将以下行添加到我的dockerfile中:

My attempt was to add the following line to my dockerfile:

RUN curl -XPUT 'http://127.0.0.1:9200/myindex' -d @index.json

我收到以下错误:

0curl: (7) Failed to connect to 127.0.0.1 port 9200: Connection refused

我可以在构建期间使用这种API调用到达Elasticsearch还是有完全不同的方式来实现?

Can I reach elasticsearch during build with such an API call or is there a complete different way to implement that?

推荐答案

我遇到了类似的问题。

我想要创建一个带有预加载数据的docker容器(通过仓库中的一些脚本和json文件)。 Elasticsearch内部的数据在执行期间不会改变,我想要尽可能少的构建步骤(最好是 docker-compose up -d )。

I wanted to create a docker container with preloaded data (via some scripts and json files in the repo). The data inside elasticsearch was not going to change during the execution and I wanted as few build steps as possible (ideally only docker-compose up -d).

一种选择是手动执行一次,然后将elasticsearch data文件夹(带有docker卷)存储在存储库中。但是那时我将拥有重复的数据,并且每次数据更改时我都必须手动检入新版本的数据文件夹。

One option would be to do it manually once, and store the elasticsearch data folder (with a docker volume) in the repository. But then I would have had duplicate data and I would have to check in manually a new version of the data folder every time the data changes.


  1. 使elasticsearch将数据写入未在Elasticsearchs官方dockerfile中声明为卷的文件夹中。

运行mkdir / data&& chown -R elasticsearch:elasticsearch / data&&回声‘es.path.data:/ data’>> config / elasticsearch.yml&&回声 path.data:/data>> config / elasticsearch.yml

(需要使用正确的权限创建文件夹)

(the folder needs to be created with the right permissions)


  1. 下载 wait-

  1. Download wait-for-it

ADD https://raw.githubusercontent.com/vishnubob/wait -for-it / e1f115e4ca285c3c24e847c4dd4be955e0ed51c2 / wait-for-it.sh /utils/wait-for-it.sh

此脚本将等待直到elasticsearch可以运行我们的插入命令为止。

This script will wait until elasticsearch is up to run our insert commands.


  1. 将数据插入elasticsearch

RUN /docker-entrypoint.sh elasticsearch -p / tmp / epid& / bin / bash /utils/wait-for-it.sh -t 0 localhost:9200-路径/到/插入/script.sh;杀死$(cat / tmp / epid)&&等待$(cat / tmp / epid);出口0;

此命令在构建过程中启动elasticsearch,插入数据并将其放入一个RUN命令中。容器保留了原来的位置,除了elasticsearch的数据文件夹已正确初始化。

This command starts elasticsearch during the build process, inserts data and takes it down in one RUN command. The container is left as it was except for elasticsearch's data folder which has been properly initialized now.

FROM elasticsearch

RUN mkdir /data && chown -R elasticsearch:elasticsearch /data && echo 'es.path.data: /data' >> config/elasticsearch.yml && echo 'path.data: /data' >> config/elasticsearch.yml

ADD https://raw.githubusercontent.com/vishnubob/wait-for-it/e1f115e4ca285c3c24e847c4dd4be955e0ed51c2/wait-for-it.sh /utils/wait-for-it.sh

# Copy the files you may need and your insert script

RUN /docker-entrypoint.sh elasticsearch -p /tmp/epid & /bin/bash /utils/wait-for-it.sh -t 0 localhost:9200 -- path/to/insert/script.sh; kill $(cat /tmp/epid) && wait $(cat /tmp/epid); exit 0;

就是这样!运行此映像时,数据库将预加载数据,索引等...

And that's it! When you run this image, the database will have preloaded data, indexes, etc...

这篇关于如何在Docker构建期间添加Elasticsearch索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆