for-win icon indicating copy to clipboard operation
for-win copied to clipboard

vm.max_map_count in docker-desktop distro for WSL2

Open thespatt opened this issue 6 years ago • 47 comments

When running an elasticsearch container in the WSL2 Tech Preview, the container continually restarts itself complaining "max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]"

Would it be bad to just modify sysctl.conf in the docker-desktop distro to set vm.max_map_count = 262144? I have confirmed that modifying that is persistent across restarts.

Expected behavior

elasticsearch container should start successfully

Actual behavior

elasticsearch container continually restarts itself

Information

  • Windows Version: Windows 10 Preview Build 19025
  • Docker Desktop Version: 2.1.6.1 edge

Steps to reproduce the behavior

DOCKERFILE: FROM docker.elastic.co/elasticsearch/elasticsearch:6.8.4 RUN /usr/share/elasticsearch/bin/elasticsearch-plugin install analysis-phonetic

Compose: version: '2.4' services: elasticsearch: image: elastic-nwps:6 platform: linux container_name: elasticsearch environment: - cluster.name=docker-cluster - bootstrap.memory_lock=false - "ES_JAVA_OPTS=-Xms1500m -Xmx1500m" - http.cors.enabled=true - http.cors.allow-origin="*" - http.cors.allow-headers="Access-Control-Allow-Origin, X-Requested-With, Content-Type, Content-Length, Authorization" mem_limit: 2g restart: always volumes: - esdata1:/usr/share/elasticsearch/data ports: - 9200:9200 - 9300:9300 networks: - esnet

thespatt avatar Nov 22 '19 17:11 thespatt

@simonferquel

mikeparker avatar Dec 11 '19 21:12 mikeparker

@thespatt is there a workaround to getting it work for now?

gertjvr avatar Dec 12 '19 06:12 gertjvr

Hi, as modifying this sysctl setting would apply to all wsl distros (they all run in the same VM), we won't do that automatically. However, you should be able to set this flag from your own wsl distro (using sudo sysctl -w), and it will apply to Docker as well.

simonferquel avatar Dec 12 '19 07:12 simonferquel

for future me next time I find this issue https://www.elastic.co/guide/en/elasticsearch/reference/current/vm-max-map-count.html

gertjvr avatar Jan 28 '20 06:01 gertjvr

what I have to do on every system restart or docker desktop update: open powershell wsl -d docker-desktop sysctl -w vm.max_map_count=262144

It does seem odd that there's not a way to make this sticky across restarts. @simonferquel is there a way for me to set this permanently at the VM level?

thespatt avatar Feb 13 '20 01:02 thespatt

same here, setting sysctl.conf has no affect, doesn't get loaded, I am close to reverting the bashrc hacks

gertjvr avatar Feb 13 '20 01:02 gertjvr

@thespatt think I found a possible solution could you verify it for me.

  • open powershell
  • wsl -d docker-desktop
  • echo "vm.max_map_count = 262144" > /etc/sysctl.d/99-docker-desktop.conf

Restart docker-desktop

gertjvr avatar Feb 13 '20 01:02 gertjvr

I am not sure about persistent sysctl configs. Maybe @benhillis has the info ?

simonferquel avatar Feb 13 '20 10:02 simonferquel

I wrote "vm.max_map_count=262144" on docker-desktop's /etc/sysctl.d/00-alpine.conf last line. Then I restart , that has no effect. But that line is recorded on that file. @gertjvr Does your setting run properly?

tomofu74 avatar Mar 29 '20 02:03 tomofu74

Been working fine for me, haven't tried using existing files, created my own.

gertjvr avatar Mar 29 '20 03:03 gertjvr

I wrote "vm.max_map_count=262144" on docker-desktop's /etc/sysctl.d/00-alpine.conf last line. Then I restart , that has no effect. But that line is recorded on that file. @gertjvr Does your setting run properly?

same for me. I append the to the end of /etc/sysctl.d/99-sysctl.conf .

johnsonlu avatar Mar 30 '20 08:03 johnsonlu

This fixed the issue for me:

  • wsl -d docker-desktop
  • echo -e "\nvm.max_map_count = 262144\n" >> /etc/sysctl.d/00-alpine.conf

CyberAP avatar Apr 13 '20 00:04 CyberAP

what I have to do on every system restart or docker desktop update: open powershell wsl -d docker-desktop sysctl -w vm.max_map_count=262144

It does seem odd that there's not a way to make this sticky across restarts. @simonferquel is there a way for me to set this permanently at the VM level?

This seems to be the only thing that's working for me so far. I tried adding setting the oo-alpine.conf in docker-desktop tried create a new file as well. Also tried setting these things in my Ubuntu wsl. Nothing seems to work except for the above.

DenisValcke avatar Apr 13 '20 10:04 DenisValcke

Note that most kernel settings (except those which can be set per-namespace) will apply both to docker desktop and your own distro. So 3 things:

  • Please make sure it does not conflict with things you need in your own distros
  • You can actually apply these settings with whatever automation tool in your own distro, and it will apply to Docker Desktop
  • We won't add sysctl config in docker desktop, because it has side effects in every wsl distro.

simonferquel avatar Apr 14 '20 08:04 simonferquel

even echoing the setting into 00-alpine.conf and 99-docker-desktop.conf, both, and restarting Docker didn't work for me, I still get that error...

@thespatt's solution, however, worked... but I'll have do to it manually every time I restart?

joaociocca avatar May 29 '20 02:05 joaociocca

@joaociocca @DenisValcke @johnsonlu finally solved it! Have to set the environment variable "discovery.type=single-node"

I had to solve it because all of the devs on my team will be switching to wsl2 soon and there's no way I can deal with people forgetting to set that.

https://twitter.com/thespatt/status/1260221113768062978 https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html#docker-cli-run-dev-mode

thespatt avatar May 29 '20 15:05 thespatt

I'm aware of that setting, but my goal is to make a "realistic lab", so single-node is a no-no. Thanks, though! Hope it helps others where it helps <3

joaociocca avatar May 29 '20 16:05 joaociocca

This worked for me.

Added this to my compose file:

privileged: true
    user: root
    command:
      - /bin/bash
      - -c
      - |
        sysctl -w vm.max_map_count=262144 &&
        su elasticsearch -c bin/es-docker

simon-hofmann avatar Jun 02 '20 12:06 simon-hofmann

Setting this to 262144 as @CyberAP had it works for me, but the problem is that my Docker stack then ends up consuming huge amounts of memory. Before using Docker with WSL2, I had docker-desktop set to use 8GB. With WSL2 and this setting I'm seeing close to 19GB consumed. That's way more than I can spare on my 32GB system due to other required apps. My system is sluggish and apps are running out of memory.

Can this be set only for one container (in my case)? Elasticsearch is where I saw a problem.

<Edit>It seems the high mem use is due to the way Linux behaves wrt to allocated memory and caching and adding a .wslconfig file allows setting some parameters to constrain that.</Edit>

asampal avatar Jun 02 '20 20:06 asampal

@asampal if you also add settings you can limit elasticsearch memory per node:

  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.8.9
    platform: linux
    container_name: elasticsearch
    environment:
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=false
      - "ES_JAVA_OPTS=-Xms1500m -Xmx1500m"
      - discovery.type=single-node
    mem_limit: 2g

thespatt avatar Jun 02 '20 20:06 thespatt

I think I'll do that @thespatt - thanks.

asampal avatar Jun 02 '20 20:06 asampal

There is a "one-shot" command line to solve it from wsl.exe

❯ wsl --shutdown # because we don't really need to restart the computer to see the config is lost ...
⚡ beccari@RAYGUN  ~                                                                                         [00:28]
❯ wsl -d docker-desktop cat /proc/sys/vm/max_map_count # current value
65530
⚡ beccari@RAYGUN  ~                                                                                         [00:28]
❯ wsl -d docker-desktop sysctl -w vm.max_map_count=262144
vm.max_map_count = 262144
⚡ beccari@RAYGUN  ~                                                                                         [00:28]
❯ wsl -d docker-desktop cat /proc/sys/vm/max_map_count 
262144
⚡ beccari@RAYGUN  ~                                                                                         [00:28]
❯

So, in to make it stick across restarts, I've created a nasty fix-sysctl.bat as follows

@echo off
wsl -d docker-desktop sysctl -w vm.max_map_count=262144 

and put it at shell:startup folder.

Far from ideal (awful), but it looks like it does the trick.

beccari avatar Jun 12 '20 03:06 beccari

@echo off wsl -d docker-desktop cat /proc/sys/vm/max_map_count

cat?

and put it at shell:startup folder.

where and what is this?

denov avatar Jun 17 '20 22:06 denov

@echo off wsl -d docker-desktop cat /proc/sys/vm/max_map_count

cat?

and put it at shell:startup folder.

where and what is this?

In Windows go to Start -> Run (or Win+R) -> and type 'shell:startup' this short path to Win Startup directory -> %APPDATA%\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

there create suggested by @beccari .bat file

zzFluke avatar Jun 18 '20 11:06 zzFluke

The question is why is @gertjvr's solution not working?

PS C:\Users\Artem Russakovskii> wsl -d docker-desktop
Archon-SkylakeD:/tmp/docker-desktop-root/mnt/host/c/Users/Artem Russakovskii# cat /etc/sysctl.d/99-docker-desktop.conf
cat: can't open '/etc/sysctl.d/99-docker-desktop.conf': No such file or directory
Archon-SkylakeD:/tmp/docker-desktop-root/mnt/host/c/Users/Artem Russakovskii# echo "vm.max_map_count = 262144" > /etc/sysctl.d/99-docker-desktop.conf
Archon-SkylakeD:/tmp/docker-desktop-root/mnt/host/c/Users/Artem Russakovskii# cat /etc/sysctl.d/99-docker-desktop.conf
vm.max_map_count = 262144
Archon-SkylakeD:/tmp/docker-desktop-root/mnt/host/c/Users/Artem Russakovskii#
PS C:\Users\Artem Russakovskii> wsl -d docker-desktop cat /proc/sys/vm/max_map_count
65530

archon810 avatar Aug 09 '20 02:08 archon810

This worked for me even after restart. Hope it helps others

wsl -d docker-desktop
echo 262144 >> /proc/sys/vm/max_map_count

It worked for me. Hope it will work for you too.

shortcodes avatar Aug 21 '20 12:08 shortcodes

Not perfect, but good enough;

Add to ~/.bashrc:

minimum_vm_max_map_count=262144
sysctl_vm_max_map_count=$(sysctl vm.max_map_count | cut -d'=' -f2 | sed -e 's/^[[:space:]]*//')
if [ $sysctl_vm_max_map_count -ge $minimum_vm_max_map_count ]; then
     echo "sysctl vm.max_map_count greater or equal to $minimum_vm_max_map_count ($sysctl_vm_max_map_count)"
else
     echo "sysctl vm.max_map_count lower as $minimum_vm_max_map_count ($sysctl_vm_max_map_count). Setting it to $minimum_vm_max_map_count..."
     sudo sysctl -w vm.max_map_count=$minimum_vm_max_map_count
fi

PieterScheffers avatar Aug 26 '20 11:08 PieterScheffers

Are there any news for this topic?

rabauss avatar Dec 01 '20 10:12 rabauss

No. I understand it is difficult for WSL2. Thank you.

tomofu74 avatar Dec 01 '20 15:12 tomofu74

To all the people saying their fix persists through a "restart", I think you're only testing a Docker for Desktop restart. Everybody else is confused because they're looking for a solution which survives a Windows reboot.

These solutions are equivalent, they allow an elasticsearch container to run, but the setting is not persisted after Windows reboots.

wsl -d docker-desktop sysctl -w vm.max_map_count=262144

Or

docker run --rm -ti --privileged centos sysctl vm.max_map_count=262144

Or

wsl -d docker-desktop
# echo "vm.max_map_count = 262144" > /etc/sysctl.d/99-docker-desktop.conf
#    OR
# echo 262144 >> /proc/sys/vm/max_map_count

Besides running an elasticsearch container you can test by checking this:

docker run --privileged centos sysctl -a | grep max_map_count
vm.max_map_count = 262144

Unfortunately the Elastic docs https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html#_set_vm_max_map_count_to_at_least_262144 don't mention this reboot problem.

As @simonferquel said earlier https://github.com/docker/for-win/issues/5202#issuecomment-564889075:

Hi, as modifying this sysctl setting would apply to all wsl distros (they all run in the same VM), we won't do that automatically. However, you should be able to set this flag from your own wsl distro (using sudo sysctl -w), and it will apply to Docker as well.

Simon do you know where the central wsl VM is? Is there a way to make the sysctl permanent, surviving Windows reboots? I wouldn't mind that it affects all wsl distros I run on my PC.

jamshid avatar Feb 13 '21 01:02 jamshid