Working with PHP projects usually requires to have a local development environment. In this article we will show you how a typical docker-based environment for Magento 2.
Some containers will be based on the official Magento Cloud Docker images.
Step 1: Prepare folders structure for the project
├── html
└── mgdb
Step 2: Create the custom docker network
The easiest way to do this is to define all services in docker-compose.yml file under the docker directory.
│ ├── apache-config.conf
│ ├── auth.json
│ ├── Dockerfile
│ ├── msmtprc
│ ├── php-dev.ini
│ └── www
├── elasticsearch
│ ├── docker-entrypoint.sh
│ └── Dockerfile
├── mariadb
│ └── Dockerfile
├── rabbitmq
│ ├── definitions.json
│ └── rabbitmq.conf
├── redis
│ ├── Dockerfile
│ └── redis.conf
├── router
│ ├── ssl
│ └── templates
└── varnish
│ ├── default.vcl
│ └── Dockerfile
├── .env
└── docker-compose.yml
We can build custom containers from Dockerfiles or pull already pre-built images from Docker Hub, everything depends on your preferences. If we need a custom container like an application, it's better to take a base image and install required libraries there (in our case it is php:7.4-apache)
Step 3: Prepare the containers
We based our application container on php:7.4-apache image and installed additional dependencies as
in php/7.4-fpm/Dockerfile from Magento Cloud Docker repository because that Dockerfile already contains all dependencies needed from the requirement stack.
We used php-apache image instead php-fpm to simplify virtual hosts configuration and to have everything we need in one container for development purposes.
Additionally we installed git, msmtp, NodeJS, PHP-SPX profiler and yarn;
The app root folder is mounted under /var/www/html and the composer cache folder is also mounted to user machine along with PHP, APACHE and SMTP configuration files.
When config wiles are also mounted to the user's machine it simplifies editing of config (just restart the container without rebuilding it).
The Database - contains the DB data
The Elasticsearch - is required to make Magento catalog work (see the removal info)
The Varnish - we recommend to have this service in case you need to test FPC Magento feature with Varnish. Check our VCL config.
The Router - an nginx service used to route requests to the Varnish or the Application containers. It allows us to add more services accessible via 80 or 443 port in the browser by routing them based on server name or other rules (e.g. use different php version, fpm-worker or PWA app)
In our example we are routing all storefront requests to app through Varnish and all requests to custom admin URL directly to the application container
The Redis - a cache service
The RabbitMQ - a queue manager
The MailHog - GUI for mailbox, needed to view emails caught by msmtprc
The complete network: https://github.com/WebMageic/docker-dev/tree/main/docker
Step 4: Run and use
(make sure these domains added to the hosts file)
Comments
Post a Comment