Setting JVM options for an ElasticSearch service run in a Docker container

featured image

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 ES_JAVA_OPTS environment variable (…)

https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html#docker-set-heap-size

Run ElasticSearch with Docker Compose

To keep our configuration clean and readable we use the .env file with the following environment variables:

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:

Run the following command in the console to start the service:

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:

browsing logs with docker plugin in inteliJ screenshot

If you don’t, enter the container by using the command shown below:

We can confirm that our settings have been applied by browsing the container logs:

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:

browsing service files with inteliJ screenshot

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

Leave a Reply

Your email address will not be published. Required fields are marked *