How to deploy Magento 2 on Docker Swarm

DaiPham
3 min readDec 17, 2021

--

Magento 2 and Docker

Magento 2 is known as one of the most powerful open-source solutions for e-commerce businesses.

Your local development machine is where you develop and deploy your code to test it against a running Magento application. Its configuration should be as close to a production server as possible. So how to make it closest to the production environment? The answer is Docker.

Magento 2 System requirements

As of the time of writing the article, Magento 2 last stable version is 2.4.3 so these requirements can be different from the time you read or your custom system.

System requirements:

  • Composer (Composer 2 was supported in Magento 2.4.3)
  • PHP 7.4
  • MySql 8.0 or MariaDB 10.4
  • Nginx 1.8 or Apache 2.4
  • Elasticsearch 7.9

Optional services:

  • Varnish
  • Redis
  • RabbitMQ
Magento 2 system requirements (Source: Magento 2 Devdocs)

Docker Swarm for Magento 2

Clone this docker at Magento 2 Docker Swarm

In this article, we will build a minimalist system to deploy Magento 2 on your local machine with Docker Swarm.

If you have base knowledge about Docker, here is the point you looking for: Magento 2 Docker Swarm.

It will look like this:

The build folder contains PHP (Elasticsearch) build file.

The config folder contains all configs of Nginx, PHP, MySql…

The data folder contains MySql data and Elasticsearch indices

At first, we create a docker network “magento2”:

docker network create -d overlay magento2

Next one we build a PHP image for Magento 2.

cd build
docker build -t php74 -f php.Dockerfile .
cd ..

php.Dockerfile

Finally, mount Magento 2 Root folder to PHP and Nginx via docker-compose.yml. In this case, I put my Magento 2 Code in “/var/www/magento2”

docker-compose.yml

Let deploy our stack and go to settings up Magento 2.

docker stack deploy -c docker-compose.yml magento2

To check current status.

docker service ls

For now, you can execute into PHP service to set up Magento 2.

Set up Magento 2.

bin/magento setup:install \
--base-url=http://magento2.local \
--db-host=mysql \
--db-name=magento2 \
--db-user=root \
--db-password=root \
--admin-firstname=admin \
--admin-lastname=admin \
--admin-email=admin@admin.com \
--admin-user=admin \
--admin-password=admin123 \
--language=en_US \
--currency=USD \
--timezone=America/Chicago \
--use-rewrites=1 \
--elasticsearch-host=elasticsearch

Don’t forget to add host config in your /etc/hosts with your local IP (Ex: 192.168.31.67)

And you got this.

Conclusion

In this article, we successfully discussed the set-up process for Magento 2 on Docker Swarm. The Magento 2 on Docker set-up process can prove advantageous for today’s eCommerce platforms for reasons varying from the ease in application development to data security, from malicious attacks or malware software. Moreover, Docker makes Magento developments much more efficient. That is why so many enterprise-level Magento eCommerce stores are built on top of Docker.

I hope I can help some beginner developers like me to find a good way to start.

--

--

DaiPham
DaiPham

No responses yet