DEV Community

Cover image for Installation of ElasticSearch in Docker (Ubuntu 18.04)
Z. QIU
Z. QIU

Posted on • Updated on

Installation of ElasticSearch in Docker (Ubuntu 18.04)

Info: This post is one of the article in context of this post: Huawei Cloud ECS server notes

I have installed ES on our Huawei CLoud server using docker just now. It's simple, but there is still something important to be aware of.

Make it Run in Docker

Create firstly my launch file:

touch ~/launchers/docker_es_8881.sh
vi ~/launchers/docker_es_8881.sh
Enter fullscreen mode Exit fullscreen mode

Fill the following code into this file:

docker rm -f es_docker_8881
docker run -it -d --privileged=true --net bridge-network \
 --name es_docker_8881 \
 -p 8881:9200 \
 -e ES_JAVA_OPTS="-Xms256m -Xmx256m" \
 -e "discovery.type=single-node" \
 -v /home/jemaloQ/docker/es_8881/elasticsearch:/usr/share/elasticsearch \
 elasticsearch:7.5.2
Enter fullscreen mode Exit fullscreen mode

Then I tried to launch the docker container using this launch file (sh ~/launchers/docker_es_8881.sh), however, the launched container is always in exited status (check it by executing docker ps -a | grep es_8881).

Thus I tried firstly changing the access permission of the mapping directory:

sudo chmod -R 777 /home/jemaloQ/docker/es_8881/elasticsearch/
Enter fullscreen mode Exit fullscreen mode

However, I still had the same issue.

Then I tried to launch it without folder mapping using the following cmd line:

docker rm -f es_docker_8881
docker run -it -d --privileged=true --net bridge-network  --name es_docker_8881  -p 8881:9200  -e ES_JAVA_OPTS="-Xms256m -Xmx256m"  -e "discovery.type=single-node"  -v elasticsearch:7.5.2
Enter fullscreen mode Exit fullscreen mode

Now the container started to work. So I then copied its content to the mapping folder in host machine:

docker cp es_docker_8881:/usr/share/elasticsearch/ /home/jemaloQ/docker/es_8881/elasticsearch
Enter fullscreen mode Exit fullscreen mode

Then I tried again launching the docker container using our launch file (sh ~/launchers/docker_es_8881.sh), it works this time.

Setup Password of ES

By default, the ES database is not secured by a password. One needs to firstly enable the xpack feature.

sudo vi /home/jemaloQ/docker/es_8881/elasticsearch/config/elasticsearch.yml
Enter fullscreen mode Exit fullscreen mode

Add the following lines into this config file and save

http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
Enter fullscreen mode Exit fullscreen mode

Alt Text

Now relaunch our container. Then enter the inside bash of the container and setup the password

sudo docker exec -it es_docker_8881 /bin/bash
cd bin/
./elasticsearch-setup-passwords interactive
Enter fullscreen mode Exit fullscreen mode

Then setup a series of passwords in interactive mode:

Alt Text

Connect ES via Python

Now test our ElasticSearch database in Python:

from elasticsearch import Elasticsearch

#es = Elasticsearch('IP:9200')
#es = Elasticsearch('http://host:9200')
es = Elasticsearch('http://111.22.233.177:8881',http_auth=("elastic", "123456"))


es.indices.get_alias()
Enter fullscreen mode Exit fullscreen mode

It works!
Alt Text

Top comments (0)