Docker安装ElasticSearch

在Docker Hub上查询可用版本


ES可用版本

下载安装并运行8.0.1版本

(base) ArchitectRoad: joey$docker pull elasticsearch:8.0.1
(base) ArchitectRoad: joey$docker run -d -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" --name es elasticsearch:8.0.1

安装完8.0.1版本后,发现http://localhost:9200 无法访问,改为安装7.17.1版本

docker run -d --name es7 -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" elasticsearch:7.17.1

(base) ArchitectRoad: joey$docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                                                                                              NAMES

安装完成后,通过访问http://localhost:9200确认是否安装成功

安装版本信息

安装可视化工具,这里选择了elasticsearch-head

也可以选择其他工具,参照:https://zhuanlan.zhihu.com/p/105355487

docker pull mobz/elasticsearch-head:5

docker run -d -p 9100:9100 --name eh5 mobz/elasticsearch-head:5

访问http://localhost:9100,输入http://localhost:9200,进行连接,如果连接成功,就说明安装成功了

image.png

如果连接无反应,可能是访问es跨域问题,本地可以修改elasticsearch.yml开启跨域

(base) ArchitectRoad: joey$docker ps
CONTAINER ID   IMAGE                       COMMAND                  CREATED        STATUS         PORTS                                            NAMES
76c17eee63cd   mobz/elasticsearch-head:5   "/bin/sh -c 'grunt s…"   17 hours ago   Up 17 hours    0.0.0.0:9100->9100/tcp                           eh5
f52b34fae745   elasticsearch:7.17.1        "/bin/tini -- /usr/l…"   19 hours ago   Up 5 minutes   0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp   es7
d1aa0eae1c69   mysql:8.0                   "docker-entrypoint.s…"   44 hours ago   Up 24 hours    0.0.0.0:3306->3306/tcp, 33060/tcp                mysql
(base) ArchitectRoad: joey$docker exec -ti f52b34fae745 /bin/bash
root@f52b34fae745:/usr/share/elasticsearch# find ./ -name elasticsearch.yml
./config/elasticsearch.yml
root@f52b34fae745:/usr/share/elasticsearch# vi ./config/elasticsearch.yml

添加以下配置

#开启跨域访问支持,默认为false
http.cors.enabled: true
#跨域访问允许的域名地址,(允许所有域名)以上使用正则
http.cors.allow-origin: /.*/ 

如果是线上环境,考虑安全问题,需要指定允许访问的域名

如果elasticsearch head查询数据出错

请求网址: http://localhost:9200/events/_search
请求方法: POST
状态代码: 406 Not Acceptable

则还需要修改elasticsearch head中的数据提交方式

(base) ArchitectRoad: joey$docker ps
CONTAINER ID   IMAGE                       COMMAND                  CREATED        STATUS          PORTS                                            NAMES
76c17eee63cd   mobz/elasticsearch-head:5   "/bin/sh -c 'grunt s…"   17 hours ago   Up 17 hours     0.0.0.0:9100->9100/tcp                           eh5
f52b34fae745   elasticsearch:7.17.1        "/bin/tini -- /usr/l…"   20 hours ago   Up 14 minutes   0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp   es7
d1aa0eae1c69   mysql:8.0                   "docker-entrypoint.s…"   44 hours ago   Up 24 hours     0.0.0.0:3306->3306/tcp, 33060/tcp                mysql
(base) ArchitectRoad: joey$docker exec -ti 76c17eee63cd /bin/bash
root@76c17eee63cd:/usr/src/app# ls
Dockerfile    LICENCE         _site               elasticsearch-head.sublime-workspace  index.html    package.json          src
Gruntfile.js  README.textile  elasticsearch-head.sublime-project  grunt_fileSets.js         node_modules  plugin-descriptor.properties  test
root@76c17eee63cd:/usr/src/app# find ./ -name vendor.js
./_site/vendor.js

文件中有两处
contentType: "application/x-www-form-urlencoded"

修改为
contentType: "application/json;charset=UTF-8"

由于docker内无vi命令,可以先cp出来,改好后,再cp回去

(base) ArchitectRoad: joey$docker cp eh5:/usr/src/app/_site/vendor.js ./
进行修改
(base) ArchitectRoad: joey$docker cp ./vendor.js eh5:/usr/src/app/_site/vendor.js
重启容器
(base) ArchitectRoad: joey$docker restart eh5
查询index events中数据

你可能感兴趣的:(Docker安装ElasticSearch)