## 前言
* 作者博客: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 "$@"
```