sudo docker pull elasticsearch:7.12.0
sudo docker images

mkdir /home/zx/es_docker
cd es_docker
mkdir logs config plugins data
chmod -R 777 es_docker
http.host: 0.0.0.0
# Uncomment the following lines for a production cluster deployment
#transport.host: 0.0.0.0
#discovery.zen.minimum_master_nodes: 1
#Password config
# 开启密码验证
xpack.security.enabled: true
http.cors.allow-methods: OPTIONS, HEAD, GET, POST, PUT, DELETE
##不添加无法使用head插件连接es,连接时在http:ip:port/?auth_user=elastic&auth_password=密码
http.cors.allow-headers: Content-Type,Accept,Authorization, x-requested-with
由于是yml文件配置,所以host后面的冒号,必须空格后才能输入0.0.0.0。也就是后面的ip地址前面需要空格。
sudo docker run --privileged=true --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms84m -Xmx512m" -v /home/zx/es_docker/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /home/zx/es_docker/data:/usr/share/elasticsearch/data -v /home/zx/es_docker/plugins:/usr/share/elasticsearch/plugins -d elasticsearch:7.12.0
sudo docker run
--privileged=true #授予逻辑卷访问权
--name elasticsearch
-p 9200:9200 #端口映射配置
-p 9300:9300
-e "discovery.type=single-node" #非集群模式
-e ES_JAVA_OPTS="-Xms84m -Xmx512m" #内存大小
-v /home/zx/es_docker/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml #挂载逻辑卷,绑定elasticsearch的配置文件
-v /home/zx/es_docker/data:/usr/share/elasticsearch/data #挂载逻辑卷,绑定elasticsearch的数据目录
-v /home/zx/es_docker/plugins:/usr/share/elasticsearch/plugins #挂载逻辑卷,绑定elasticsearch的件目录
-d elasticsearch:7.12.0
sudo docker ps -a

sudo docker logs elasticsearch

sudo docker exec -it elasticsearch bash
cd /bin
elasticsearch-setup-passwords interactive #此步为手动设置密码
#以下不是命令
Initiating the setup of passwords for reserved users elastic,apm_system,kibana,logstash_system,beats_system,remote_monitoring_user.
You will be prompted to enter passwords as the process progresses.
Please confirm that you would like to continue [y/N]y
Enter password for [elastic]: #在这里设置密码,注意最少六位,下面也一样
Reenter password for [elastic]:
Passwords do not match.
Try again.
Enter password for [elastic]:
Reenter password for [elastic]:
Enter password for [apm_system]:
Reenter password for [apm_system]:
Enter password for [kibana]:
Reenter password for [kibana]:
Enter password for [logstash_system]:
Reenter password for [logstash_system]:
Enter password for [beats_system]:
Reenter password for [beats_system]:
Enter password for [remote_monitoring_user]:
Reenter password for [remote_monitoring_user]:
Changed password for user [apm_system]
Changed password for user [kibana]
Changed password for user [logstash_system]
Changed password for user [beats_system]
Changed password for user [remote_monitoring_user]
Changed password for user [elastic]


docker pull mobz/elasticsearch-head:5
sudo docker run -d --name es-head -p 9100:9100 docker.io/mobz/elasticsearch-head:5
执行docker命令后,也可以使用docker ps -a来查看是否成功启动。不过更好的方式是和es服务链接上。
因此接下来首先需要修改es的config目录的配置文件elasticsearch.yml,在其后面增加两行语句:
http.cors.enabled: true
http.cors.allow-origin: "*"
##不添加无法使用head连接es,连接时在http:ip:port/?auth_user=elastic&auth_password=密码
http.cors.allow-headers: Content-Type,Accept,Authorization, x-requested-with
#主要目的就是允许跨域请求。
#由于修改了配置文件,所以需要重启一下es的服务。直接运行如下语句:
docker restart elasticsearch
输入
http://ip:port/?auth_user=elastic&auth_password=密码

curl -u elastic -H "Content-Type: application/json" -X POST "localhost:9200/_xpack/security/user/elastic/_password" --data '{"password":"新密码"}'