Default minimum and maximum heap size used by Elasticsearch is set to 1GB. I want to show you how you can modify this value when running the service with Docker.
What to do if you don’t want to create your own jvm.options file
Any value we set in ES_JAVA_OPTS will override a heap size set by the default jvm.options
file.
While setting the heap size via bind-mounted JVM options is the recommended method, you can also configure this by using the
https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html#docker-set-heap-sizeES_JAVA_OPTS
environment variable (…)
Run ElasticSearch with Docker Compose
To keep our configuration clean and readable we use the .env file with the following environment variables:
1 2 3 4 |
# .env COMPOSE_PROJECT_NAME=elasticsearch-example ELASTIC_STACK_VERSION=7.6.2 JAVA_OPTS=-Xmx256m -Xms256m |
As you can see, I set the minimum and maximum size to be even smaller than the default value. We know that:
Elasticsearch will assign the entire heap specified in jvm.options (…)
https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html#heap-size
Therefore, if we want to run the example service on a local machine just to tinker with settings, we can start with using as little resources as possible. In the production environment we’ll folllow the guideances and set the Xmx
and Xms
to bigger values (but no more than 50% of available physical RAM). Remember that both values should be equal.
A simple container configuration is enclosed in the following docker-compose.yml file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# docker.yml version: "3.3" services: elasticsearch: image: elasticsearch:$ELASTIC_STACK_VERSION volumes: - elasticsearch:/usr/share/elasticsearch/data environment: ES_JAVA_OPTS: $JAVA_OPTS # Change discovery type to enable the production mode and bootstrap checks discovery.type: single-node ports: - "9200:9200" networks: - internal networks: internal: volumes: elasticsearch: |
Run the following command in the console to start the service:
1 |
$ docker-compsoe up -d |
Verify whether the heap size was set according to your configuration
If you use IntelliJ, verifying container setup is effortless thanks to the Docker support:
If you don’t, enter the container by using the command shown below:
1 |
$ docker exec -it elasticsearchexample_1 /bin/bash |
We can confirm that our settings have been applied by browsing the container logs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
{ "type":"server", "timestamp":"2020-05-02T11:52:41,968Z", "level":"INFO", "component":"o.e.e.NodeEnvironment", "cluster.name":"docker-cluster", "node.name":"c17c952f0e3e", "message":"heap size [247.5mb], compressed ordinary object pointers [true]" } … { … "message":"JVM arguments [… -Xmx256m, -Xms256m, …]" } |
Creating your custom jvm.options file
You can see the defaults by checking the content of the jvm.otpions
file inside the container. On the screenshot below you can see how to get to a file inside a Docker container with IntelliJ:
You can use this file to create your custom settings. Once you have your jvm.options
file ready, just mount it in the docker-compose.yml
file under the /usr/share/elasticsearch/config/jvm.options.d
path.
Useful resources
Photo by Ishan @seefromthesky on StockSnap