## 前言
* 作者博客:http://www.zimug.com*
* 最近在github上看到了Elasticsearch官方的Dockerfile的文档,感觉不错,有必要学习一下。
* 包括了安全考虑,和镜像entrypoint的设计也很巧妙
### Elasticsearch2.3官方Dockerfile
* [github地址](https://github.com/docker-library/elasticsearch/tree/master/2.3)
## Dockefile解析
```
# 使用Dockerhu的java:8-jre作为基础镜像,elashticsearch依赖于jdk7以上版本
FROM java:8-jre
# elashticsearch不能用root用户运行,所以安装gosu.用法: ./gosu user-spec command [args],
# 这样可以用指定的用户,运行指定的程序,gosu版本是GOSU_VERSION
# wget下载,mktemp -d创建临时目录,gpg去公钥服务器下载公钥并校验
# 增加gosu执行权限,gosu nobody true切换到nobody用户,安全
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
# apt-key是Debian软件包的安全管理工具。每个发布的deb包,都是通过密钥认证的,apt-key用来管理密钥。
# 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
# 大版本,小版本,版本库URL
ENV ELASTICSEARCH_MAJOR 2.3
ENV ELASTICSEARCH_VERSION 2.3.3
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
# 安装ELASTICSEARCH
RUN set -x \
&& apt-get update \
&& apt-get install -y --no-install-recommends elasticsearch=$ELASTICSEARCH_VERSION \
&& rm -rf /var/lib/apt/lists/*
# 将ELASTICSEARCH的bin目录加入环境变量目录
ENV PATH /usr/share/elasticsearch/bin:$PATH
# 工作目录
WORKDIR /usr/share/elasticsearch
# 工作目录下面新建四个目录,并修改拥有者为elasticsearch
RUN set -ex \
&& for path in \
./data \
./logs \
./config \
./config/scripts \
; do \
mkdir -p "$path"; \
chown -R elasticsearch:elasticsearch "$path"; \
done
#将config目录放到工作目录下,config目录的配置内容请参考elasticsearch
COPY config ./config
# 数据卷映射
VOLUME /usr/share/elasticsearch/data
# 将入口执行文件放到"/"根目录下面
COPY docker-entrypoint.sh /
# 端口映射
EXPOSE 9200 9300
# 容器启动入口,/docker-entrypoint.sh是入口文件 ,elasticsearch是参数
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["elasticsearch"]
```
## 入口文件docker-entrypoint.sh解析
```
#!/bin/bash
# set -e 若shell中的指令不返回0,立即退出shell
set -e
# 第一个传入参数的第一个字符是"-"么?如果是,执行elasticsearch 和参数"$@"."$@"是参数列表
if [ "${1:0:1}" = '-' ]; then
set -- elasticsearch "$@"
fi
# 如果参数1是elasticsearch,并且是root用户
if [ "$1" = 'elasticsearch' -a "$(id -u)" = '0' ]; then
# 变更/usr/share/elasticsearch/data的拥有者为elasticsearch
chown -R elasticsearch:elasticsearch /usr/share/elasticsearch/data
# 设置参数列表"$@"= 1.gosu 2.elasticsearch 3.原始参数列表"$@"
set -- gosu elasticsearch "$@"
#注意脚本最后的exec "$@"实际就是:exec gosu elasticsearch "$@"
fi
# 如果参数中没有 elasticsearch,表示用户希望运行自己的其他进程
# 如通过 `bash` shell 进入容器内部
exec "$@"
```