Create Elasticsearch services using Docker
Created#More Posted time:Oct 18, 2016 9:19 AM
I have been working on several Elasticsearch-based functions recently. I don’t want to contaminate the system environment, so I spent some time studying how to use Docker to create Elasticsearch services.
First, let’s get to know Docker and Elasticsearch respectively.
What is Docker?
Docker is an open-source tool that can encapsulate a web application in a lightweight, portable and independent container and it can be run in almost any service environment.
Docker containers ensure applications work on any server and deliver consistent performance. A container established by a developer on a laptop can be run in many environments, such as testing environments, production environments, virtual machines, VPS, OpenStack clusters and public computers.
Docker is usually used for the following purposes:
• Automated packaging and application deployment
• Creation of a lightweight and private PaaS environment
• Automated testing and continual integration/deployment
• Deployment of web applications, databases and backend services
So Docker is a system-compatible container. It adopts Linux Container technology to construct a virtual environment in which users can install various applications to provide services, and the environment can be created or destroyed at any moment without generating any impact to the host environment.
What is Elasticsearch?
Elasticsearch is developed in Java and is based on Lucene for implementing all the indexing and searching functions. But it aims to conceal the complexity of Lucene through simple RESTful APIs to facilitate full-text searches.
However, Elasticsearch is not only about Lucene and full-text search, we can describe it as follows:
• Distributed real-time file storage, and every field can be indexed and searchable
• Distributed real-time analysis and search engines
• Extensible to around 100 servers to handle petabytes of structured or non-structured data
In a word, Elasticsearch is an amazing search and storage engine.
It is very easy to install Docker in OSX, just following the instructions on the official website. After the installation is complete, make sure Docker is started, as shown in the figure below:
Create a Docker image
Elasticsearch has provided an official image in Docker Hub. You can, if you have no additional requirements, directly use the officially provided Elasticsearch image by running the command below:
docker run -d -p 9200:9200 --name="es" elasticsearch:2.3.5
But for me, I want to install an additional Elasticsearch plug-in for the convenience of debugging, so I made an image on my own, using this Dockerfile.
RUN /usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head
Access the Dockerfile folder, and run the following command:
docker build --tag=es_ezio:2.3.5 .
Run the command.
You will see the image just created.
Start the container and service
In the previous step, we have made a Docker image, but haven’t created a Docker container. The relationship between the image and the container in Docker can be compared to the program and the process in an operating system, or the class and the instance in an object-oriented language. We must create a container from the image to run our services (that is, Elasticsearch services).
If you create a Docker container for the first time, run the following command:
docker run -d -p 9200:9200 --name="es_ezio" es_ezio:2.3.5
The default port of Elasticsearch is 9200. We can map Port 9200 in the host environment to the Port 9200 in the Docker container so that we can have direct access to the Elasticsearch services in the Docker container by accessing Port 9200 in the host environment. At the same time, we can name the container es_ezio.
If everything goes right, visit http://127.0.0.1:9200/_plugin/head/
By now, we have completed the configuration of providing Elasticsearch services using Docker without contaminating the host environment. Another advantage of doing this is, if you want to start different versions of Elasticsearch or other services, Docker is also an ideal solution.
Docker mainly targets O&M and deployment, but it is also a good playground for developers. In the future, my personal services will be deployed using Docker as much as possible.
Elasticsearch is a powerful search and storage engine that excels in full-text searching which deserves further exploration.