FresherTechnicalElasticsearch
How do you run Elasticsearch locally with Docker and send a first request?
Try answering first
Model answer
Run a single-node container with the official image, then call it over HTTPS.
docker run -d --name es01 -p 127.0.0.1:9200:9200 \
-e "discovery.type=single-node" \
-e "ES_JAVA_OPTS=-Xms1g -Xmx1g" -m 2GB \
docker.elastic.co/elasticsearch/elasticsearch:9.0.0
docker cp es01:/usr/share/elasticsearch/config/certs/http_ca.crt .
curl --cacert http_ca.crt -u elastic:$ELASTIC_PASSWORD https://localhost:9200
Points to know:
- On first start, Elasticsearch 8 and later enable security: TLS for HTTP and a generated password for the
elasticuser, printed in the container logs. Reset it withdocker exec -it es01 bin/elasticsearch-reset-password -u elastic. discovery.type=single-nodeskips the cluster bootstrap checks that a one-node development setup cannot satisfy.- Set the JVM heap to half of the container memory, and on a Linux host raise
vm.max_map_countto 262144. - Without a volume (
-v esdata:/usr/share/elasticsearch/data), data disappears when the container is removed. - A successful call returns JSON with the cluster name, the version and the tagline
You Know, for Search.
Bind to localhost, keep security on, and mount a volume if the data matters.
Key points
- Single-node discovery for local development
- Security and TLS are on by default in 8 and later
- Heap at half of container memory, vm.max_map_count raised
- A volume keeps data after the container is removed
Likely follow-ups
- How do you add Kibana to this setup?
- Why is a one-node cluster yellow for indices with replicas?
Common mistakes
- Disabling security and exposing port 9200 on all interfaces
- Forgetting the memory limits and getting OOM-killed