Phpstorm Wsl2 Docker



Estimated reading time: 7 minutes

  1. Jetbrains Wsl2
  2. Intellij Docker Wsl 2

Docker enables developers to deploy applications inside containers for testing code in an environment identical to production. PhpStorm provides Docker support using the Docker plugin. The plugin is bundled and enabled by default. The Docker plugin is bundled with PhpStorm and activated. PHPStorm has a pretty good docker integration if we are talking about docker run or docker-compose run. That means, with the PHPStorm docker integration, you can use a docker image to run your tests, however you cannot connect to an existing, running docker container and use that to run your scripts. I’m running a Magento2 PHP app inside a set of docker containers that are orchestrated by docker-compose locally. I’m running Docker Desktop for Windows in WSL2 mode.

This means, that the build time was 2–3 times faster on WSL2 than under Windows native. Another test was to compile k8s-jcasc-management-go with a GoLang compiler on both systems. I'm running Docker Desktop for Windows in WSL2 mode. My php 7.3 interpreter with Xdebug sits inside one of those containers. Once I'm in FPM context - I can debug things fine if I just have activated remote debugging in PhpStorm. Now I need to debug a CLI script and PhpStorm shows me following error.

Windows Subsystem for Linux (WSL) 2 introduces a significant architectural change as it is a full Linux kernel built by Microsoft, allowing Linux containers to run natively without emulation. With Docker Desktop running on WSL 2, users can leverage Linux workspaces and avoid having to maintain both Linux and Windows build scripts. In addition, WSL 2 provides improvements to file system sharing, boot time, and allows access to some cool new features for Docker Desktop users.

Docker Desktop uses the dynamic memory allocation feature in WSL 2 to greatly improve the resource consumption. This means, Docker Desktop only uses the required amount of CPU and memory resources it needs, while enabling CPU and memory-intensive tasks such as building a container to run much faster.

Additionally, with WSL 2, the time required to start a Docker daemon after a cold start is significantly faster. It takes less than 10 seconds to start the Docker daemon when compared to almost a minute in the previous version of Docker Desktop.

Prerequisites

Before you install the Docker Desktop WSL 2 backend, you must complete the following steps:

  1. Install Windows 10, version 1903 or higher.
  2. Enable WSL 2 feature on Windows. For detailed instructions, refer to the Microsoft documentation.
  3. Download and install the Linux kernel update package.
Phpstorm Wsl2 Docker

Best practices

  • To get the best out of the file system performance when bind-mounting files, we recommend storing source code and other data that is bind-mounted into Linux containers (i.e., with docker run -v <host-path>:<container-path>) in the Linux file system, rather than the Windows file system. You can also refer to the recommendation from Microsoft.

    • Linux containers only receive file change events (“inotify events”) if the original files are stored in the Linux filesystem. For example, some web development workflows rely on inotify events for automatic reloading when files have changed.
    • Performance is much higher when files are bind-mounted from the Linux filesystem, rather than remoted from the Windows host. Therefore avoid docker run -v /mnt/c/users:/users (where /mnt/c is mounted from Windows).
    • Instead, from a Linux shell use a command like docker run -v ~/my-project:/sources <my-image> where ~ is expanded by the Linux shell to $HOME.
  • If you have concerns about the size of the docker-desktop-data VHDX, or need to change it, take a look at the WSL tooling built into Windows.
  • If you have concerns about CPU or memory usage, you can configure limits on the memory, CPU, Swap size allocated to the WSL 2 utility VM.
  • To avoid any potential conflicts with using WSL 2 on Docker Desktop, you must uninstall any previous versions of Docker Engine and CLI installed directly through Linux distributions before installing Docker Desktop.

Download

Download Docker Desktop Stable 2.3.0.2 or a later release.

Install

Ensure you have completed the steps described in the Prerequisites section before installing the Docker Desktop Stable 2.3.0.2 release.

  1. Follow the usual installation instructions to install Docker Desktop. If you are running a supported system, Docker Desktop prompts you to enable WSL 2 during installation. Read the information displayed on the screen and enable WSL 2 to continue.
  2. Start Docker Desktop from the Windows Start menu.
  3. From the Docker menu, select Settings > General.

  4. Select the Use WSL 2 based engine check box.

    If you have installed Docker Desktop on a system that supports WSL 2, this option will be enabled by default.

  5. Click Apply & Restart.
  6. Ensure the distribution runs in WSL 2 mode. WSL can run distributions in both v1 or v2 mode.

    To check the WSL mode, run:

    wsl.exe -l -v

    To upgrade your existing Linux distro to v2, run:

    wsl.exe --set-version (distro name) 2

    To set v2 as the default version for future installations, run:

    wsl.exe --set-default-version 2

  7. When Docker Desktop restarts, go to Settings > Resources > WSL Integration.

    The Docker-WSL integration will be enabled on your default WSL distribution. To change your default WSL distro, run wsl --set-default <distro name>.

    For example, to set Ubuntu as your default WSL distro, run wsl --set-default ubuntu.

    Optionally, select any additional distributions you would like to enable the Docker-WSL integration on.

    Note

    The Docker-WSL integration components running in your distro depend on glibc. This can cause issues when running musl-based distros such as Alpine Linux. Alpine users can use the alpine-pkg-glibc package to deploy glibc alongside musl to run the integration.

  8. Click Apply & Restart.

Develop with Docker and WSL 2

The following section describes how to start developing your applications using Docker and WSL 2. We recommend that you have your code in your default Linux distribution for the best development experience using Docker and WSL 2. After you have enabled WSL 2 on Docker Desktop, you can start working with your code inside the Linux distro and ideally with your IDE still in Windows. This workflow can be pretty straightforward if you are using VSCode.

  1. Open VSCode and install the Remote - WSL extension. This extension allows you to work with a remote server in the Linux distro and your IDE client still on Windows.
  2. Now, you can start working in VSCode remotely. To do this, open your terminal and type:

    wsl

    code .

    This opens a new VSCode connected remotely to your default Linux distro which you can check in the bottom corner of the screen.

    Alternatively, you can type the name of your default Linux distro in your Start menu, open it, and then run code .

  3. When you are in VSCode, you can use the terminal in VSCode to pull your code and start working natively from your Windows machine.

GPU support

Starting with Docker Desktop 3.1.0, Docker Desktop supports WSL 2 GPU Paravirtualization (GPU-PV) on NVIDIA GPUs. To enable WSL 2 GPU Paravirtualization, you need:

  • A machine with an NVIDIA GPU
  • The latest Windows Insider version from the Dev Preview ring
  • Beta drivers from NVIDIA supporting WSL 2 GPU Paravirtualization
  • Update WSL 2 Linux kernel to the latest version using wsl --update from an elevated command prompt
  • Make sure the WSL 2 backend is enabled in Docker Desktop

To validate that everything works as expected, run the following command to run a short benchmark on your GPU:

Feedback

Your feedback is very important to us. Please let us know your feedback by creating an issue in the Docker Desktop for Windows GitHub repository and adding the WSL 2 label.

WSL, WSL 2 Tech Preview, Windows Subsystem for Linux, WSL 2 backend Docker

Jetbrains Wsl2

I’ve been a long-time user of PhpStorm for my PHP and Javascript (React) development. It’s an extremely powerful tool but let’s face it - it’s a pretty big beast. You can tell it’s running by the fans on the laptop spinning up.

Enter WSL2

I recently rebuilt my PC using the latest Windows Insider build in order to take advantage of Docker on WSL2 - this is a massive step-change from WSL1 in terms of performance and reliability. Unfortunately, PhpStorm doesn’t support it, and doesn’t look like it will any time soon. So, I decided to step out on a limb and give Visual Studio Code a shot.

Visual Studio Code - First Impressions

Number one: it is fast. Unbelievably so! The workspace opens instantly, and there’s no delays searching. There’s some magic pixie dust in there I’m sure.

Docker

Number two: VSCode Remote is a game-changer. Essentially the editor is split into a client/server, and the server runs directly in my WSL2 virtual machine. Broken file watchers are a thing of the past.

Number three: for PHP development, it’s kinda…basic. At least to start with. As I’ve discovered, there are a plethora of extensions that enhance the editor in almost every way imaginable.

Phpstorm wsl2 docker xdebug

Configuration

Because a large amount of the functionality is in the extensions, I’m likely to forget what I’ve installed and how I’ve configured it, so here is a rundown of what I’ve set up.

Intellij Docker Wsl 2

Basics

You need to be running Windows 10 build 18917 (currently Insider Preview) in order to get and install WSL2. You’ll also need Docker build 36883 or higher (currently edge channel) and enable WSL2 in Docker.

Extensions

With VSCode Remote, extensions can be installed locally (ie in your profile or workspace) or remotely. WSL2 acts as a remote environment. So, the first extension we need, and the one you’ll probably be prompted for when VSCode detects the presence of WSL2 is Remote - WSL.

I do have some other extensions installed locally, but they’re mainly for convenience, not development.

Extensions installed in WSL:

  • Code Spell Checker - I’m a terrible speller, and it also helps to keep on top of variable misnaming
  • Composer - PHP’s defacto package manager. Handles all the updates and stuff
  • Docker - Docker integration
  • Docker Compose - Docker Compose integration
  • PHP Debug - PHP Debug tools
  • PHP DocBlocker - Helpers for writing php docblocks
  • PHP Intelephense - Intelisense for your PHP. This one is critical - I paid the ~$15 for the pro version
  • PHP-CS-Fixer - Fix any issues phpcs picks up
  • phpcs - Code sniffer for bad php code
Phpstorm Wsl2 Docker

A couple of points:

  • To get the most out of Intelephense you need to disable the built-in PHP Language Features (follow the quickstart guide)
  • Make sure you set the phpcs.standard to what your project adheres to, otherwise all the warnings will make you really sad

Wrap-up

That’s all for now. I’m new to VSCode so if you’ve got any great tips let me know via the comments. As I iterate on this environment I’ll try and keep this page updated.