有些应用默认不允许root用户运行,来确保应用的安全性,这也会导致我们使用docker run后一些操作问题,用es安装ik分词器举例(es版本8.9.0,analysis-ik版本8.9.0)
1. 容器启动elasticsearch
如挂载方式,我在本地es目录下创建子目录文件{plugins/data/logs/config}
创建完成后赋值777权限(chmod -R 777 xxx),运行启动正常
version: '3.2'
services:
elasticsearch:
image: elasticsearch:8.9.0
volumes:
- /etc/localtime:/etc/localtime
- ./es/plugins:/usr/share/elasticsearch/plugins
- ./es/data:/usr/share/elasticsearch/data
- ./es/logs:/usr/share/elasticsearch/logs
- ./es/config:/usr/share/elasticsearch/config
ports:
- '9200:9200'
- '9300:9300'
container_name: es
restart: always
privileged: true
environment:
- 'discovery.type=single-node' #以单一节点模式启动
- 'ES_JAVA_OPTS=-Xms1g -Xmx1g' #设置使用jvm内存大小
networks:
- elk
kibana:
image: kibana:8.9.0
container_name: kibana
restart: always
volumes:
- /etc/localtime:/etc/localtime
- ./kibana/config:/usr/share/kibana/config
- ./kibana/data:/usr/share/kibana/data
ports:
- '5601:5601'
links:
- elasticsearch:es #可以用es这个域名访问elasticsearch服务
environment:
- ELASTICSEARCH_URL=http://elasticsearch:9200 #设置访问elasticsearch的地址
- 'elasticsearch.hosts=http://es:9200' #设置访问elasticsearch的地址
- I18N_LOCALE=zh-CN
networks:
- elk
depends_on:
- elasticsearch
networks:
elk:
ipam:
driver: default
2. 安装分词器analysis-ik
analysis-ik下载列表根据自己对应的es版本下载相同版本的ik,docker机器如无互联网权限可先下载本地复制容器内install
复制容器内:
docker cp elasticsearch-analysis-ik-8.9.0.zip es:/home
进入容器安装:
`[root@iZ3ic06r07y423aufi9oypZ elasticsearch]# docker exec -it es bash`
elasticsearch@a0ff9699bfd6:~$ cd bin/
elasticsearch@a0ff9699bfd6:~/bin$ ./elasticsearch-plugin install file:///home/elasticsearch-analysis-ik-8.9.0.zip
-> Installing file:///home/elasticsearch-analysis-ik-8.9.0.zip
-> Downloading file:///home/elasticsearch-analysis-ik-8.9.0.zip
[=================================================] 100%??
-> Failed installing file:///home/elasticsearch-analysis-ik-8.9.0.zip
-> Rolling back file:///home/elasticsearch-analysis-ik-8.9.0.zip
-> Rolled back file:///home/elasticsearch-analysis-ik-8.9.0.zip
Exception in thread "main" java.nio.file.AccessDeniedException: /usr/share/elasticsearch/plugins/.installing-16427832938312211034
at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:90)
xxxxx.......
安装报错,发现原因为权限问题,exec进入后为1000用户,工作目录下除了bin其他都是root权限,
重新运行一个新的es对比看下效果,结果发现当没有挂载文件时其余文件都是以elasticsearch:root为权限的,已挂载的是根据宿主机的权限一致,问题很明显了,
容器内部不支持sudo命令 su - root也不知道root密码的情况下
,宿主机新建useradd elasticsearch,对应挂载目录文件用户授权在安装正常运行,chown -R elasticsearch:root {plugins/data/logs/config}