Docker containers provide an easy way to create and package applications in their own environment. If you are interested in starting the Docker container running Elasticsearch, the process is actually quite simple. In this article we will show you how to create an image and start the Elasticsearch Docker container using Dockerfile.

 

The premise condition

Before proceeding with the steps outlined in this tutorial, there is one key prerequisite: you need a stable and supported version of Docker to create Elasticsearch images using Dockerfile. You can use the following command to check the version of your Docker:

$docker --version docker version 20.10.0, build 7287ab3Copy the code

Elasticsearch YAML files

For custom Docker images, we usually need to customize our own ElasticSearch.yml file. Then, you need to COPY the contents of the file into the container using the COPY command of Dockerfile. Let’s start by creating a file directory:

mkdir elasticsearch-docker && cd elasticsearch-docker
Copy the code

With Elasticsearch and Dockerfile, you can make many different configuration changes using YAML. The following YAML command shows just a few things you can modify for Elasticsearch clusters running in Docker:

elasticsearch.yml

# ---------------------------------- Cluster ----------------------------------- # # Use a descriptive name for your cluster: # cluster.name: cluster-liuxg # # ------------------------------------ Node ------------------------------------ # # Use a descriptive name for the node: # node.name: node1 # # Add custom attributes to the node: # #node.attr.rack: r1 # # ----------------------------------- Paths ------------------------------------ # # Path to directory where to store the data (separate multiple locations by comma): # path.data: /var/lib/elasticsearch # # Path to log files: # path.logs: /var/log/elasticsearch # # ----------------------------------- Memory ----------------------------------- # # Lock the memory on startup: # #bootstrap.memory_lock: true # # Make sure that the heap size is set to about half the memory available # on the system and that the owner of the process is allowed to use this # limit. # # Elasticsearch performs poorly when the system is swapping the memory. # # ---------------------------------- Network ----------------------------------- # # Set the bind address to a specific IP (IPv4 or IPv6): # network. Host: 0.0.0.0 # 9200 # # For more information, consult the network module documentation. # # --------------------------------- Discovery ---------------------------------- # # Pass an initial list of hosts to perform discovery when this node is started: # The default list of hosts is ["127.0.0.1", "[::1]"] # # Discovery. Seed_hosts: ["host1", "host2"] # # Bootstrap the cluster using an initial set of master-eligible nodes: # #cluster.initial_master_nodes: ["node-1", "node-2"] # # For more information, consult the discovery and cluster formation module documentation. # # ---------------------------------- Gateway ----------------------------------- # # Block initial recovery after a full cluster restart until N nodes are started: # #gateway.recover_after_nodes: 3 # # For more information, consult the gateway module documentation. # # ---------------------------------- Various ----------------------------------- # # Require explicit names when deleting indices: # #action.destructive_requires_name: true discovery.type: single-nodeCopy the code

I modified cluster.name and Node. name above. More importantly, I changed network. Host to 0.0.0.0 so that Elasticsearch can be accessed externally. You can configure this file according to your actual needs.

 

Elasticsearch Dockerfile

You can build Elasticsearch images in dockerfiles in a number of ways. In the following example, Ubuntu image uses wget to download the DEB package of Elasticsearch service:

Dockerfile

FROM Ubuntu :20.04 # Container Creator LABEL Maintainer ="[email protected]" # Copy the configuration file into the container COPY logging.yml /usr/share/elasticsearch/config/ RUN groupadd -g 1000 elasticsearch && useradd elasticsearch -u 1000 -g 1000 # install APT transport and wget RUN apt-get update && \ apt-get install -y --no-install-recommends \ apt-transport-https \ curl \ wget -y RUN wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.10.1-amd64.deb - no - check - certificate && DPKG -i Elasticsearch - 7.10.1 - amd64. Deb COPY - chown = elasticsearch: elasticsearch elasticsearch. Yml/etc/elasticsearch / # start elasticsearch service USER elasticsearch ENV PATH=$PATH:/usr/share/elasticsearch/bin CMD ["elasticsearch"] # expose the default Elasticsearch port EXPOSE 9200 9300Copy the code

Note: Be sure to run the apt-get update and apt-get install commands on the same line, as shown in the example above. If you don’t, it will be cached and not updated every time.

We use the following command to build the docker image:

docker build -t liuxg/liuxg-elasticsearch $PWD
Copy the code

The -t above gives the image a name. We use. To represent the current directory. After running the above command, we can use the following command to check:

docker images
Copy the code

The command above shows:

We can see a newly added Docker image.

 

The other option is to download the created Elasticsearch image and copy our configuration files directly into the image’s container. The following example shows how to do this:

Dockerfile

FROM ElasticSearch :7.10.1 # Container Creator LABEL Maintainer ="[email protected]" # Copy the configuration file into the container COPY elasticsearch.yml /usr/share/elasticsearch/config # expose the default Elasticsearch port EXPOSE 9200, 9300,Copy the code

Note: Docker only allows you to extract one image FROM each Dockerfile, so be sure not to use multiple FROM commands. If you do try to extract multiple images using FROM, it will just build an image using the last command and ignore any previous commands.

At this point, we have successfully created our Docker image. We can use the following command to check if there is a container in which the image was just created to run:

docker ps -a
Copy the code
docker ps -a | grep liuxg
Copy the code

The command above shows that there are no running containers. This is because only images have been created so far. You need to create containers from the image using the Docker run command.

 

Start an Elasticsearch container

You can run the liuxg/ iuxg-ElasticSearch Docker image you created earlier with the following command:

docker run --rm -it liuxg/liuxg-elasticsearch /bin/bash
Copy the code

You can also expose the default Elasticsearch port 9200 using the -p option. Here’s an example of how to use this option:

docker run --rm -p 9200:9200 liuxg/liuxg-elasticsearch
Copy the code

Once we start the container, we can view it by typing the following command on the host machine’s command line:

curl -XGET http://localhost:9200
Copy the code

The command above shows the result:

$ curl -XGET http://localhost:9200 { "name" : "node1", "cluster_name" : "cluster-liuxg", "cluster_uuid" : "8MWE-_ecrsaliaj5mjd4WG ", "version" : {"number" : "7.10.1", "build_flavor" : "default", "build_type" : "deb", "build_hash" : "1c34507e66d7db1211f66f3513706fdf548736aa", "build_date" : "2020-12-05T01:00:33.671820z ", "build_snapshot" : false, "lucene_version" : "Minimum_index_compatibility_version" : "6.8.0", "minimum_index_compatibility_version" : "6.0.0-beta1"}, "tagline" : "You Know, for Search"}Copy the code

The cluster_name and node names are the same as those set in elasticSearch.yml. We can go to localhost:9200 in the browser to see:

If you want to learn more about Deploying Elastic Stack Docker, please read Elastic: Deploying Elastic Stacks with Docker.