一. docker 安装 ES
1. 下载镜像
docker pull elastcisearch:8.7.1
2. 启动镜像
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms256m -Xmx256m" elasticsearch:8.7.1
参数说明
-d
后台启动–name
起别名即:NAMES-p 9200:9200
将端口映射出来
elasticsearch的9200端口是供外部访问使用;9300端口是供内部访问使用集群间通讯-e "discovery.type=single-node"
单节点启动-e ES_JAVA_OPTS="-Xms256m -Xmx256m"
限制内存大小
3.访问es
curl http://localhost:9200
curl: (52) Empty reply from server
4.进入docker
docker exec -it 0a9bb3fa153d bash
docker exec 在运行的容器中执行命令
-
-i :即使没有附加也保持STDIN 打开
-
-t :分配一个伪终端
找到es配置文件位置
/usr/share/elasticsearch/config/elasticsearch.yml
将docker容器中的配置文件拷贝到本地文件
docker cp 0a9bb3fa153d:/usr/share/elasticsearch/config/elasticsearch.yml /usr/local/share/elasticsearch/elasticsearch.yml
将 xpack.security.enable: true 改为 xpack.security.enable: false
再将修改后的配置文件拷贝回docker中
docker cp /usr/local/share/elasticsearch/elasticsearch.yml 0a9bb3fa153d:/usr/share/elasticsearch/config/elasticsearch.yml
重启docker
docker stop 0a9bb3fa153d
docker start 0a9bb3fa153d
curl localhost:9200
{
"name" : "0a9bb3fa153d",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "8hyCPOJESfKParQcDmGgtw",
"version" : {
"number" : "8.7.1",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "f229ed3f893a515d590d0f39b05f68913e2d9b53",
"build_date" : "2023-04-27T04:33:42.127815583Z",
"build_snapshot" : false,
"lucene_version" : "9.5.0",
"minimum_wire_compatibility_version" : "7.17.0",
"minimum_index_compatibility_version" : "7.0.0"
},
"tagline" : "You Know, for Search"
}
5. 决定将重新建立docker镜像,本机创建文件夹,并将docker容器中的配置文件等信息拷贝出来,重新起容器并挂载外部文件夹
mkdir -p /Users/chenpeng/share/elasticsearch/{config,data,logs,plugins}
docker cp 0a9bb3fa153d:/usr/share/elasticsearch/config /Users/chenpeng/share/elasticsearch
docker cp 0a9bb3fa153d:/usr/share/elasticsearch/logs /Users/chenpeng/share/elasticsearch
docker cp 0a9bb3fa153d:/usr/share/elasticsearch/data /Users/chenpeng/share/elasticsearch
docker cp 0a9bb3fa153d:/usr/share/elasticsearch/plugins /Users/chenpeng/share/elasticsearch
注意mac系统不要把文件夹放在系统文件夹中,容易没有权限。我之前放在宿主的/usr/share/elasticsearch目录下,不管如何更改权限,docker容器都没有权限访问。
确保有以下几个配置,原有的配置可以不改动
cluster.name: "docker-cluster"
network.hosts:0.0.0.0
# 跨域
http.cors.allow-origin: "*"
http.cors.enabled: true
http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
删除临时容器
docker stop 0a9bb3fa153d
docker rm 0a9bb3fa153d
重新起容器并挂起文件夹
docker run -d --name elasticsearch \
-p 9200:9200 \
-p 9300:9300 \
-e "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms512m -Xmx512m" \
-e TAKE_FILE_OWNERSHIP=true \
-v /Users/chenpeng/share/elasticsearch/logs:/usr/share/elasticsearch/logs \
-v /Users/chenpeng/share/elasticsearch/data:/usr/share/elasticsearch/data \
-v /Users/chenpeng/share/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
-v /Users/chenpeng/share/elasticsearch/config:/usr/share/elasticsearch/config \
elasticsearch:8.7.1
访问curl localhost:9200
curl localhost:9200
{
"name" : "61c83e2b4f2b",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "Wp0M92PxQYyYbJVL_2IKmA",
"version" : {
"number" : "8.7.1",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "f229ed3f893a515d590d0f39b05f68913e2d9b53",
"build_date" : "2023-04-27T04:33:42.127815583Z",
"build_snapshot" : false,
"lucene_version" : "9.5.0",
"minimum_wire_compatibility_version" : "7.17.0",
"minimum_index_compatibility_version" : "7.0.0"
},
"tagline" : "You Know, for Search"
}
启动成功