最近由于工作需要,要用Elasticsearch做搜索功能。

但是目前手里能够用来搭建集群的服务器只有一台,于是便想用docker搭建个Elasticsearch集群。

准备工作

阿里云code  https://code.aliyun.com/

先在阿里云code 新建个项目 名为:elasticsearch

建立完成后使用git

git config --global user.name "xxx"
git config --global user.email "xxx@xxx.com"

git clone git@code.aliyun.com:xxx/test.git
cd test
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master

cd existing_folder
git init
git remote add origin git@code.aliyun.com:xxx/test.git
git add .
git commit
git push -u origin master

随后新建文件:Dockerfile

Dockerfile 代码如下

FROM java:8-jre
MAINTAINER "30040852@qq.com"

RUN groupadd -r elasticsearch && useradd -r -g elasticsearch elasticsearch

ENV GOSU_VERSION 1.10

RUN set -ex \
    && mkdir -p /usr/local/java \
    && mkdir -p /usr/local/data \
    && mkdir -p /usr/local/data/elasticsearch \
    && mkdir -p /usr/local/data/elasticsearch/data \
    && mkdir -p /usr/local/data/elasticsearch/logs

RUN set -x \
    && apt-get update && apt-get install -y --no-install-recommends ca-certificates wget && rm -rf /var/lib/apt/lists/* \
    && 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 \
    && apt-get purge -y --auto-remove ca-certificates wget

COPY /soft/elasticsearch-6.1.3.tar.gz /usr/local/elasticsearch-6.1.3.tar.gz
COPY /soft/jdk-8u181-linux-x64.tar.gz /usr/local/jdk-8u181-linux-x64.tar.gz

RUN tar xzf /usr/local/jdk-8u181-linux-x64.tar.gz -C /usr/local/java/ && rm -rf /usr/local/jdk-8u181-linux-x64.tar.gz
RUN tar xzf /usr/local/elasticsearch-6.1.3.tar.gz -C /usr/local/ && rm -rf /usr/local/elasticsearch-6.1.3.tar.gz

ENV JAVA_HOME /usr/local/java/jdk1.8.0_181
ENV CLASSPATH .:%JAVA_HOME%\lib:%JAVA_HOME%\lib\dt.jar:%JAVA_HOME%\lib\tools.jar
ENV PATH /usr/local/elasticsearch-6.1.3/bin:%JAVA_HOME%\bin:%JAVA_HOME%\jre\bin:$PATH

WORKDIR /usr/local/elasticsearch-6.1.3

RUN set -ex \
    && for path in \
        ./data \
        ./logs \
        ./config \
        ./config/scripts \
    ; do \
        mkdir -p "$path"; \
        chown -R elasticsearch:elasticsearch "$path"; \
        chown -R elasticsearch:elasticsearch /usr/local/data; \
        chown -R elasticsearch:elasticsearch /usr/local/elasticsearch-6.1.3; \
        chown -R elasticsearch:elasticsearch /usr/local/data/elasticsearch/data; \
        chown -R elasticsearch:elasticsearch /usr/local/data/elasticsearch/logs; \
    done

COPY /config/elasticsearch.yml ./config/elasticsearch.yml

VOLUME /usr/local/elasticsearch-6.1.3/data

COPY docker-entrypoint.sh /

RUN chmod 777 /docker-entrypoint.sh

EXPOSE 9200 9300

ENTRYPOINT ["/docker-entrypoint.sh"]

CMD ["elasticsearch"]

简单说下,这个镜像里面主要是有JDK和Elasticsearch,安装包被我通过git上传到目录soft里面。

同时在config目录有一个elasticsearch的默认配置文件。

之后进入阿里云镜像仓库:https://cr.console.aliyun.com

新建命名空间,再创建镜像仓库

这个镜像仓库可以直接关联到刚刚的阿里云Code。

创建完成后,点击立即构建。

另外说一下,阿里云镜像层库,会自动根据阿里云Code的修改重新构建镜像。

这个时候我们已经有了一个Elasticsearch6.1.3的镜像了。

docker-entrypoint.sh 代码如下:

#!/bin/bash

set -e

if [ "${1:0:1}" = '-' ]; then
    set -- elasticsearch "$@"
fi

if [ "$1" = 'elasticsearch' -a "$(id -u)" = '0' ]; then
    chown -R elasticsearch:elasticsearch /usr/local/elasticsearch-6.1.3/data
    set -- gosu elasticsearch "$@"
fi

exec "$@"

 

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐