# Prevent swap in docker container

**URL:** <https://discuss.elastic.co/t/prevent-swap-in-docker-container/58801>\
**Category:** Elasticsearch\
**Created:** [August 24, 2016, 10:19am UTC](https://discuss.elastic.co/t/prevent-swap-in-docker-container/58801 "2016-08-24T10:19:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dorr](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@dorr](https://discuss.elastic.co/u/dorr)\
**Post date:** [August 24, 2016, 10:19am UTC](https://discuss.elastic.co/t/prevent-swap-in-docker-container/58801/1 "2016-08-24T10:19:30Z")

</div>

Hello,  
How can I prevent ElasticSearch 1.7 from swapping when running inside a docker container?

I am running the container with the `-memory-swap=-1` flag, and still seeing around 500 MB of swapping.  
Is this normal?

Also tried to add `bootstrap.mlockall: true` to the elasticsearch.yml file, but then got the message:  
`Unable to lock JVM memory (ENOMEM). This can result in part of the JVM being swapped out. Increase RLIMIT_MEMLOCK (ulimit).`

Is there any documented way to achieve this?

Thanks.

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [August 25, 2016, 2:05am UTC](https://discuss.elastic.co/t/prevent-swap-in-docker-container/58801/2 "2016-08-25T02:05:14Z")

</div>

[https://www.elastic.co/guide/en/elasticsearch/reference/1.7/setup-configuration.html](https://www.elastic.co/guide/en/elasticsearch/reference/1.7/setup-configuration.html) should help.

---

<div class="post-metadata">

**Author:** ![dorr](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@dorr](https://discuss.elastic.co/u/dorr)\
**Post date:** [August 25, 2016, 8:38am UTC](https://discuss.elastic.co/t/prevent-swap-in-docker-container/58801/3 "2016-08-25T08:38:47Z")

</div>

Thanks.  
There are 3 ways to achieve this, two of them require a system-wide change which I am currently not able to do.  
Is there any way to do this from within a Docker container **without affecting the whole server,** and succeed?

---

<div class="post-metadata">

**Author:** ![dorr](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@dorr](https://discuss.elastic.co/u/dorr)\
**Post date:** [September 8, 2016, 8:09am UTC](https://discuss.elastic.co/t/prevent-swap-in-docker-container/58801/4 "2016-09-08T08:09:11Z")

</div>

Self-Answer:  
I was using the wrong `docker run` flag.  
To prevent swapping, you should use something like `--memory-swappiness=0 --memory 40G --memory-swap 40G`.  
By stating the memory is X and memory+swap is X, you prevent swapping.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 10:21pm UTC](https://discuss.elastic.co/t/prevent-swap-in-docker-container/58801/5 "2017-07-05T22:21:53Z")

</div>


