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="-Xms512m -Xmx512m" 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:9200curl: (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 0a9bb3fa153ddocker start 0a9bb3fa153dcurl 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/elasticsearchdocker cp 0a9bb3fa153d:/usr/share/elasticsearch/logs /Users/chenpeng/share/elasticsearchdocker cp 0a9bb3fa153d:/usr/share/elasticsearch/data /Users/chenpeng/share/elasticsearchdocker 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: truehttp.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
删除临时容器
docker stop 0a9bb3fa153ddocker 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
进入容器设置密码
docker exec -it elasticsearch bashbin/elasticsearch-reset-password --username elastic -i#bin/elasticsearch-setup-passwords interactive
curl -k -u elastic:123456 https://localhost:9200
启动成功
2.1 下载镜像
docker pull kibana:8.7.1
2.2 运行临时容器
docker run -idt --name kibana -p 5601:5601 kibana:8.7.1
2.3 创建本地挂载文件
docker cp kibana:/usr/share/kibana/config /Users/chenpeng/share/kibana
修改配置文件
# Default Kibana configuration for docker targetserver.host: "0"server.shutdownTimeout: "5s"elasticsearch.hosts: [ "http://localhost:9200" ] # 记得修改ipmonitoring.ui.container.elasticsearch.enabled: truei18n.locale: "zh-CN"
2.4 停掉删掉镜像并重新创建
docker stop kibanadocker rm kibana
docker run -d --name kibana -p 5601:5601 -v /Users/chenpeng/share/kibana/config:/usr/share/kibana/config kibana:8.7.1
2.5 访问页面发现需要es token
2.6 进入es容器获取token
docker exec -it 61c83e2b4f2b bashbin/elasticsearch-create-enrollment-token --scope kibana
将token粘到kibana中,提示输入验证码
2.7 进入kibana容器,获取验证码
docker exec -it kibana bashbin/kibana-verification-code#Your verification code is: 976 363
输入用户名密码elastic 123456
来源地址:https://blog.csdn.net/chen_peng7/article/details/131037354