Easily Configure Magento 2 on Docker in Windows

Posted by

In this article, we will explain how to install Magento 2 on a Docker container using a Docker desktop. We will use Mark Shust’s Docker Configuration for Magento on a Windows system.

Limitations without docker

Long ago, we used to install Magento using Xampp on Windows, and for Unix, we used to install individual applications using shell-like PHP and MySQL. This method had some limitations. One of them was that when there was a version update, it was not possible to install the new updates as it was using a new PHP or MySQL version. To install a new version of Magento, one needed to first uninstall the Xampp application and install a new one.

This was tedious work as you had to uninstall all applications along with it you had to remove the developed app or keep a backup of it somewhere else. After installing a new version of Xampp you then again installed Magento with the new version. I mean installing not updating.

Applications needed

I will be showing the installation of Magento using docker on the Windows system. I will be using the repository of Mark Shust. The repository is a simple few lines of command and has nothing to do much more. He has made it very simple to install. Below are the applications for which we need to do the setup of docker on our system.

Docker desktop

Docker Desktop is an application that enables developers to build, share, and run containerized applications on their local machines. This application is available for all operating systems. Windows, Mac and Unix. You can download the application from their official website https://docs.docker.com/desktop/install/windows-install/. In this application, we will mostly look into 3 main areas that are containers, images, and volumes.

Containers

Docker Desktop allows us to run multiple applications in isolated environments which are known as containers. Each container has its own file system, processes, and network interfaces but shares the host machine’s kernel.

Images

Docker Desktop manages Docker images, which are templates that define what software and dependencies a container will run. You can pull images from Docker Hub or build your own.

Volumes

Docker Desktop lets you manage volumes, which are persistent storage used by containers. Data stored in a volume will persist even after the container stops. Like a file system for PHP containers where it will have the vendor directory core code in it.

WSL – Windows Subsystem for Linux

WSL also known as Windows Subsystem for Linux allows Windows users to run a Linux environment directly on Windows. We don’t need to install a VM or set up a dual boot.

To install WSL you can enable it from “Turn Windows Features On and Off” In there, you need to click two checkboxes “Windows Hypervisor Platform” and “Windows Subsystem for Linux” After clicking OK you will need to restart your system or you can run the command wsl --install for WSL. Next thing we need to choose the Linux Distribution from the Microsoft Store such as Ubuntu, Debian or others.

Install Magento on container

The most important file for configuring the docker container is the compose.yml file. In this file you can mention which PHP version you want, Database, OpenSearch and other things which are needed. These will create a container for each of them. Here in this article, I will be using Mark Shust’s Docker Configuration for Magento in which he have made it very simple to install Magento 2 on a docker.

Now open the WSL from the Windows menu and create a directory under /home/<username>/learningmagento. Next you will need to run the following command.

curl -s https://raw.githubusercontent.com/markshust/docker-magento/master/lib/onelinesetup | bash -s -- magento.test community 2.4.7-p3

Here in the command magento.test is the hostname. You can make changes to it as to what domain name you need for your local website.

Once you run the above command there will be other commands run in the background and a container will be created. You can see that on the docker desktop. In the below image the name is m2 this is because I had created the container with the folder name as m2. In our case, it will be learningmagento.

After the command is finished running the next thing will need to do is add of host to our system. Open the hosts which reside in C:\Windows\System32\drivers\etc\hosts. Add the domain URL, in our case, it was 127.0.0.1 magento.test. You can also add this at the start of the project.

Memory error

If you ran the above command and got an error saying “You should assign min 6GB RAM to the docker”. In order to solve this problem firstly will need to remove all the packages which got downloaded when we ran the above command.

bin/removeall
rm -rf learningmagento

The above command will remove all the packages and also will have to delete the entire directory. Now will have to increase the memory of the docker. To do that we will run a nano command editing the config of the wsl.

nano /etc/wsl.conf

The file will be opened after running the above command. Now change the value under [wsl] memory to something more than the existing one. I have made it to 8GB. After the change save the file. After this start the process from the start which is when the command curl -s https://raw.githubusercontent.com/markshust/docker-magento/master/lib/onelinesetup | bash -s -- magento.test community 2.4.7-p3.

After completion now you can hit the URL magento.test and you will see the Magento 2 Home page loaded.

Magento 2 on Docker

With the above step, we finally install Magento 2 on docker. The next thing which we need is optional those are installing sample data. To add the sample data you will need to run the command bin/magento sampledata:deploy after this you will need to run setup upgrade and other commands like di:comple and static content deploy. You should always run the command starting as bin/magento and not as php bin/magento.

The file system will be inside the src directory of the project again you have to run the command from the root directory (learningmagento) and not from src. To learn about Magento 2 file structure check our article Learn about Magento 2 File Structure Quickly. Lastly, in order to stop the container you can run the command docker compose stop and same way to start docker compose start.