1. 下载镜像
docker pull docker.elastic.co/elasticsearch/elasticsearch:8.6.2
2. 启动容器
2.1 创建配置文件docker-compose.yml
:
version: "3.3"
services:
elasticsearch:
container_name: elasticsearch
image: docker.elastic.co/elasticsearch/elasticsearch:8.6.2
restart: always
ports:
- 9200:9200
- 9300:9300
environment:
# 指定单节点启动
- discovery.type=single-node
- "ES_JAVA_OPTS=-Xms2g -Xmx8g"
# 开启内存锁定
- bootstrap.memory_lock=true
# 不开启权限验证
- xpack.security.enabled=false
# mem_limit: ${MEM_LIMIT}
ulimits:
# 取消内存相关限制 用于开启内存锁定
memlock:
soft: -1
hard: -1
2.2 运行容器docker-compose up
3. 复制出配置文件、插件、数据目录
# 创建一个目录, 用来存放elasticsearch配置和数据
mkdir /data/elasticsearch
cd /data/elasticsearch
# 复制配置文件目录
docker cp elasticsearch:/usr/share/elasticsearch/config /data/elasticsearch/config
# 复制插件目录
docker cp elasticsearch:/usr/share/elasticsearch/plugins /data/elasticsearch/plugins
# 复制数据目录
docker cp elasticsearch:/usr/share/elasticsearch/data /data/elasticsearch/data
# 修改文件权限, 不然Docker容器无法写数据
cd /data
chgrp -R 1000 elasticsearch
chown -R 1000 elasticsearch
4. 重启容器
# 先移除容器
docker stop elasticsearch
docker rm elasticsearch
docker-compose的安装参考 https://segmentfault.com/a/1190000043696968
修改docker-compose.yml
配置
version: "3.3"
services:
elasticsearch:
container_name: elasticsearch
image: docker.elastic.co/elasticsearch/elasticsearch:8.6.2
restart: always
# 映射目录到宿主机
volumes:
- /data/elasticsearch/config:/usr/share/elasticsearch/config
- /data/elasticsearch/data:/usr/share/elasticsearch/data
- /data/elasticsearch/plugins:/usr/share/elasticsearch/plugins
ports:
- 9200:9200
- 9300:9300
environment:
# 指定单节点启动
- discovery.type=single-node
- "ES_JAVA_OPTS=-Xms2g -Xmx8g"
# 开启内存锁定
- bootstrap.memory_lock=true
# 不开启权限验证
- xpack.security.enabled=false
# mem_limit: ${MEM_LIMIT}
ulimits:
# 取消内存相关限制 用于开启内存锁定
memlock:
soft: -1
hard: -1
再启动一个新的容器docker-compose up
如需安装IK分词器, 下载解压到/data/elasticsearch/plugins, 重启ES即可;
附IK分词器各版本下载地址: https://github.com/medcl/elasticsearch-analysis-ik/releases。
问题记录
问题1
容器启动报错:
elasticsearch | ERROR: Elasticsearch exited unexpectedly
elasticsearch exited with code 78
解决方法:
- 修改配置
vim /etc/sysctl.conf
最后添加一行vm.max_map_count=262144
- 更新配置
sysctl -p
查看更改后的值sysctl -a | grep vm.max_map_count
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。