Docker on Windows 10 with WSL2

3 Jun 2021

Hello! Today I would like to share my experience of using Docker on Windows.

Recently I tried to run one of my projects on Windows 10. I use Linux as my development environment and run projects in Docker. Docker works great on Linux.

Previously I also tried to use Docker on Windows/Mac, but all the time I had different issues. Finally, I just installed Linux into Virtualbox and used Docker there.

Everything becomes better when WSL was introduced in Windows 10. WSL creates a Linux environment in Windows 10. It's not just a virtual machine, Linux is somehow integrated into the host environment. WSL2 is compatible with Docker for Windows.

Windows Updates

I would recommend installing all the latest Windows updates before. I couldn't configure Docker + WSL without it. At least, it's important to have 2nd version of WSL installed.

WSL and Docker install

I enable WSL and install Docker using this tutorial. General steps are:

1 . Run powershell

2 . Enable WSL

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

3 . Enable Virtual Machine

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

4 . Restart computer

5 . Install WSL update

6 . Set WSL2 as a default

wsl --set-default-version 2

7 . Install Ubuntu 20.04 from Microsoft Store. Set username (for example: antonshell) and password.

8 . Install Docker for Windows, enable WSL2 support

9 . Run sample Docker image

docker run -it --rm -p 8000:80 --name my-sample mcr.microsoft.com/dotnet/samples:aspnetapp

10 . Ensure everything works: http://localhost:8000/

Run WSL

Linux distro can be opened like a desktop app, login happens automatically. Then the user comes to the home directory and can use standard Linux utilities like htop, mc etc.

Linux дистрибутив открывется с ярлыка, пользователь автоматически залогинен, находится в домашней директории. Можно использовать стандартные linux утилиты, например htop, mc и т.д.

Run sample PHP project

I used placeholder-service as a sample PHP project. This is a self-hosted service for generating placeholder images.

1 . Go to Ubuntu console and create Projects directory

mkdir -p /home/antonshell/Projects
cd /home/antonshell/Projects

2 . Then install project locally

git clone https://github.com/antonshell/placeholder-service.git
cd placeholder-service
docker-compose up -d
docker-compose exec php-fpm composer install

3 . Ensure it works: http://127.0.0.1:16880, http://127.0.0.1:16880/img

Docker performance, file systems

It's very important to locate the project in the Linux file system. Otherwise, web-app works much slower.

For example, if a project is located in /home/antonshell/Projects/placeholder-service, the loading page takes 80ms. If it is located in C:\Projects\placeholder-service, the is about 1180ms.

Windows filesystem is automatically mounted in Ubuntu into /mnt/c/. Ubuntu file system also available from Windows. Need to open explorer, type \\wsl$ in the address line, and select folder. It also would be helpful to create a bookmark or network drive.

XDebug

I tried to configure XDebug for WSL, but unfortunately, it doesn't work. However, it works great on Linux. I used this tutorial: https://blog.denisbondar.com/post/phpstorm_docker_xdebug.

Summary

Docker works under Windows 10/WSL2 quite well. Of course, I don't have a plan to move back from Linux. But, in general, it would be easier to start developing on Windows with Docker.

Demo project available on github. That's all for today. Thank you for your attention!

Tags: Windows, Docker, WSL