Developing with Docker on Windows Subsystem for Linux (WSL)

Developing with Docker on Windows Subsystem for Linux (WSL)

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

First steps

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

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

In both paragraphs, follow all the instructions for installation and configuration.

Installing the Ubuntu 18.04 distribution

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

  1. Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux (requires a computer restart)
  2. Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform

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

  • wsl -l -v - see which version is currently installed. If 1, then move down the list
  • wsl --set-version ubuntu 18.04 2 - to upgrade to version 2
  • wsl -s ubuntu 18.04 - install Ubuntu 18.04 as the default distribution

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

Installing Docker Desktop

Follow the instructions during the installation process. The computer will require a restart after installation and on first startup to enable Hyper-V (which is why Windows 10 Pro is required).

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

  • Go to Settings -> Security -> Firewall -> Configure packet rules -> Local Service (TCP) -> Edit
  • Remove port 445 from the list of local ports
  • Save

After launching Docker Desktop, select WSL 2 Tech Preview from its context menu.

Developing with Docker on Windows Subsystem for Linux (WSL)

In the window that opens, click the Start button.

Developing with Docker on Windows Subsystem for Linux (WSL)

Docker and docker-compose are now available inside the WSL distribution.

Important! The updated Docker Desktop now has a tab with WSL inside the settings window. WSL support is enabled there.

Developing with Docker on Windows Subsystem for Linux (WSL)

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

Developing with Docker on Windows Subsystem for Linux (WSL)

Release

The many problems that arose when trying to raise containers of projects located in the Windows user directory came as a surprise.

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

.

From the solution of the previous problem, the following follows: how to work with project files through an 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, be sure to install it in the extension Remote Development extension pack.

After installing the above extension, simply run the command code . in the project directory when VSCode is running.

In this example, nginx is required to access containers through a browser. Install it via sudo apt-get install nginx it turned out not to be so easy. The first step was to update the WSL distribution by running sudo apt update && sudo apt dist-upgrade, and only after that run the nginx installation.

Important! All local domains are written not in the Linux distribution's /etc/hosts file (it's not even there), but in the Windows 32 hosts file (usually located C:WindowsSystem10driversetchosts).

Sources of

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

Source: habr.com

Add a comment