Elasticsearch is a distributed search and analytics engine built on Apache Lucene. It provides a distributed, multitenant-capable full-text search engine with an HTTP web interface and schema-free JSON documents. Elasticsearch has quickly become the most popular search engine and is commonly used for log analytics, full-text search, security intelligence, business analytics, and operational intelligence use cases.
In this guide, we will learn how to install Elasticsearch using docker.
Table of Content
- Ensure docker is installed
- Using docker run command to launch Elasticsearch
- Using docker-compose to run Elasticsearch
- Using Systemd to run Elasticsearch docker
1. Ensure that docker is installed
Before proceeding, ensure docker is installed locally. Please check out these guides if you do not have docker installed:
- How to Install and Configure Elasticsearch on Ubuntu 20.04
- How to Install and Configure Elasticsearch on Debian 11
- Using Ansible to install and configure Elasticsearch on Ubuntu 20.04
2. Using docker run command to launch Elasticsearch
We are going to use the docker run
command to achieve our goal. The version of Elasticsearch that we want is <meta charset="utf-8">elasticsearch/elasticsearch:7.16.3
– the latest version of Elasticsearch image as of the writting of this guide.
First create a volume. A local docker volume can be used to mount container data so it persist container recreations.
docker volume create elast_data
Then run the container with this command:
$ docker run -d \
-p 9200:9200 -p 9300:9300 \
-e "discovery.type=single-node" \
-v elast_data:/usr/share/elasticsearch/data \
docker.elastic.co/elasticsearch/elasticsearch:7.16.3
Unable to find image 'docker.elastic.co/elasticsearch/elasticsearch:7.16.3' locally
7.16.3: Pulling from elasticsearch/elasticsearch
c661c71060f1: Pull complete
d990ef6f2b05: Pull complete
82a269b58846: Pull complete
b55a8747471f: Pull complete
fc908a7aa21e: Pull complete
71c4eb4161c5: Pull complete
746cbfc9ba83: Pull complete
a82834885d6f: Pull complete
a19c3acab779: Pull complete
Digest: sha256:0efc3a054ae97ad00cccc33b9ef79ec022970b2a9949893db4ef199edcdca2ce
Status: Downloaded newer image for docker.elastic.co/elasticsearch/elasticsearch:7.16.3
57ba51f4a23615ebde5f3063b04296021c3d43a78a45658b4a020af161d93c96
In the above command:
- The
-d
instructs docker container to run as a detached process. It run container in background and print container ID -p
is for port mapping. We are instructing the container to expose the container port externally. Container port 9200 and 9300 is mapped to host port9200
and 9300. That means the service can be accessed throughlocalhost:9200
- The
-e "discovery.type=single-node"
defines an enviroment variable - The
docker.elastic.co/elasticsearch/elasticsearch:7.16.3
defines the image that we are running. We are running elasticsearch image version 7.16.3 .
To check that our container is running as expected, use the docker ps
command:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f56f14827473 docker.elastic.co/elasticsearch/elasticsearch:7.16.3 "/bin/tini -- /usr/l…" 3 seconds ago Up 3 seconds 0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 127.0.0.1:9300->9300/tcp my-elast
In my case the container is running as my-elast
the name we gave it. We can login to the container using the docker exec
command while executing /bin/bash
interactively. Here we are also logging in to posgtres with the credentials we specified above and checking the version.
$ docker exec -it my-elast /bin/sh
sh-5.0# curl -X GET "localhost:9200/"
{
"name" : "f56f14827473",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "EOSx1cc_SNSO3NxbyVZD-A",
"version" : {
"number" : "7.16.3",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "4e6e4eab2297e949ec994e688dad46290d018022",
"build_date" : "2022-01-06T23:43:02.825887787Z",
"build_snapshot" : false,
"lucene_version" : "8.10.1",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
sh-5.0#
If you need to clean up the container when not in use, you can stop and remove the container using this command:
docker stop my-elast
# Removing
docker rm my-elast
3. Using docker-compose to run Elasticsearch
We can achieve the same functionality with docker-compose
. Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services.
Docker Compose allows you to define the service (Redis in our case) with properties like the image
to use, command
to overide the default Dockerfile command and andports
to expose.
Here is how we would use docker-compose
to achieve the functionality above. Save this as docker-compose.yaml
:
version: '3.9'
services:
my-elast:
image: docker.elastic.co/elasticsearch/elasticsearch:7.16.3
ports:
- 9200:9200
- 9300:9300
volumes:
- elast_data:/usr/share/elasticsearch/data
environment:
- "discovery.type=single-node"
volumes:
elast_data:
Now bring up the containers:
$ docker-compose up -d
Creating network "elast_default" with the default driver
Creating volume "elast_elast_data" with default driver
Creating elast_my-elast_1 ... done
The commands:
up
brings up the container-d
in a detached mode
Verify the container processes using the ps command:
$ docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------------------------------------------------
elast_my-elast_1 /bin/tini -- /usr/local/bi ... Up 0.0.0.0:9200->9200/tcp,:::9200->9200/tcp,
0.0.0.0:9300->9300/tcp,:::9300->9300/tcp
To login to the container and login to redis, use this:
$ docker-compose exec my-elast /bin/sh
sh-5.0# curl -X GET "localhost:9200/"
{
"name" : "876b169c35d6",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "UmBfLoVARnKvLc79Nk5Yhg",
"version" : {
"number" : "7.16.3",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "4e6e4eab2297e949ec994e688dad46290d018022",
"build_date" : "2022-01-06T23:43:02.825887787Z",
"build_snapshot" : false,
"lucene_version" : "8.10.1",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
sh-5.0#
3. Using Systemd to run Elasticsearch docker
We can manage our Elasticsearch docker container through systemd. Let us create a systemd
resource file for handling the elasticsearch container we just created above.
Create a elasticsearch docker file:
sudo vim /etc/systemd/system/elasticsearch-docker.service
Add this content
[Unit]
Description=Custom Elasticsearch Docker Container
After=network.target
[Service]
Type=simple
TimeoutStartSec=5m
ExecStartPre=-/usr/bin/docker rm -f myelast
ExecStart=/usr/bin/docker run \
--name myelast \
-p 9200:9200 -p 9300:9300 \
-e "discovery.type=single-node" \
-v elast_data:/usr/share/elasticsearch/data \
docker.elastic.co/elasticsearch/elasticsearch:7.16.3
ExecReload=-/usr/bin/docker stop myelast
ExecReload=-/usr/bin/docker rm myelast
ExecStop=-/usr/bin/docker stop myelast
Restart=always
RestartSec=30
[Install]
Then we can reload the systemd
catalog and start the service:
sudo systemctl daemon-reload
sudo systemctl start <meta charset="utf-8">elasticsearch-docker
Confirm the service status
$ sudo systemctl status elasticsearch-docker ● elasticsearch-docker.service - Custom Elasticsearch Docker Container Loaded: loaded (/etc/systemd/system/elasticsearch-docker.service; static; vendor preset: disabled) Active: active (running) since Tue 2022-01-25 16:05:54 UTC; 24s ago Process: 43524 ExecStartPre=/usr/bin/docker rm -f myelast (code=exited, status=0/SUCCESS) Main PID: 43531 (docker) Tasks: 7 (limit: 23176) Memory: 18.8M CGroup: /system.slice/elasticsearch-docker.service └─43531 /usr/bin/docker run --name myelast -p 9200:9200 -p 9300:9300 -e discovery.type=single-node -v elast_data:/usr/share/elasticsearch/data docker.elastic.co/elasticse> Jan 25 16:06:17 dev-rockysrv.inv.re docker[43531]: {"type": "server", "timestamp": "2022-01-25T16:06:17,311Z", "level": "INFO", "component": "o.e.i.g.GeoIpDownloader", "cluster.n
We just set up a custom systemd service based on a container managed through docker!
Conclusion
In this guide we managed to run Elasticsearch as a container in our system, we explored using the docker run
command while passing the required arguments an alternative approach of simplifying the process with docker-compose