Docker

Docker Elasticsearch Integration Tutorial

Welcome readers, in this tutorial, we will discuss how to start (or integrate) ElasticSearch on Docker. Please note as I have limited access to the resources so I will keep this tutorial simple and easy.

1. Introduction to Docker

In the present world, Docker is an important term,

  • Often used in CI/CD platform that packages and runs the application with its dependencies inside a container
  • Is a standard for Linux Containers
  • A Container is a runtime that runs under any Linux kernel and provides a private machine-like space under Linux

1.1 Docker Terminology

  • Image: Representation of Docker container i.e. a JAR or WAR file in Java
  • Container: Runtime of Docker i.e. a deployed and running Docker image. For example, an executable Spring Boot jar
  • Engine: The code that manages, creates and runs the Docker containers
  • Hub: A public developers registry to distribute their code
  • Repository: A collection of Docker related images i.e. different versions of the same application

1.2 Docker Command Basics

Here’s an example command.

Docker Elasticsearch - Basic structure
Fig. 1: Basic structure

1.3 Need for using Docker

  • For environment replication, while the code runs locally on the machine
  • For numerous deployment phases i.e. Dev/Test/QA
  • For version control and distributing the application’s OS within a team

1.4 Setting up Docker

If someone needs to go through the Docker installation, please watch this video.

To start with this tutorial, we are hoping that users at present have the Docker installation completed.

2. Docker Elasticsearch Integration Tutorial

Before going any further let us take a look at the Elasticsearch introduction. Elasticsearch is a distributed and open-source full-text search and analytics engine that is primarily used in the single-page applications (popularly known as SPA’s).

  • It offers a nice replacement for NoSQL databases like – MongoDB and RavenDB
  • Used denormalization to improve the search performance and store up to petabytes of structured and non-structured data
  • Uses JSON objects as responses
  • Its key concepts revolve around the following –
    • Node: Refers to a single instance
    • Cluster: Basically a collection of one or more nodes
    • Index: Collection of different types of documents and their properties. Also supports sharding in order to improve performance
    • Document: Collection of fields
    • Replicas: Offers to create replicas of their indexes and shards

Let us go ahead and explore the steps required to start the Elasticsearch integration with Docker.

2.1 Pull the image

This is the basic step where obtaining the Elasticsearch image for Docker is simple as issuing the docker pull command against the registry.

Pull the Docker image

# To pull the elastic search image from docker hub.
docker pull docker.elastic.co/elasticsearch/elasticsearch:7.7.0

2.2 Starting the single node cluster

For the tutorial purpose, we are starting a single-node Elasticsearch cluster by specifying the single node discovery to bypass certain bootstrapping checks.

Pull the Docker image

# To start the docker container in foreground. 
# If user wants to run the container in the background they can add the '-d' attribute to the run command.
docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.7.0

Make note –

  • We can also start the container via the docker-compose file (which in the present time is considered as an efficient approach)
  • We can make use of the volume attribute (-v) as well to attach a permanent disk to the Elasticsearch container. This helps to ensure that if the container is restarted the data is not lost
  • Developers can use the docker logs <container_name> command to view the container logs

2.2.1 Elasticsearch configuration via docker-compose

To address the points stated above here is the simple Elasticsearch configuration via the docker-compose file.

elasticsearch-7.7.0-compose-volume.yaml

version: '3.3'
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.7.0
    container_name: elasticsearch_development_container
    volumes:
      - /var/db/data/elasticsearch:/Users/docker_volumes/elasticsearch_bkp
    ports:
      - 9200:9200
      - 9300:9300
    environment:
      ES_JAVA_OPTS: '-Xms256m -Xmx256m'
      network.bind_host: 0.0.0.0
      network.host: 0.0.0.0
      discovery.type: single-node

Run the docker-compose file with the following command –

docker-compose -f elasticsearch-7.7.0-compose-volume.yaml up -d

3. Output

Once the docker run command is fired and if everyone thing goes well developers can navigate to the following URL – localhost:9200 and they will get an output as shown in Fig. 2.

Docker Elasticsearch - Elasticsearch JSON info
Fig. 2: Elasticsearch JSON info

Alternatively, they can also hit the cluster health URL (http://127.0.0.1:9200/_cat/health) to confirm if everything is correct or not. Try out these commands in your development environment to practice and learn.

4. Conclusion

In this tutorial, we saw the following:

  • Introduction to Docker and Elasticsearch
  • Commands to run docker in a single-node cluster and via the docker-compose file

That is all for this tutorial and I hope the tutorial will help you to set up Elasticsearch on Docker. Happy learning and do not forget to share!

5. Download the source code

Download
You can download the full source code of this example here: Docker Elasticsearch Integration Tutorial

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button