Docker 容器提供了一种在自己的环境中创建和打包应用程序的简单方法。 如果你有兴趣启动运行 Elasticsearch 的 Docker 容器,那么过程实际上非常简单。 在本文中,我们将向你展示如何创建镜像并使用 Dockerfile 启动 Elasticsearch Docker 容器。
前提条件
在继续执行本教程中概述的步骤之前,你需要具备一个关键的先决条件:你需要一个稳定且受支持的 Docker 版本,才能使用 Dockerfile 创建 Elasticsearch 映像。你可以使用如下的命令来检查你的 Docker 的版本:
$ docker --version
Docker version 20.10.0, build 7287ab3
Elasticsearch YAML 文件
针对定制的 Docker image,我们通常需要定制自己的 elasticsearch.yml 文件。然后,你需要使用 Dockerfile 的 COPY 命令将文件内容复制到容器中。我们首先来创建一个文件目录:
mkdir elasticsearch-docker && cd elasticsearch-docker
使用 Elasticsearch 和 Dockerfile 时,你可以使用 YAML 进行许多不同的配置更改。 以下 YAML 命令仅显示了你可以为在 Docker 中运行的 Elasticsearch 集群修改的一些内容:
elasticsearch.yml
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: cluster-liuxg
http://www.junse.cn/xx/yingwen/id/127531/
http://www.junse.cn/xx/yingwen/id/124845/
http://www.junse.cn/xx/yingwen/id/127833/
http://www.junse.cn/xx/yingwen/id/136407/
http://www.junse.cn/xx/yingwen/id/122790/
http://www.junse.cn/xx/yingwen/id/1126954/
http://www.junse.cn/xx/yingwen/id/1138428/
http://www.junse.cn/xx/yingwen/id/1138576/
http://www.junse.cn/xx/yingwen/id/137793/
http://www.junse.cn/xx/yingwen/id/1133896/
http://www.junse.cn/xx/yingwen/id/2124416/
http://www.junse.cn/xx/yingwen/id/1129689/
http://www.junse.cn/xx/yingwen/id/2136519/
http://www.junse.cn/xx/yingwen/id/1136662/
http://www.junse.cn/xx/yingwen/id/2123783/
http://www.junse.cn/xx/yingwen/id/1138873/
http://www.junse.cn/xx/yingwen/id/1132276/
http://www.junse.cn/xx/yingwen/id/126520/
http://www.junse.cn/xx/yingwen/id/1123551/
http://www.junse.cn/xx/yingwen/id/2138675/
http://www.junse.cn/xx/yingwen/id/1128833/
http://www.junse.cn/xx/yingwen/id/2135153/
http://www.junse.cn/xx/yingwen/id/130113/
http://www.junse.cn/xx/yingwen/id/124991/
http://www.junse.cn/xx/yingwen/id/2124717/
http://www.junse.cn/xx/yingwen/id/1126891/
http://www.junse.cn/xx/yingwen/id/1123447/
http://www.junse.cn/xx/yingwen/id/2129921/
http://www.junse.cn/xx/yingwen/id/131702/
http://www.junse.cn/xx/yingwen/id/1132329/
http://www.junse.cn/xx/yingwen/id/2123788/
http://www.junse.cn/xx/yingwen/id/2135228/
http://www.junse.cn/xx/yingwen/id/2126143/
http://www.junse.cn/xx/yingwen/id/2136275/
http://www.junse.cn/xx/yingwen/id/2125333/
http://www.junse.cn/xx/yingwen/id/1129732/
http://www.junse.cn/xx/yingwen/id/2128645/
http://www.junse.cn/xx/yingwen/id/2126194/
http://www.junse.cn/xx/yingwen/id/2126164/
http://www.junse.cn/xx/yingwen/id/1132535/
http://www.junse.cn/xx/yingwen/id/1134899/
http://www.junse.cn/xx/yingwen/id/1138699/
http://www.junse.cn/xx/yingwen/id/1123255/
http://www.junse.cn/xx/yingwen/id/132671/
http://www.junse.cn/xx/yingwen/id/126229/
http://www.junse.cn/xx/yingwen/id/125086/
http://www.junse.cn/xx/yingwen/id/1138136/
http://www.junse.cn/xx/yingwen/id/1124236/
http://www.junse.cn/xx/yingwen/id/1126452/
http://www.junse.cn/xx/yingwen/id/1123596/
http://www.junse.cn/xx/yingwen/id/126579/
http://www.junse.cn/xx/yingwen/id/123728/
http://www.junse.cn/xx/yingwen/id/127258/
http://www.junse.cn/xx/yingwen/id/137540/
http://www.junse.cn/xx/yingwen/id/2130135/
http://www.junse.cn/xx/yingwen/id/135691/
http://www.junse.cn/xx/yingwen/id/2124494/
http://www.junse.cn/xx/yingwen/id/2124299/
http://www.junse.cn/xx/yingwen/id/2133286/
http://www.junse.cn/xx/yingwen/id/2133446/
注意:请确保在同一行中运行 apt-get update 和 apt-get install 命令,如上例所示。 如果你不这样做,它将被缓存并且不会每次都更新。
我们使用如下的命令来 build docker image:
docker build -t liuxg/liuxg-elasticsearch $PWD
上面的 -t 是给这个镜像一个名称。我们使用 . 来表示当前的目录。等运行完上面的命令后,我们可以使用如下的命令来进行检查:
docker images
上面的命令显示:
我们可以看到一个新增加的 docker image。
另外一种方法是我们通过下载已经创建好的 Elasticsearch 镜像,然后把我们的配置文件直接拷入到镜像的容器中。下面的例子展示如何实现:
Dockerfile
FROM elasticsearch:7.10.1
# container creator
MAINTAINER orkb
# copy the configuration file into the container
COPY elasticsearch.yml /usr/share/elasticsearch/config
# expose the default Elasticsearch port
EXPOSE 9200 9300
注意:Docker 仅允许你从每个 Dockerfile 提取一个镜像,因此请确保不要使用多个 FROM 命令。 如果你确实尝试使用 FROM 提取多个镜像,它将仅使用最后一个命令构建一个镜像,而忽略之前的任何命令。
至此,我们已经成功地创建了我们的 docker 镜像。我们可以使用如下的命令来检查是否有刚才被创建的镜像运行的容器:
docker ps -a
docker ps -a | grep liuxg
上面的命令显示没有任何运行的容器。这是因为到目前为止仅创建了镜像。 你需要使用 docker run 命令从镜像创建容器。
启动一个 Elasticsearch 容器
你可以使用以下命令来运行先前创建的 liuxg/iuxg-elasticsearch Docker 镜像:
docker run --rm -it liuxg/liuxg-elasticsearch /bin/bash
你还可以使用 -p 选项公开默认的 Elasticsearch 端口 9200。以下是如何使用此选项的示例:
docker run --rm -p 9200:9200 liuxg/liuxg-elasticsearch
一旦我们启动这个容器,我们可以在 host 机器的命令行中打入如下的命令来进行查看:
curl -XGET http://localhost:9200
上面命令显示的结果为:
$ curl -XGET http://localhost:9200
{
"name" : "node1",
"cluster_name" : "cluster-liuxg",
"cluster_uuid" : "8mWE-_EcRSaLIAJ5MJD4Wg",
"version" : {
"number" : "7.10.1",
"build_flavor" : "default",
"build_type" : "deb",
"build_hash" : "1c34507e66d7db1211f66f3513706fdf548736aa",
"build_date" : "2020-12-05T01:00:33.671820Z",
"build_snapshot" : false,
"lucene_version" : "8.7.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
从上面我们可以看出来 cluster_name 以及 node 名字都是我们之前在 elasticsearch.yml 中所设置的。我们可以在浏览器中访问 localhost:9200 查看: