Dockerfile用于执行bash脚本的入口点

我从官方存储库(版本2.3)下载了docker文件,现在我想构build映像并将一些本地数据( test.json )上载到容器中。 仅仅运行COPY test.json /usr/share/elasticsearch/data/是不够的,因为在这种情况下数据的索引是没有完成的。

我想实现的是能够运行sudo docker run -d -p 9200:9200 -p 9300:9300 -v /home/gosper/tests/tempESData/:/usr/share/elasticsearch/data test/elasticsearch ,并执行后,我希望能够看到http://localhost:9200/tests/test/999的映射数据。

如果我使用下面给出的Dockerfile和* sh脚本,那么我得到以下错误: Failed to connect to localhost port 9200: Connection refused

这是我从中build立图像的Dockerfile:

 FROM java:8-jre # grab gosu for easy step-down from root ENV GOSU_VERSION 1.7 RUN set -x \ && wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture)" \ && wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture).asc" \ && export GNUPGHOME="$(mktemp -d)" \ && gpg --keyserver ha.pool.sks-keyservers.net --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4 \ && gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu \ && rm -r "$GNUPGHOME" /usr/local/bin/gosu.asc \ && chmod +x /usr/local/bin/gosu \ && gosu nobody true # https://www.elastic.co/guide/en/elasticsearch/reference/current/setup-repositories.html # https://packages.elasticsearch.org/GPG-KEY-elasticsearch RUN apt-key adv --keyserver ha.pool.sks-keyservers.net --recv-keys 46095ACC8548582C1A2699A9D27D666CD88E42B4 ENV ELASTICSEARCH_VERSION 2.3.4 ENV ELASTICSEARCH_REPO_BASE http://packages.elasticsearch.org/elasticsearch/2.x/debian RUN echo "deb $ELASTICSEARCH_REPO_BASE stable main" > /etc/apt/sources.list.d/elasticsearch.list RUN set -x \ && apt-get update \ && apt-get install -y --no-install-recommends elasticsearch=$ELASTICSEARCH_VERSION \ && rm -rf /var/lib/apt/lists/* ENV PATH /usr/share/elasticsearch/bin:$PATH WORKDIR /usr/share/elasticsearch RUN set -ex \ && for path in \ ./data \ ./logs \ ./config \ ./config/scripts \ ; do \ mkdir -p "$path"; \ chown -R elasticsearch:elasticsearch "$path"; \ done COPY config ./config VOLUME /usr/share/elasticsearch/data COPY docker-entrypoint.sh / EXPOSE 9200 9300 RUN chmod +x /docker-entrypoint.sh ENTRYPOINT ["/docker-entrypoint.sh"] CMD ["elasticsearch"] COPY template.json /usr/share/elasticsearch/data/ RUN /bin/bash -c "source /docker-entrypoint.sh" 

这是docker-entrypoint.sh ,我在其中添加了curl -XPOST http://localhost:9200/uniko-documents/document/978-1-60741-503-9 -d "/usr/share/elasticsearch/data/template.json"

 #!/bin/bash set -e # Add elasticsearch as command if needed if [ "${1:0:1}" = '-' ]; then set -- elasticsearch "$@" fi # Drop root privileges if we are running elasticsearch # allow the container to be started with `--user` if [ "$1" = 'elasticsearch' -a "$(id -u)" = '0' ]; then # Change the ownership of /usr/share/elasticsearch/data to elasticsearch chown -R elasticsearch:elasticsearch /usr/share/elasticsearch/data set -- gosu elasticsearch "$@" #exec gosu elasticsearch "$BASH_SOURCE" "$@" fi curl -XPOST http://localhost:9200/tests/test/999 -d "/usr/share/elasticsearch/data/test.json" # As argument is not related to elasticsearch, # then assume that user wants to run his own process, # for example a `bash` shell to explore this image exec "$@" 

从docker-entrypoint.sh中移除以下内容:

 curl -XPOST http://localhost:9200/tests/test/999 -d "/usr/share/elasticsearch/data/test.json" 

它在最后执行服务之前运行。

在Dockerfile中,在修改目录的任何命令之后移动以下内容:

 VOLUME /usr/share/elasticsearch/data 

创build卷后,通常会忽略目录的更改。

最后,在你的Dockerfile中,最后这行可能不会做你的想法,我会删除它:

 RUN /bin/bash -c "source /docker-entrypoint.sh" 

在启动容器时应该运行entrypoint.sh,而不是在构build容器时运行。

@Klue万一你仍然需要它..你需要改变你的curl命令的-d选项到–data-binary。 -d剥离换行符。 这就是为什么你会收到错误。