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

时间:2016-07-14 12:43:43

标签: bash elasticsearch docker dockerfile

我从官方存储库(版本2.3)下载了docker文件,现在我想构建映像并将一些本地数据(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

这是我构建映像的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 "$@"

2 个答案:

答案 0 :(得分:2)

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

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

它在你最后执行服务之前就已经运行了。

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

VOLUME /usr/share/elasticsearch/data

创建卷后,通常会忽略对目录的未来更改。

最后,在您的Dockerfile中,最后这行可能没有按您的想法行事,我会将其删除:

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

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

答案 1 :(得分:0)

@Klue,如果您仍然需要它...您需要将curl命令的-d选项更改为--data-binary。 -d剥离换行符。这就是你收到错误的原因。