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

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

问题描述

我使用官方的 elasticsearch docker 映像,想知道如何在构建自定义索引期间也包含在内,以便在我启动容器时索引已经存在.

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

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

我收到以下错误:

0curl: (7) 无法连接到 127.0.0.1 端口 9200:连接被拒绝

我可以在构建期间通过这样的 API 调用访问 elasticsearch 吗?或者是否有完全不同的方法来实现它?

解决方案

我遇到了类似的问题.

我想创建一个带有预加载数据的 docker 容器(通过 repo 中的一些脚本和 json 文件).elasticsearch 中的数据在执行过程中不会改变,我希望构建步骤尽可能少(理想情况下只有 docker-compose up -d).

一种选择是手动执行一次,并将 elasticsearch 数据文件夹(带有 docker 卷)存储在存储库中.但是这样我就会有重复的数据,每次数据更改时我都必须手动签入新版本的数据文件夹.

解决办法

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

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

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

  1. 下载等待

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

此脚本将等到 elasticsearch 运行我们的插入命令.

  1. 将数据插入到elasticsearch中

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

此命令在构建过程中启动 elasticsearch,插入数据并在一个 RUN 命令中将其删除.除了已经正确初始化的 elasticsearch 数据文件夹之外,容器保持原样.

总结

FROM elasticsearch运行 mkdir/data &&chown -R elasticsearch:elasticsearch/data &&echo 'es.path.data:/data' >>配置/elasticsearch.yml &&echo 'path.data:/data' >>配置/elasticsearch.yml添加 https://raw.githubusercontent.com/vishnubob/wait-for-it/e1f115e4ca285c3c24e847c4dd4be955e0ed51c2/wait-for-it.sh/utils/wait-for-it.sh# 复制您可能需要的文件和插入脚本运行/docker-entrypoint.sh elasticsearch -p/tmp/epid &/bin/bash/utils/wait-for-it.sh -t 0 localhost:9200 -- path/to/insert/script.sh;杀死 $(cat/tmp/epid) &&等待 $(cat/tmp/epid);退出 0;

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

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.

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

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

I get the following error:

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

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

解决方案

I've had a similar problem.

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).

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.

The solution

  1. Make elasticsearch write data to a folder that is not declared as a volume in elasticsearchs' official dockerfile.

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

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

  1. Download wait-for-it

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

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

  1. Insert data into elasticsearch

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;

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.

Summary

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 构建期间添加弹性搜索索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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