Magento 2 with Docker for Windows and WSL 2

Magento 2 with Docker for Windows and WSL 2

Before COVID-19 cut the possibility of public gatherings, we had plenty of offline hackathons or contribution days in the Magento community world. An essential part of a working toolkit at such events is, obviously, a laptop (there were gurus that wrote the code even on tablets but that’s a different story). And at every table, along with 10 developers with serious faces, you saw at least 7 MacBooks and 3 [something else] with Ubuntu on board. If you are lucky enough, you may meet a dev who uses a Windows-based laptop as a working station but it’s rather a rare exception to the rule.

The growing unpopularity of Windows-based laptops among web-developers in the past has its roots. There was a period when Windows could not provide a toolkit powerful enough for productive web application development. Many developers, who made the switch from Windows to OSX or Linux, never looked back. But during the last few years, the situation with the Windows platform has changed dramatically. Microsoft took the direction of improving the development tools they own (VS Code, WSL) and acquiring new ones (GitHub). We would like to share our recent experience in building Magento 2 local development environment using Docker and WSL 2.

WSL (Windows Subsystem for Linux) is a system that allows running a Linux environment in the Windows system without using virtual machines. With WSL, you may install, for example, Ubuntu on your workstation and use all features of this operating system with almost no overhead. WSL was available on Windows 10 in 2016 with the follow-up second version of the system, WSL 2 released in June 2019 (build 18362 or higher). The main difference between the two versions is a significantly improved file system performance (and full system call support that adds more possibilities for interaction between Linux and the hardware directly).

Now imagine that you have the main OS, where you run a browser, keep docs, occasionally play games, and another OS for development that may be running simultaneously. You can keep all the development stuff there without overwhelming the main OS with NPM packages etc. Sound greats, doesn’t it? Moreover, you can run Docker on your Linux system, use volumes mounting and have access to the Linux file system directly from Windows. Now let’s see how it works.

After installing the WSL you may choose your favorite Linux distribution.

WSL Distributives

Installing the distribution usually takes up to 1 minute. After the installation, you may simply go to the terminal and enter the distribution name to boot the Linux system. It might take some time during the very first run, but every next run will be almost immediate.

Run Ubuntu In Windows Terminal

Now it’s about time to install/upgrade Docker for Windows. Starting from version 2.3.0.2, Docker supports WSL 2 and shows an impressive performance in terms of working with the file system, CPU and RAM. After the installation, you may want to make sure that Windows and Linux use the Docker system transparently. Go to the Windows terminal and run something like:

docker run mongo

This command will download the latest MongoDB image and run the corresponding container. Once the container is up and running, you may check the details about it in both systems Windows and Linux.

docker ps
Docker containers list on Windows

You may have noticed, we have the same list for both operating systems. We did not install Docker on Ubuntu manually, Docker with WSL2 support did the trick automatically.

Once you have booted the Linux distribution, you will have access to its’ file system via the network drive.

Access to WSL file system from Windows

This means, if you use volumes mounting in Docker, you may easily access the shared Docker files. For example, we use the following simple docker-compose setup for running a Docker container for Magento 2.4.

version: "3"

services:
  app:
    image: magebase
    ports:
      - "8080:80"
      - "4443:443"
      - "2222:22"
    command: ['/root/entrypoint.sh']
    tty: true
    stdin_open: true
    volumes: 
      - ./src:/home/dev/sites/mage24:cached

As you can see from the “volumes” section, the “src” directory of the project is shared with the Docker container. This allows you to access the project files by using WSL volume mounted in Windows. The main benefit of that is the possibility to use IDE/Editor hosted in Windows without additional synchronization tools such as Mutagen.io, docker-sync, and others.

If you use PHPStorm, it will recognize the WSL volume with no additional configuration.

PHPStorm working with WSL drive

The next challenge that may appear is running the CLI tools such as CodeSniffer, PHPUnit and other PHP utiles. Fortunately, we are not forced to do it using .bat files in Windows (phew!). You may configure PHPStorm to run the PHP interpreter from your WSL as well as all other command-line tools.

PHPStorm working with WSL interpreter

The main pain point of running Docker with native volume mounting on Windows/Mac was a horrible performance on read-heavy workloads (hello Magento 2 codebase!). Frankly speaking, that was a big surprise for us when we saw the time to first byte ~300-400 ms for most of the admin pages of a project with 200k SKUs. We don’t have a benchmark chart for different types of pages yet. Still, we can say that for the same setup with the native volumes mounting on Mac we’ve experienced (4500-6500 ms) TTFB. The difference is impressive. With a proper Redis setup for caching (the example system has no Redis so far) things will be even faster.

To summarize, if you have a Windows laptop somewhere on the shelf, maybe it’s the right time to blow the dust from it and give it another shot. If you have an interesting experience in using Windows as your main workstation for web-development, please, share in the comments.

Thank you for reading!

Case Studies

Meet our clients

From startups backed by Mark Cuban and Sir Richard Branson, to some of the biggest eCommerce operations in the world, Atwix helps our clients deliver unparalleled eСommerce experiences. We’re proud to work with the following companies: