繁体   English   中英

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

[英]Dockerfile with entrypoint for executing bash script

我从官方存储库(版本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 "$@"

从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应该在启动容器时运行,而不是在构建容器时运行。

@Klue,以防您仍然需要它。.您需要将curl命令上的-d选项更改为--data-binary。 -d删除换行符。 这就是为什么您会得到错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM