The Open Source version of Elasticsearch is the best solution out there for many things. We can use it in our daily developer lives as a Search Engine to create a fast and relevant search experience for our users or as analytics storage to craft some nice dashboards.
If you want to try and work with Elasticsearch locally this post is for you. I will show you all you need to know to run and manage Elasticsearch on your local machine.
Docker
The easiest way to run and manage Elasticsearch locally is Docker. If you haven't installed Docker yet, follow these steps:
- Run this command to download the Docker installation script.
$ curl -fsSL https://get.docker.com -o install-docker.sh
- Once you downloaded the installation script run this command to install docker on your system.
$ sudo sh install-docker.sh
Running Elasticsearch
Assuming Docker is up and running, use the following command to run Elasticsearch:
$ docker run -p 9200:9200 --name elasticsearch -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch-oss:7.10.2-amd64
This command pulls the Elasticsearch Open Source (OSS) image tagged with version 7.10.2, binds the ports 9200 to your localhost, and sets the discovery type to "single-node."
Let’s see what all this means.
- Elasticsearch operates on port
9200
using the flag-p 9200:9200
we are binding this port from the docker container to our local machine. Making Elasticsearch available onhttp://localhost:9200
. - Using
—name elasticsearch
we name our docker container so that we can reference it easily later on. - When Elasticsearch is running in a production environment, it’s usually a Cluster of multiple Elasticsearch nodes running. These nodes communicate with each other over the network and are always ready to discover new nodes that want to join the cluster. Since we are running Elasticsearch on our local machine for development purposes it makes sense to disable this behavior. Passing the environmental variable
discovery.type=single-node
we are telling Elasticsearch that it shouldn’t look for other Elasticsearch nodes in the network. - And last we are pulling the OSS image of Elasticsearch (
docker.elastic.co/elasticsearch/elasticsearch-oss:7.10.2-amd64
). OSS stands for “Open Source Software.” and is the open-source version of Elasticsearch that is more lightweight than the full version and includes all necessary functionalities of the search and analytics engine.
The OOS version is Licensed under the Apache 2.0 license giving us the freedom to use, modify, and distribute Elasticsearch without any licensing restrictions.
After the docker run
command finishes running you can check if Elasticsearch is also running with curl
.
$ curl -X GET http://localhost:9200
If everything is correct you will see this output in your console
{
"name" : "0a25fd395139",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "nBknoxw0QyG-fHS8j9dW8w",
"version" : {
"number" : "7.10.2",
"build_flavor" : "oss",
"build_type" : "docker",
"build_hash" : "747e1cc71def077253878a59143c1f785afa92b9",
"build_date" : "2021-01-13T00:42:12.435326Z",
"build_snapshot" : false,
"lucene_version" : "8.7.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
Starting, Stopping, and Restarting Elasticsearch:
Once you have Elasticsearch running as a Docker container, you can control its lifecycle using the following commands:
- To stop Elasticsearch:
$ docker stop elasticsearch
This command stops the running Elasticsearch container gracefully.
- To start Elasticsearch:
$ docker start elasticsearch
This command starts the previously created Elasticsearch container.
- To restart Elasticsearch:
$ docker restart elasticsearch
This command restarts the Elasticsearch container and applies any configuration changes or recovers from crashes.
Dealing with Elasticsearch Crashes
If your Elasticsearch crashes and docker restart elasticsearch
will not fix the problem, you can always wipe all Elasticsearch data away and start new.
Elasticsearch stores its data in the /usr/share/elasticsearch/data/nodes
folder in the docker container. If you delete this folder you will have a fresh Elasticsearch to work with.
Use the following command to delete the nodes
folder within the docker container.
$ docker exec -it elasticsearch rm -rf /usr/share/elasticsearch/data/nodes
This command removes the Elasticsearch data stored in the container’s /usr/share/elasticsearch/data/nodes
directory. However, keep in mind that executing this command will result in data loss and you shouldn’t use it if you have data in Elasticsearch that you don’t want to lose.
After deleting the nodes
folder, it is essential to restart Elasticsearch to recreate the necessary data structures. Use the docker restart elasticsearch
command to do this.
Conclusion:
Running Elasticsearch with Docker provides a convenient way to manage Elasticsearch. By following the steps outlined you can quickly run Elasticsearch using Docker and start playing with it.
Now that you have Elasticsearch running, in a future post, I will show you how you can use Elasticsearch to create a powerful search for your application.
Top comments (0)