You're reading for free via D's Friend Link. Become a member to access the best of Medium.

Member-only story

How to run a local Elasticsearch using docker (in under 5 min)?

D
FAUN — Developer Community 🐾
4 min readJan 31, 2025
Photo by Michael Walter on Unsplash

Read this for free here.

Elasticsearch is a powerful database that supports a variety of full text search related use cases. Their speed is a definite plus to any application’s arsenal. In order to play around with elasticsearch, it is possible to set up a local instance of this database and start messing around with it if you follow the rest of this article.

There are a number of sub applications in the elasticsearch ecosystem. We will need 2 of them today. The first is the elasticsearch application and the second is kibana. The former is the actual database powering this whole experience while the second is a User Interface with various features including developer tools that will allow us to interact with the data more effectively. Enough yapping, let us get into the actual set up (take note that a more updated set-up is shown in the next section using Volumes instead of Links).

  1. Elasticsearch
  2. Kibana

First start an elasticsearch instance:

Simply start an instance with a mounted volume. Be sure to disable all ssl in localhost to allow http connections to be used (this is localhost after all).

docker run --name elasticsearch \
> -e "discovery.type=single-node" \
> -e "ELASTIC_PASSWORD=password" \
> -e "xpack.security.enabled=false" \
> -e "xpack.security.transport.ssl.enabled=false" \
> -e "xpack.security.http.ssl.enabled=false" \
> -p 9200:9200 \
> -v esdata:/usr/share/elasticsearch/data \
> --ulimit memlock=-1:-1 \
> --ulimit nofile=65536:65536 \
> docker.elastic.co/elasticsearch/elasticsearch:8.7.0

You will know the above works by heading over to https://localhost:9200 and using your username and password to log in. If by any chance the password does not work or you need to reset it simply use the following:

# Connect to the docker container
docker exec -it <elastic_container> /bin/sh
bin/elasticsearch-reset-password -u elastic
docker restart <elastic_container>

The above will start a shell to the docker container of elasticsearch and reset the password.

Second start a kibana instance:

This uses the soon to be deprecated feature links to connect one docker container to another. In this case, the kibana docker container will connect to the other elasticsearch database.

docker run --name kibana --link <elastic_container>:elasticsearch -p 5601:5601   -e "ELASTICSEARCH_HOSTS=http://elasticsearch:9200"  docker.elastic.co/kibana/kibana:8.7.0

Connect to https://localhost:5601?code=<some_code>.The code will be shown in the kibana output when the container is run. Once you head over to the User Interface you may be asked to input an enrolment token.

Enrolement Token

This can be obtained by running the command below in the Kibana Container.

# Reset the environment token
bin/elasticsearch-create-enrollment-token --scope kibana

Alternative Docker Set Up with Volumes instead of Links

Follow this guide to use Volumes to recreate the above set up instead.

Brief Overview of Elasticsearch

Elasticsearch is a powerful database and can be used to perform full text search as mentioned earlier. This means that if I have a data field called “fruits”, and would like to input “orange” into it or “pear”, I can search for these fruits later using a query in the developer tools of Kibana.

POST fruits/_search
{
"query": {
"term": {
"fruit": "orange"
}
}
}

The above will search the index fruits for any records with the word orange. Wait hold up, how do we even create an index? How do we put data into the index?

Create an Index

Elasticsearch actually has some pretty good documentation that you can explore. It’s much better than this article here. Nevertheless this article will attempt to be a distilled version of the documentation with some explanation peppered in between.

PUT fruits

This will create the index

Putting data into the index

POST fruits/_doc/
{
"fresh": false,
"fruit": "Orange",
"price": 50
}

The above will create a document in the index with the above data. Notice that elasticsearch document support a variety of types including integers, booleans and strings.

Queries

Elasticsearch even boasts a number of different query types. There are match queries and term queries. These are the 2 most popular types of queries. A term query searches elasticsearch’s documents for the term exactly as it is. The term “pear” will be used to search exactly for another term “pear”. On the other hand, a match query goes through the analyzer and the results of the analyzer will be used to search against the document. An explanation about this can be found here by another Medium Article.

Analyzers

Analyzers consist of both tokenizers and filters.

  • Tokenizers are ways to break up an input (e.g. hello => h, he, hel, hell, hello, e, el, ell, ello etc)
  • Filters are ways to transform the input (e.g. Lowercase, Punctuation removal etc)

Analyzers can be a powerful way to index documents because it allows for partial matching of search terms.

👋 If you find this helpful, please click the clap 👏 button below a few times to show your support for the author 👇

🚀Join FAUN Developer Community & Get Similar Stories in your Inbox Each Week

Published in FAUN — Developer Community 🐾

We help developers learn and grow by keeping them up with what matters. 👉 www.faun.dev

Written by D

An aspiring Robotics Researcher. I am currently in my 4th year of undergraduate studies. I am working on optimising the ROS navigation packages. Follow 4 more!

No responses yet

What are your thoughts?