Set up config like memory limits in Docker for Windows and WSL2

Setup configurations for Docker is different for Windows than it is for Mac and Linux

M. Rake Linggar A.
3 min readJan 19, 2022
Photo by Ian Taylor on Unsplash

If you are starting to learn about setting up a system environment using Docker, then you definitely want to make sure it uses the right amount of memory and processing power.

After installing Docker in Windows, you might think that changing these settings would be as easy as changing configurations in VirtualBox (via UI).

Well, not quite.

Unlike Linux and Mac, we cannot directly change the configuration via Docker’s UI.

The latest release of Docker for Windows requires that we install the Windows Subsystem for Linux version 2 (WSL2) that allows you to configure global options that will be used by all WSL2 Linux distributions installed in Windows 10.

And to change the setting for Docker means that we need to create a .wslconfig file. Note that there are two types of config files that we can create. The .wslconfig file would let us create a global configuration. The other one, wsl.conf allows us to configure settings per -distribution for Linux distros in our machine. You can learn more about it here.

  1. Make sure you shut down Docker and any other instances of wsl2. You can do this by going to a command prompt and type the command wsl — shutdown.
  2. To create a .wslconfig file, simply open your File Explorer, and type and enter %UserProfile% to go to your profile directory in Windows.
  3. Docker or WSL2 by default does not create these config files so we should do it ourselves. Create a new file called .wslconfig (make sure there are no .txt at the end)
Image by Author

4. Add the (either all or some of the) following commands in that file to configure docker

# Settings apply across all Linux distros running on WSL 2
[wsl2]
# Limits VM memory to use no more than 4 GB, this can be set as whole numbers using GB or MB
memory=4GB
# Sets the VM to use two virtual processors
processors=2
# Specify a custom Linux kernel to use with your installed distros. The default kernel used can be found at https://github.com/microsoft/WSL2-Linux-Kernel
kernel=C:\\temp\\myCustomKernel
# Sets additional kernel parameters, in this case enabling older Linux base images such as Centos 6
kernelCommandLine = vsyscall=emulate
# Sets amount of swap storage space to 8GB, default is 25% of available RAM
swap=8GB
# Sets swapfile path location, default is %USERPROFILE%\AppData\Local\Temp\swap.vhdx
swapfile=C:\\temp\\wsl-swap.vhdx
# Disable page reporting so WSL retains all allocated memory claimed from Windows and releases none back when free
pageReporting=false
# Turn off default connection to bind WSL 2 localhost to Windows localhost
localhostforwarding=true
# Disables nested virtualization
nestedVirtualization=false
# Turns on output console showing contents of dmesg when opening a WSL 2 distro for debugging
debugConsole=true

You can configure it however you want depending on your system’s need and available resources.

Once those are done, you can start your Docker and continue development.

Hope this is helpful in your journey to learn about Docker. Let me know if you have questions by replying to this post

Sources

  1. https://docs.microsoft.com/en-us/windows/wsl/wsl-config#configure-global-options-with-wslconfig
  2. https://stackoverflow.com/questions/44533319/how-to-assign-more-memory-to-docker-container

--

--

M. Rake Linggar A.
M. Rake Linggar A.

Written by M. Rake Linggar A.

Love anything data-related (especially programming), travel, movies, and gaming.

Responses (1)