docker on wsl2 very slow

DockerWindows 10Wsl 2

Docker Problem Overview


After having read about the performance improvements when running Docker on wsl2, I have been waiting for the official release of Windows 10 that supports wsl2. I updated Windows and Docker and switched on the Docker flag to use wsl2 and was hoping for some performance boost for my Oracle Database running in a Docker container but unfortunately the change slowed down the container and my laptop dramatically. The performance of the container is about 10x slower and my laptop is pretty much stuck when starting the container. It seems as if the memory consumption would completely use up my 8GB and heavy memory swapping starts to take place. Is there anything I can do to improve the performance of Docker on wsl2 or at least to better understand what's wrong in my setup?

My environment:

  • Processor Intel(R) Core(TM) i7-2620M CPU @ 2.70GHz, 2 Core(s)
  • Installed Physical Memory (RAM) 8.00 GB
  • Microsoft Windows 10 Pro Version 10.0.19041 Build 19041
  • Docker version 19.03.8, build afacb8b

Docker Solutions


Solution 1 - Docker

This comes from the "vmmem" which consumes as much resource as it can. To solve the problem just go to your user file for me in

> C:\Users\userName

In this directory create a file named ".wslconfig" in which you will configure how many resources can consume WSL2:

[wsl2] 
memory=900MB    #Limits VM memory in WSL 2 to 900MB 
processors=1    #Makes the WSL 2 VM use one virtual processors

Now close your docker and wait for "vmmem" to close in the task manager.

then You can restart docker and normally "vmmem" will not exceed the limit you have set (here 900MB) If don't work restart your computer.

I hope it helped you.

Solution 2 - Docker

You probably have your code stored on the Windows machine in a folder similar to this...

C:\\Users\YourName\projects\blahfu

But you are using Docker on WSL 2 which is a different (Linux) filesystem. So, when you do a Docker build all of the code/context gets copied from the Windows filesystem to Linux filesystem and then from there to the Docker container. This is what takes the most time and is incredibly slow.

Try to put your project into a folder like this...

/home/YouName/projects/blahfu

You should get quite a performance boost.

Solution 3 - Docker

wsl container have they proper filesystem isolated from the windows filesystem. The base idea is to copy your source code from windows file systeme to wsl file systeme.

from window you can acces the wsl container and copy your project to a wslcontainer :

navigate with explorer to \\wsl$

rebuild the container from this location this will do the trick !

Solution 4 - Docker

If the data for the actual docker container is stored on a windows file system (i.e. NTFS) instead of stored on a native linux filesystem (regardless of what the docker container contents are, which are likely already linux based), then I think you are going to see slow performance because you're running WSL and using the docker container from a mounted WINDOWS file system (i.e. /c/mnt/...).

If you copy your docker container to something like /usr/local, or /home//docker on WSL then you may see a 10x performance INCREASE. Try that and see if it works?

Solution 5 - Docker

you need edit "vmmem" resource just add file .wslconfig in path

> C:\Users<yourUserName>.wslconfig

Configure global options with .wslconfig

Available in Windows Build 19041 and later

You can configure global WSL options by placing a .wslconfig file into the root directory of your users folder: C:\Users<yourUserName>.wslconfig. Many of these files are related to WSL 2, please keep in mind you may need to run

> wsl --shutdown

to shut down the WSL 2 VM and then restart your WSL instance for these changes to take affect.

Here is a sample .wslconfig file:

Console

Copy
[wsl2]
kernel=C:\\temp\\myCustomKernel
memory=4GB # Limits VM memory in WSL 2 to 4 GB
processors=2 # Makes the WSL 2 VM use two virtual processors

see this https://docs.microsoft.com/en-us/windows/wsl/wsl-config

Solution 6 - Docker

If you are using VS Code, there is a command named "Remote-Containers: Clone Repository in Container Volume..." which assures you have full speed file access.

Form the documentation:

> Repository Containers use isolated, local Docker volumes instead binding to the local filesystem. In addition to not polluting your file tree, local volumes have the added benefit of improved performance on Windows and macOS.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondoberkoflerView Question on Stackoverflow
Solution 1 - DockerThezozolino LView Answer on Stackoverflow
Solution 2 - DockerAndyView Answer on Stackoverflow
Solution 3 - DockertooyView Answer on Stackoverflow
Solution 4 - Dockeratom88View Answer on Stackoverflow
Solution 5 - DockerHosam ElzaghView Answer on Stackoverflow
Solution 6 - DockerAdrian DymorzView Answer on Stackoverflow