When working with Magento, there are several must-have pieces of software for developers. You need a PHP interpreter, a web server, a database server, and some configuration around those things. Moreover, some requirements may vary from one project to another.
Apart from that, when multiple developers work on a single project, they need to have a very similar (or ideally identical) setup, so that introduced changes behave the same for every person who runs the code locally. Not to mention projects where new developers are introduced on a daily basis and preparing the environment may be time consuming and tricky.
That’s where Docker comes in handy, as it can provide a ready to use environment with predefined images and configuration.
Docker comes with a tool called Docker Compose to make it easier to manage environments with multiple containers responsible for different tasks. Containers are made with an intention of a single responsibility. For instance, we can have following separate containers for Magento:
- Web server
- Database server
Thanks to Docker Compose, we can easily define services in the docker-compose.yml file and the tool will do it’s thing automatically. We can also provide predefined stuff like networks, volumes and more.
A word on platform differences
Docker runs natively on GNU/Linux, so if you’re using any desktop Linux distro, you can skip this section.
On other OS-es like Windows or MacOS, virtualization is used to provide GNU/Linux Docker containers support. In other words, containers run on top of a tiny Linux virtual machine.
The main issue with those platforms is the I/O access – there’s no way to mount your local folder inside a virtual machine without using an additional protocol, so it’s way slower than the native disk access. This is important, because when working on code, you want to keep files on your main system to make the workflow convenient. While Magento is a large system built of many small files, there’s a lot of demanding I/O operations on each request.
Docker Engine implementation for Apple’s desktop OS has some cache optimizations built-in. Adding the :delegated parameter to a volume definition can make a significant improvement to the I/O performance. You can read more about that topic on Docker website.
If that’s not enough, there’s also an unofficial tool called Docker Sync.
Apart from I/O performance issues, official Docker Engine implementation can be problematic because of OS differences. Windows is a non-POSIX compatible system, so we have to deal with caveats like different path format, different line endings in files or lack of UNIX file attributes.
Recommended solution is to use WSL2, which brings a native GNU/Linux experience inside the Windows system with almost no overhead.
Magento Developer Environment by Magently
In Magently we wanted to unify the way developers interact with Magento to bring following advantages:
- Exact same versions of PHP are used for certain projects
- Setting up development stuff with ease
- Environment files can be updated (bringing fixes or improvements) without affecting the project itself
- Some advanced features are available to use
We prepared a fairly flexible solution that is now publicly available on our GitHub.
By default it has the following features:
- Wide range of supported PHP versions (5.6, 7.0, 7.1, 7.2, 7.3, 7.4)
- Pre-built images, as well as customizable source code
- Using NGINX + FastCGI
- MySQL storage using internal Docker volume, as well as user directory
- Support for non-standard HTTP ports
- Built-in Composer
- Support for legacy Magento
- Built-in XDebug extension
- Optional proxying through third-party services
- PHP’s mail function to use any SMTP server
- Built-in NodeJS for assets compilation
- Easy setup using .env file
- Third-party services examples:
Though it offers a lot of features for different use cases, we wanted to keep it simple and easy to use by default.
Here’s how to run Magento project on Docker in few steps:
- Clone the mde repository somewhere on your disk. A single clone of the repository should correspond to a single project that you want to run.
git clone firstname.lastname@example.org:magently/mde.git project1-env
- Now change the directory to a newly cloned repository content and copy sample .env file.
cp .env.example .env
- Examine the file and adjust to your needs. It’s well commented, so it should be clear how to use those variables. Refer to GitHub page or README.md file for more information.
- (Optional) Run validate.sh script to check if you have all requirements to run the environment.
- Pull Docker images. If you skip this, Docker will try to build those images itself – it might be useful when there’s a need for customization.
- Finally, fire it up!
If you’re interested in learning about more features of the environment, we strongly advise to read the documentation in README.md.
If you like, feel free to fork the project and experiment with your own customizations!