Skip to main content

Magento Docker Setup for module development

This blog post might be outdated!
This blog post was published more than one year ago and might be outdated!
· 3 min read
Stephan Hochdörfer
Head of IT Business Operations

For quite a while I wanted to upgrade the Docker setup for the open-source Force Login module we develop for Magento 2. We were using a very old version of Mark Shust's Docker Configuration for Magento setup. Since there were a lot of changes how things work nowadays, the upgrade was not simple and came with a few side-effects. The latest version of the setup requires Magento to be checkout out locally and to be mounted in the container. If you develop a Magento application that totally makes sense, but for our module, it felt it makes more sense to have the Magento application code in the container as it used to be in the old setup. This led to a bunch of changes that I'll cover below.

At first I had to modify the setup script to download Magento in the container:

bin/root curl -o /var/www/html/magento.tar.gz http://pubfiles.nexcess.net/
magento/ce-packages/magento2-with-samples-${MAGE_VERSION}.tar.gz

bin/rootnotty tar xvfz /var/www/html/magento.tar.gz

bin/rootnotty rm /var/www/html/magento.tar.gz

bin/rootnotty cp /var/www/html/nginx.conf.sample /var/www/html/nginx.conf

bin/rootnotty chown -R app.app /var/www/html

bin/rootnotty chmod +x bin/magento

This downloads the specified Magento version, extract it and sets up the nginx configuration as expected by the Docker setup. The $MAGE_VERSION can be defined by passing the version string to the setup script.

Additionally I wanted to have magerun available in the container because awesome ;)

bin/root curl -o /usr/local/bin/magerun \
https://files.magerun.net/n98-magerun2-latest.phar

bin/rootnotty chmod +x /usr/local/bin/magerun

As a final step, it is needed to install our module via Composer and activate it. This turned out to be a bit tricky. At first I mounted the local files to /var/www/html/app/code/BitExpert/ForceCustomerLogin which worked fine, but I realized that I cannot install the Composer dependencies needed globally the same time. So it was clear that I need to install the module as Composer dependency, ideally symlinked so that I do not have to constantly re-run composer update when changes are made locally. This is what I ended up doing after some trial & error experiments:

bin/clinotty composer config minimum-stability dev

bin/clinotty composer config \
"repositories.bitexpert/magento2-force-customer-login" \
'{"type": "path", "url": "/var/www/html/BitExpert/ForceCustomerLogin/",
"options": {"symlink": true}}'

bin/clinotty composer require bitexpert/magento2-force-customer-login:*

Since our package is available on GitHub an can be downloaded via Magento Marketplace and via Packagist, I had to make sure that the local copy is used instead when the composer require commands gets executed, that's why I add a path repository to the composer.json file of the Magento project inside the Docker container. I had to set the minimum-stability to dev otherwise Composer would prefer to use the stable version from GitHub and Packagist. Since we don't install that many packages as dependencies, I can live with that constraint, although it still feels a bit wrong using dev as minimum-stability. To make sure Magento can load the views properly, I had to make sure to use /var/www/html as a base path to store the module as this is the path Magento is installed.

Last not least, we need to enable the module:

bin/clinotty bin/magento module:enable BitExpert_ForceCustomerLogin

bin/clinotty bin/magento setup:upgrade

bin/clinotty bin/magento setup:di:compile

So far I am quite happy with the setup, maybe this inspires you to use a similar approach when developing a Magento module.