Installation of ElasticSearch in Docker (Ubuntu 18.04)
Tags: docker, es, elasticsearch, devops
Published: 2020-11-24 10:44:58

###### Info: This post is one of the article in context of this post: [Huawei Cloud ECS server notes](https://dev.to/jemaloq/huawei-cloud-ecs-server-notes-1gnj)



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: 
```bash
touch ~/launchers/docker_es_8881.sh
vi ~/launchers/docker_es_8881.sh
```
Fill the following code into this file:

```bash
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
```
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:
 ```bash
sudo chmod -R 777 /home/jemaloQ/docker/es_8881/elasticsearch/
```
However, I still had the same issue.

Then I tried to launch it without folder mapping using the following cmd line:
```bash
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
```
Now the container started to work. So I then copied its content to the mapping folder in host machine:
```bash
docker cp es_docker_8881:/usr/share/elasticsearch/ /home/jemaloQ/docker/es_8881/elasticsearch
```

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. 

```bash
sudo vi /home/jemaloQ/docker/es_8881/elasticsearch/config/elasticsearch.yml
```
Add the following lines into this config file and save
```bash
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/qq563y1alky4x1lsu0u1.PNG)

Now relaunch our container. Then enter the inside bash of the container and setup the password
```bash
sudo docker exec -it es_docker_8881 /bin/bash
cd bin/
./elasticsearch-setup-passwords interactive
```
Then setup a series of passwords in interactive mode:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/qt3ffrey00j0co4ghqtk.PNG)


### Connect ES via Python 
Now test our ElasticSearch database in Python:
```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()
```
It works!
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6rq6v2l2lqca7otdnzpj.png)