Introduction
Docker Hub is a cloud-based repository where Docker users and partners can create, test, store and distribute container images. It provides a centralized resource for container image discovery, distribution and change management, user and team collaboration, and workflow automation throughout the development pipeline. In this article, we will delve into the process of deploying Elasticsearch using Docker Hub.
Prerequisites
Before we begin, ensure that you have Docker installed on your system. Docker is a platform that allows developers to automate the deployment, scaling, and management of applications within containers.
Pulling Elasticsearch Image from Docker Hub
The first step in deploying Elasticsearch using Docker Hub is to pull the Elasticsearch image. Docker Hub hosts multiple versions of the Elasticsearch Docker image, which are tagged with their respective version numbers.
To pull the Elasticsearch image, use the following command:
docker pull docker.elastic.co/elasticsearch/elasticsearch:8.8.2
This command pulls the 8.8.2 version of Elasticsearch from Docker Hub. Replace `8.8.2` with the version number of your choice.
Running Elasticsearch Container
After pulling the image, the next step is to run the Elasticsearch container. Use the following command to run the container:
docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:8.8.2
This command runs the Elasticsearch container and maps the ports 9200 and 9300 to the host. The `-e “discovery.type=single-node”` option is used to start a single-node Elasticsearch cluster.
Verifying the Elasticsearch Container
Once the Elasticsearch container is running, you can verify it by sending a GET request to the Elasticsearch service. Use the following command:
curl -k -X GET -u elastic "https://localhost:9200/"
If the Elasticsearch service is running correctly, it will return a JSON response with information about the Elasticsearch cluster. Note that you need to use the password that has been generated for the elastic built-in superuser during the initial run and printed out in the container output.
Configuring Elasticsearch Container
You can configure the Elasticsearch container by setting environment variables when running the container. For example, you can set the `ES_JAVA_OPTS` environment variable to configure the JVM heap size:
docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" docker.elastic.co/elasticsearch/elasticsearch:8.8.2
This command sets the JVM heap size to 512MB.
Persisting Elasticsearch Data
By default, the data stored in the Elasticsearch container is not persistent. If the container is stopped or deleted, all data will be lost. To persist the data, you can use Docker volumes.
Use the following command to create a Docker volume:
docker volume create elasticsearch_data
Then, run the Elasticsearch container with the `-v` option to mount the volume:
docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -v elasticsearch_data:/usr/share/elasticsearch/data docker.elastic.co/elasticsearch/elasticsearch:8.8.2
This command mounts the `elasticsearch_data` volume to the `/usr/share/elasticsearch/data` directory in the container. All data stored in this directory will be persisted in the `elasticsearch_data` volume.
Conclusion
In conclusion, Docker Hub provides a convenient way to deploy Elasticsearch. By pulling the Elasticsearch image from Docker Hub and running it as a Docker container, you can quickly set up an Elasticsearch service for development, testing, or production use.