1.下载镜像
docker pull elasticsearch:7.18.4
刚开始下载的es 8以上版本的,代码不能跑通,报错,代码如下:
from elasticsearch import Elasticsearch
import datetime
es = Elasticsearch(hosts="http://192.168.103.27:19200/")
with open('elasticsearch_knowledge.csv') as files:
file = files.readlines()
print(file)
for i in range(len(file)):
sentence = file[i].split(':')
sentence0 = sentence[0]
sentence1 = sentence[1]
doc = {
'author': 'XXX',
'text': sentence1,
'timestamp': datetime.datetime.now(),
}
res = es.create(index=sentence0, doc_type="test-type", id=1, body=doc)
print(res)
res = es.get(index=sentence0, doc_type="test-type", id=1)
print(res['_source'])
es.indices.refresh(index="test-index")
res = es.search(index="test-index", body={"query": {"match_all": {}}})
# print("Got %d Hits:" % res['hits']['total'])
for hit in res['hits']['hits']:
print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])
然后,将es降级,但是另外一给问题出现,有些系统是centos的,使用yum下载,不习惯,且有时候下载包找不到,需要下载的有:
apt-get update
apt-get install openjdk-8-jdk
java -version
apt-get install openssh-server
apt-get install openssh-client
vim /etc/ssh/sshd_config
/etc/init.d/ssh restart
运行elasticsearch需要下载JDK,访问容器端口需要SSH,并且开22端口,这里涉及ubantu的ufw,之前没有开放22端口,能ping通,但且telnet容器外的es进程端口也能通,就是无法访问容器映射出来的端口,刚开始以为是防火墙的问题,关闭防火墙或者开启防火墙打开端口都不能telnet:
ufw enable
ufw status
ufw allow 19200
ufw reload
2.创建本地挂在到容器中的目录
$ mkdir -p /es/elasticsearch/config
$ mkdir -p /es/elasticsearch/data
$ echo "http.host: 0.0.0.0" >> /es/elasticsearch/config/elasticsearch.yml
3.启动容器
docker run --name elasticsearch -p 19022:22-p 19200:9200 -p 19300:9300 \
-e "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms64m -Xmx128m" \
-v /es/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /es/elasticsearch/data:/usr/share/elasticsearch/data \
-v /es/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
-it elasticsearch:7.17.4
4.进入容器并以后台运行方式运行es
docker exec -it elasticsearch /bin/bash
workdir:/usr/share/elasticsearch
bin/elasticsearch -d