Ken Muse

Containers and the WSL2 File System


If you’ve been using Dev Containers on Windows, then you may have noticed the container build seems a bit slow. Many of the times I see this, the root cause is something simple and easily avoidable. Essentially, they’re working across two file systems.

Many developers that are getting started with Windows Subsystem for Linux begin the same way: using the local file system. This means that after typing wsl, they are navigating the Windows folder structure, often starting in /mnt/c/Users. For example, I might be working in C:\Users\Ken and then moving to /mnt/c/Users/Ken in WSL:

Terminal showing mount

While that has quite a bit of convenience, it requires WSL to constantly utilize a mounted drive to access the data. This is substantially slower than working within the WSL environment itself. If you want to maximize performance, work out of your /home folder. The home folder is similar to the %UserProfile% folder in Windows. You can use the special character ~ or the variable $HOME to access that folder. as well as the path /home/user (where user is the name of the user). For example, I can type:

1cd ~

This places me in /home/kmuse. By working within the Linux file system, my local operations — including Docker builds — will be substantially faster. This is especially noticable with development containers. I’ve seen an environment that took several minutes to build suddenly take under 30 seconds. Quite the improvement!

The natural question is how to access those files from Windows. The recommended approach is to use the WSL share to access those files. This ensures that the system always reads and writes files into the correct location. The share is always \\wsl$ and can appear in Explorer as “Linux”. Underneath that is the specific installed distributions. In my case, I can access those files with \\wsl$\ubuntu\home\kmuse. Similar to your Documents folder, make sure to organize the directories meaningfully. For example, you may want to create a projects folder to group your Code projects.

If you want to learn more, Microsoft provides more details in the WSL documentation, Working across file systems.