Docker Development on Windows Subsystem for Linux (WSL)







To fully work with the project on docker in WSL, you need to install WSL 2. At the time of writing, you can use it only as part of the Windows Insider Program (WSL 2 is available in builds 18932 and higher). It is also worth mentioning separately that a version of Windows 10 Pro is required to install and configure Docker Desktop.







Important! WSL 2 has no longer needed Docker Desktop for Windows 10. How to do without it is described in the Docker Pro section in Ubuntu .









The first steps



After entering the preliminary evaluation program and installing updates, you need to install the Linux distribution (in this example, Ubuntu 18.04 is used) and Docker Desktop with WSL 2 Tech Preview:







  1. Docker Desktop WSL 2 Tech Preview
  2. Ubuntu 18.04 from the Windows Store


In both paragraphs, we follow all installation and configuration instructions.







Installing the Ubuntu 18.04 distribution



Before starting Ubuntu 18.04, you must enable Windows WSL and the Windows Virtual Machine Platform by running two commands in PowerShell:







  1. Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux



    (requires restarting the computer)
  2. Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform





After you need to make sure that we will use WSL v2. To do this, in the WSL or PowerShell terminal, execute the following commands sequentially:









Now you can run Ubuntu 18.04, configure (specify a username and password).







Install Docker Desktop



Important! WSL 2 has no longer needed Docker Desktop for Windows 10. How to do without it is described in the Docker Pro section in Ubuntu .







During installation, follow the instructions. The computer will require a restart after installation and at first start to turn on Hyper-V (because of which support, a version of Windows 10 Pro is required).







Important! If Docker Desktop reports blocking by the firewall, go to the antivirus settings and make the following changes to the firewall rules (in this example, Kaspersky Total Security is used as antivirus):









After starting Docker Desktop, in its context menu, select the WSL 2 Tech Preview item.











In the window that opens, click the Start button.











Now docker and docker-compose are available inside the WSL distribution.







Important! The updated Docker Desktop now has a tab with WSL inside the settings window. It includes support for WSL.











Important! In addition to the WSL activation checkbox, you also need to activate your WSL distribution in the Resources-> WSL Integration tab.











Launch



The unexpected were the many problems that occurred when trying to raise the containers of projects located in the Windows user directory.







Various kinds of errors related to running bash scripts (which usually start when building containers to install the necessary libraries and distributions) and other things common for Linux development made us think about placing projects directly in the Ubuntu 18.04 user directory.





.

From the solution to the previous problem, the following follows: how to work with project files through the IDE installed on Windows. As a “best practice”, I found only one option for myself - working through VSCode (although I am a fan of PhpStorm).







After downloading and installing VSCode, we install it in the Remote Development extension pack .







After installing the above extension, just run the code .



command code .



in the project directory with VSCode running.







In this example, nginx is required to access containers through a browser. Installing it through sudo apt-get install nginx



was not so simple. To get started, it was necessary to update the WSL distribution by doing sudo apt update && sudo apt dist-upgrade



, and only then start the nginx installation.







Important! All local domains are registered not in the / etc / hosts file of the Linux distribution (it is not even there), but in the hosts file (usually located C: \ Windows \ System32 \ drivers \ etc \ hosts) in Windows 10.







Docker on Ubuntu



As prompted by knowledgeable users in the comments to the note, docker inside the WSL 2 distribution is absolutely functional. This allows you not to install Docker Desktop on Windows 10 and solves the problem of having a version of Windows 10 Pro (the need for Pro arises precisely in connection with the use of Docker Desktop). It is installed according to the instructions from the home site:









If the docker starts swearing at the daemon when executing the command, check the status of the service - in my case, like nginx, it does not start automatically. Run it with the sudo service docker start



.







If the error “Service failed to build: cgroups: cannot find cgroup mount destination: unknown” falls out at the time of assembly, try the following solution (not mine, I found the link in the sources): sudo mkdir /sys/fs/cgroup/systemd



; sudo mount -t cgroup -o none,name=systemd cgroup /sys/fs/cgroup/systemd



.







Work with the project through PhpStorm



To provide the ability to work with a project in WSL through PhpStorm, you must do the following:









After that, in the IDE, you can open the project along the path C: \ project_directory







PS Dmitry Simagin , thanks for the decision.







Sources



A more detailed description of each step can be found here:










All Articles