下載鏡像
docker pull elasticsearch:6.8.2
查看鏡像
docker images
控制臺輸出
C:\Users\Administrator>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
elasticsearch 7.6.2 f29a1ee41030 5 weeks ago 791MB
mysql 5.7 b598110d0fff 3 months ago 435MB
mysql 8.0 ed1ffcb5eff3 4 months ago 456MB
rabbitmq latest 6addf4b6a4ef 4 months ago 151MB
redis 5.0.5 63130206b0fa 7 months ago 98.2MB
elasticsearch 6.8.2 dbf758a9f11b 9 months ago 900MB
java 8 d23bdf5b1b1b 3 years ago 643MB
java 9 1800de06a3eb 3 years ago 579MB
運行(先不掛載目錄)
docker run -it -p 9200:9200 -p 9300:9300 --name myes elasticsearch:6.8.2
驗證是否運行成功
docker ps
C:\Users\Administrator>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7c16b70758f9 elasticsearch:6.8.2 "/usr/local/bin/dock…" 8 minutes ago Up 8 minutes 0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp myes
瀏覽器訪問http://127.0.0.1:9200/
{
"name" : "NrViUT9",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "1QvG9qWCR-ibuB7WS2yCnw",
"version" : {
"number" : "6.8.2",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "b506955",
"build_date" : "2019-07-24T15:24:41.545295Z",
"build_snapshot" : false,
"lucene_version" : "7.7.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
進入容器
C:\Users\Administrator>docker exec -it myes /bin/bash
[root@7c16b70758f9 elasticsearch]#
運行l(wèi)s查看目錄:(這里我們要將/usr/share/elasticsearch/config配置文件目錄和/usr/share/elasticsearch/data數(shù)據(jù)目錄掛載到外部)
[root@7c16b70758f9 elasticsearch]# ll
total 476
-rw-r--r-- 1 elasticsearch root 13675 Jul 24 2019 LICENSE.txt
-rw-r--r-- 1 elasticsearch root 427502 Jul 24 2019 NOTICE.txt
-rw-r--r-- 1 elasticsearch root 8519 Jul 24 2019 README.textile
drwxr-xr-x 3 elasticsearch root 4096 Jul 24 2019 bin
drwxrwxr-x 1 elasticsearch root 4096 May 2 12:47 config
drwxrwxr-x 1 elasticsearch root 4096 May 2 12:47 data
drwxr-xr-x 3 elasticsearch root 4096 Jul 24 2019 lib
drwxrwxr-x 1 elasticsearch root 4096 May 2 12:47 logs
drwxr-xr-x 31 elasticsearch root 4096 Jul 24 2019 modules
drwxr-xr-x 2 elasticsearch root 4096 Jul 24 2019 plugins
[root@7c16b70758f9 elasticsearch]#
新開cmd窗口,通過docker命令將配置和數(shù)據(jù)拷貝到宿主機外部(我本機上docker掛載相關的數(shù)據(jù)都放在D:\develop\dockerdata目錄下)
C:\Users\Administrator>docker cp myes:/usr/share/elasticsearch/config D:\develop\dockerdata\elasticsearch
C:\Users\Administrator>docker cp myes:/usr/share/elasticsearch/data D:\develop\dockerdata\elasticsearch
C:\Users\Administrator>
結束并刪除當前容器
C:\Users\Administrator>docker stop myes
myes
C:\Users\Administrator>docker rm myes
myes
重新啟動一個容器(并掛載配置和數(shù)據(jù))
docker run -dit --name myes -p 9200:9200 -p 9300:9300 -v D:\develop\dockerdata\elasticsearch\config\:/usr/share/elasticsearch/config -v D:\develop\dockerdata\elasticsearch\data\:/usr/share/elasticsearch/data elasticsearch:6.8.2
查看是否運行成功
docker ps
瀏覽器訪問 http://127.0.0.1:9200/
elasticserach.yml配置文件
如有需要,修改D:\dockerdata\dockeres\config\elasticserach.yml配置文件,6.8.2版本的配置文件內(nèi)容如下:
cluster.name: "docker-cluster"
network.host: 0.0.0.0
將 transport.host: 0.0.0.0 前的#去掉后保存文件退出。其作用是允許任何ip地址訪問elasticsearch .開發(fā)測試階段可以這么做,生產(chǎn)環(huán)境下指定具體的IP,我本地的文件最終為
cluster.name: "docker-cluster"
network.host: 0.0.0.0
transport.host: 0.0.0.0
http.cors.enabled: true
http.cors.allow-origin: "*"
相關配置說明
http.cors.enabled 是否支持跨域,默認為false
http.cors.allow-origin 當設置允許跨域,默認為*,表示支持所有域名,如果我們只是允許某些網(wǎng)站能訪問,那么可以使用正則表達式。比如只允許本地地址。 /https?:\/\/localhost(:[0-9]+)?/
http.cors.max-age 瀏覽器發(fā)送一個“預檢”O(jiān)PTIONS請求,以確定CORS設置。最大年齡定義多久的結果應該緩存。默認為1728000(20天)
http.cors.allow-methods 允許跨域的請求方式,默認OPTIONS,HEAD,GET,POST,PUT,DELETE
http.cors.allow-headers 跨域允許設置的頭信息,默認為X-Requested-With,Content-Type,Content-Length
http.cors.allow-credentials 是否返回設置的跨域Access-Control-Allow-Credentials頭,如果設置為true,那么會返回給客戶端。
配置文件修改完之后,需要重新啟動容器方可生效
docker restart myes