elasticsearch 如何在docker构建期间添加elasticsearch索引
我使用官方的elasticsearch docker图像,想知道如何在构建自定义索引时也包含索引,以便在启动容器时索引已经存在 我试图在dockerfile中添加以下行:
elasticsearch 如何在docker构建期间添加elasticsearch索引,
elasticsearch,docker,
elasticsearch,Docker,我使用官方的elasticsearch docker图像,想知道如何在构建自定义索引时也包含索引,以便在启动容器时索引已经存在 我试图在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调用
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吗?或者有完全不同的方法来实现它吗 如果我收到了您的问题,您正在尝试连接ElasticSearch实例,甚至在其主机(即docker容器)运行之前。您需要首先运行容器
您可以创建一个shell脚本,可以使用bash选项执行该脚本。类似docker run之类的问题我也遇到过类似的问题 我想创建一个带有预加载数据的docker容器(通过repo中的一些脚本和json文件)。elasticsearch中的数据在执行过程中不会发生变化,我希望构建步骤尽可能少(理想情况下只需
docker compose up-d
)
一种选择是手动执行一次,并将elasticsearch数据文件夹(带有docker卷)存储在存储库中。但是,我会有重复的数据,每次数据更改时,我都必须手动签入数据文件夹的新版本
解决方案
运行mkdir/data&&chown-R elasticsearch:elasticsearch/data&&echo'es.path.data:/data'>>config/elasticsearch.yml&&echo'path.data:/data'>>config/elasticsearch.yml
(需要使用正确的权限创建文件夹)
添加https://raw.githubusercontent.com/vishnubob/wait-for-it/e1f115e4ca285c3c24e847c4dd4be955e0ed51c2/wait-for-it.sh /utils/等待它。sh
此脚本将等待elasticsearch运行我们的insert命令
RUN/docker-entrypoint.sh elasticsearch-p/tmp/epid&/bin/bash/utils/wait-for-it.sh-t0 localhost:9200--path/to/insert/script.sh;kill$(cat/tmp/epid)和wait$(cat/tmp/epid);出口0;
此命令在构建过程中启动elasticsearch,插入数据并在一次运行命令中将其取下。除了elasticsearch的数据文件夹已经正确初始化外,容器保持原样
总结
就这样!运行此映像时,数据库将预加载数据、索引等。执行此操作的简单方法是使用Dockerfile 使用
docker build-t elasticsearch custom:latest运行此Dockerfile。
FROM elasticsearch:5.5.1 AS esbuilder
ADD script.sh path/to/insert/script.sh
RUN apt-get update \
&& apt-get install procps -y \
&& apt-get install httping -y \
&& /docker-entrypoint.sh elasticsearch -d -E path.data=/tmp/data \
&& while ! httping -qc1 http://localhost:9200 ; do sleep 1 ; done \
&& path/to/insert/script.sh \
&& apt-get clean
FROM elasticsearch:5.5.1
COPY --from=esbuilder /tmp/data/ /usr/share/elasticsearch/data/
然后只需运行
docker run-t-d elasticsearch custom:latest
谢谢您的回复。您确定无法在生成期间添加索引吗。我可以不将其复制到特定文件夹吗?@crosscross据我所知,您正在尝试构建docker,一旦构建完成,您希望docker准备好一个ES实例,让它运行,然后创建一个默认索引。如果是这种情况,那么从逻辑上讲,构建后脚本对我来说很好。显然,在我们对docker容器执行某些操作之前,它需要运行。关于文件夹,您可以使用bash概要文件将其与启动shell脚本结合使用。也就是说,在我看来,运行后脚本看起来更干净。这仍然不能回答我的问题:如何在构建期间或在索引已经运行时添加索引。顺便说一句,在这种特定情况下,我的解决方案的第3步是:RUN/docker-entrypoint.sh elasticsearch-p/tmp/epid&/utils/wait-for-it.sh-t0 localhost:9200--curl-XPUT'http://127.0.0.1:9200/myindex'-d@index.json;kill$(cat/tmp/epid)和wait$(cat/tmp/epid);出口0代码>不需要额外的脚本,尽管把整个命令都放在里面有点难看。很好奇这在今天是如何实现的?另外,您在es docker source的答案中提到的“docker entrypoint.sh”脚本是吗?@Bryce我认为,从最初的答案开始,它没有变得更容易。但这并不意味着没有其他选择。您可以在本地插入elasticsearch后保留数据文件夹的副本,然后在docker运行时将其作为卷装载。如果你想运行任意的ES命令,至少这是你能做的最好的了。如果您想要的只是索引,那么可以通过简单的add指令将其添加为索引模板:addmyindex.json/etc/elasticsearch/templates/
(没有尝试过,只是猜测一下命令)。请记住,这种方法不创建索引,只定义其mappings@Bryce,我看到的只是elastic有更好的docker支持,但我还没有看到postgresql使用/docker-entrypoint-initdb.d/文件夹填充数据库的任何说明。如果能发生,我会很高兴的;-)fatih tekin回答的是同一个思维过程+1阅读这个答案,并用它来构建,用curl替换依赖项。
FROM elasticsearch:5.5.1 AS esbuilder
ADD script.sh path/to/insert/script.sh
RUN apt-get update \
&& apt-get install procps -y \
&& apt-get install httping -y \
&& /docker-entrypoint.sh elasticsearch -d -E path.data=/tmp/data \
&& while ! httping -qc1 http://localhost:9200 ; do sleep 1 ; done \
&& path/to/insert/script.sh \
&& apt-get clean
FROM elasticsearch:5.5.1
COPY --from=esbuilder /tmp/data/ /usr/share/elasticsearch/data/