I’ve been testing out development on Windows using Docker and how best to replicate the sort of linux environment I’m most used to. One of the reasons docker is so loved on linux for development is that it makes it convenient to run lots of different machines locally without a huge hardware investment.
Having paid attention to the various things coming out of the Microsoft camp it looked like WSL2 was likely to be the best option, and so I gave it a go and it all seemed reasonable enough. Just recently I realised I wasn’t actually doing it right though. Looking at the Best practices section here – https://docs.docker.com/docker-for-windows/wsl/ I realised I should be running docker from within a Linux instance rather than Windows itself. By storing files within another linux instance it should resolve the file permission (executable permissions) and file watching issues you generally have when using docker on windows.
Having played about with it some more and rebuilt my setup to ensure I understand what I did I’m going to quickly document the basic steps. A lot of this is just cribbed from the various things I’ve read.
Cribbed from that page, assume commands are done in an admin powershell. All of this assumes that Virtualisation is enabled in Windows.
- Ensure running a new enough version of Windows (Version 1903 or higher, with Build 18362 or higher)
- Enable WSL
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
- Enable virtual machines
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
- Download kernel update package and install – https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi
- Set WSL2 to be the default version
wsl --set-default-version 2
- Limit memory usage by WSL by creating a config (tweak usage allowed based on machine capacity, key thing is to ensure Docker doesn’t make rest of the machine unusable):
memory=4GB # Limits VM memory in WSL 2 up to 4GB
- Install Ubuntu from the Windows Store and launch it. This should prompt you to setup a new username/password within the new linux.
Install Windows Terminal
Note windows terminal binds Ctrl-V and Ctrl-C to copy/paste as well as Ctrl-Shift-V/C so depending on the user, you may want to go into the settings and copy those bits out. They can get in the way of things you do in a terminal.
Ensure docker is not installed (uninstall if necessary). When prompted on install agree to use WSL2.
Initial run and bookmarks
Start Windows Terminal and open an Ubuntu shell (when you go to the start menu, find terminal and it’s an option on the right). Also pin Terminal to the taskbar.
cd’ to go to the home folder (note that by default the terminal opens in the
/mnt/c/Users/User folder, you want to be in
explorer.exe . to open up an explorer window like
Pin the folder to the quick access list (right click on the users home folder).
Using this setup
Now you can spin up an Ubuntu terminal session and use git. Remember to clone files into the linux file system (within $HOME) rather than the parent machines file system.
You may want to install git outside on Windows as well as on the inside (where it generally seems to be installed by default). You can edit on the outside by going to the
\\wsl$ folders and you can use various tooling on the inside.
What are the downsides?
Compared to regular linux on it’s own, the networking is still in a VM, so you don’t have direct access to the containers in the way you generally would on linux. This is even true within the linux machine you use the terminal on. It appears docker actually runs the containers in a different VM and simply proxies the docker commands over to it.