- Customize the Nginx configuration
- Quickly switch PHP versions
- Install extensions & packages
- Run additional services as containers
- Configure multi-store instances
- Setup PHPStorm for a Magento Docker project
- Generate XML URNs for a Magento Docker project
- Install Xdebug browser plugin
- Enable, disable, & check Xdebug
- Configure PHPStorm for Xdebug
- Trigger Xdebug breakpoints with PHPStorm
View Dockerfiles:
- markoshust/magento-nginx (Docker Hub)
- 1.18
- markoshust/magento-php (Docker Hub)
- markoshust/magento-elasticsearch (Docker Hub)
This configuration is intended to be used as a Docker-based development environment for Magento 2.
Folders:
images: Docker images for nginx and phpcompose: sample setups with Docker Compose
The Magento 1 version of this development environment has been deprecated and is no longer supported. PHP 5 was used as it's base, and that version has reached end-of-life. If you still wish to use this setup, please reference compose/magento-1 on tag 20.1.1, but please be aware these images are no longer maintained.
This setup assumes you are running Docker on a computer with at least 4GB of allocated RAM, a dual-core, and an SSD hard drive. Download & Install Docker Desktop.
This configuration has been tested on Mac & Linux. Windows is supported through the use of Docker on WSL.
- For more details on how everything works, see the extended Setup readme.
bin/bash: Drop into the bash prompt of your Docker container. Thephpfpmcontainer should be mainly used to access the filesystem within Docker.bin/cache-clean: Access the cache-clean CLI. Note the watcher is automatically started at startup inbin/start. Ex.bin/cache-clean config full_pagebin/cli: Run any CLI command without going into the bash prompt. Ex.bin/cli lsbin/clinotty: Run any CLI command with no TTY. Ex.bin/clinotty chmod u+x bin/magentobin/composer: Run the composer binary. Ex.bin/composer installbin/copyfromcontainer: Copy folders or files from container to host. Ex.bin/copyfromcontainer vendorbin/copytocontainer: Copy folders or files from host to container. Ex.bin/copytocontainer --allbin/dev-urn-catalog-generate: Generate URN's for PHPStorm and remap paths to local host. Restart PHPStorm after running this command.bin/devconsole: Alias forbin/n98-magerun2 dev:consolebin/download: Download & extract specific Magento version to thesrcdirectory. If the archive download fails, it will attempt to download with Composer. Ex.bin/download 2.4.1.bin/fixowns: This will fix filesystem ownerships within the container.bin/fixperms: This will fix filesystem permissions within the container.bin/grunt: Run the grunt binary. Ex.bin/grunt execbin/magento: Run the Magento CLI. Ex:bin/magento cache:flushbin/mysql: Run the MySQL CLI with database config fromenv/db.env. Ex.bin/mysql -e "EXPLAIN core_config_data"orbin/mysql < backups/magento.sqlbin/mysqldump: Backup the Magento database. Ex.bin/mysqldump > backups/magento.sqlbin/n98-magerun2: Access the n98 magerun CLI. Ex:bin/n98-magerun2 dev:consolebin/node: Run the node binary. Ex.bin/node --versionbin/npm: Run the npm binary. Ex.bin/npm installbin/pwa-studio: (BETA) Start the PWA Studio server. Note that Chrome will throw SSL cert errors and not allow you to view the site, but Firefox will.bin/redis: Run a command from the redis container. Ex.bin/redis redis-cli monitorbin/remove: Remove all containers.bin/removeall: Remove all containers, networks, volumes, and images.bin/removevolumes: Remove all volumes.bin/restart: Stop and then start all containers.bin/root: Run any CLI command as root without going into the bash prompt. Exbin/root apt-get install nanobin/rootnotty: Run any CLI command as root with no TTY. Exbin/rootnotty chown -R app:app /var/www/htmlbin/setup: Run the Magento setup process to install Magento from the source code, with optional domain name. Defaults tomagento2.test. Ex.bin/setup magento2.testbin/setup-grunt: Install and configure Grunt JavaScript task runner to compile .less filesbin/setup-pwa-studio: (BETA) Install PWA Studio (requires NodeJS and Yarn to be installed on the host machine). Pass in your base site domain, otherwise the defaultmaster-7rqtwti-mfwmkrjfqvbjk.us-4.magentosite.cloudwill be used. Ex:bin/setup-pwa-studio magento2.testbin/setup-ssl: Generate an SSL certificate for one or more domains. Ex.bin/setup-ssl magento2.test magento3.testbin/setup-ssl-ca: Generate a certificate authority and copy it to the host.bin/start: Start all containers, good practice to use this instead ofdocker-compose up -d, as it may contain additional helpers.bin/status: Check the container status.bin/stop: Stop all containers.bin/update: Update your project to the most recent version ofdocker-magento.bin/xdebug: Disable or enable Xdebug. Accepts paramsdisable(default) orenable. Ex.bin/xdebug enable
For an improved developer experience, caches are automatically refreshed when related files are updated, courtesy of cache-clean. This means you can keep all of the standard Magento caches enabled, and this script will only clear the specific caches needed, and only when necessary.
To disable this functionality, uncomment the last line in the bin/start file to disable the watcher.
The hostname of each service is the name of the service within the docker-compose.yml file. So for example, MySQL's hostname is db (not localhost) when accessing it from within a Docker container. Elasticsearch's hostname is elasticsearch.
To connect to the MySQL CLI tool of the Docker instance, run:
bin/mysql
You can use the bin/mysql script to import a database, for example a file stored in your local host directory at backups/magento.sql:
bin/mysql < backups/magento.sql
You also can use bin/mysqldump to export the database. The file will appear in your local host directory at backups/magento.sql:
bin/mysqldump > backups/magento.sql
First setup Magento Marketplace authentication (details in the DevDocs).
Copy src/auth.json.sample to src/auth.json. Then, update the username and password values with your Magento public and private keys, respectively. Finally, copy the file to the container by running bin/copytocontainer auth.json.
View emails sent locally through Mailhog by visiting http://{yourdomain}:8025
Redis is now the default cache and session storage engine, and is automatically configured & enabled when running bin/setup on new installs.
Use the following lines to enable Redis on existing installs:
Enable for Cache:
bin/magento config:set --cache-backend=redis --cache-backend-redis-server=redis --cache-backend-redis-db=0
Enable for Full Page Cache:
bin/magento config:set --page-cache=redis --page-cache-redis-server=redis --page-cache-redis-db=1
Enable for Session:
bin/magento config:set --session-save=redis --session-save-redis-host=redis --session-save-redis-log-level=4 --session-save-redis-db=2
You may also monitor Redis by running: bin/redis redis-cli monitor
For more information about Redis usage with Magento, see the DevDocs.
Install and enable the PHP Debug extension from the Visual Studio Marketplace.
Otherwise, this project now automatically sets up Xdebug support with VS Code. If you wish to set this up manually, please see the .vscode/launch.json file.
-
First, install the Chrome Xdebug helper. After installed, right click on the Chrome icon for it and go to Options. Under IDE Key, select PHPStorm from the list and click Save.
-
Next, enable Xdebug in the PHP-FPM container by running:
bin/xdebug enable, the restart the docker containers (CTRL+C thenbin/start). -
Then, open
PHPStorm > Preferences > Languages & Frameworks > PHPand configure:-
CLI Interpreter- Create a new interpreter and specify
From Docker, and name itmarkoshust/magento-php:7-2-fpm. - Choose
Docker, then select themarkoshust/magento-php:7-2-fpmimage name, and set thePHP Executabletophp.
- Create a new interpreter and specify
-
Path mappings- Don't do anything here as the next
Docker containerstep will automatically setup a path mapping from/var/www/htmlto./src.
- Don't do anything here as the next
-
Docker container- Remove any pre-existing volume bindings.
- Ensure a volume binding has been setup for Container path of
/var/www/htmlmapped to the Host path of./src.
-
-
Open
PHPStorm > Preferences > Languages & Frameworks > PHP > Debugand set Debug Port to9001,9003. -
Open
PHPStorm > Preferences > Languages & Frameworks > PHP > DBGp Proxyand set Port to9001. -
Open
PHPStorm > Preferences > Languages & Frameworks > PHP > Serversand create a new server:- Set Name and Host to your domain name (ex.
magento.test) - Keep port set to
80 - Check the Path Mappings box and map
srcto the absolute path of/var/www/html
- Set Name and Host to your domain name (ex.
-
Go to
Run > Edit Configurationsand create a newPHP Remote Debugconfiguration by clicking the plus sign and selecting it. Set the Name to your domain (ex.magento.test). Check theFilter debug connection by IDE keycheckbox, select the server you just setup, and under IDE Key enterPHPSTORM. This IDE Key should match the IDE Key set by the Chrome Xdebug Helper. Then click OK to finish setting up the remote debugger in PHPStorm. -
Open up
src/pub/index.php, and set a breakpoint near the end of the file. Go toRun > Debug 'magento.test', and open up a web browser. Ensure the Chrome Xdebug helper is enabled by clicking on it > Debug. Navigate to your Magento store URL, and Xdebug within PHPStorm should now trigger the debugger and pause at the toggled breakpoint.
Running Docker on Linux should be pretty straight-forward. Note that you need to run some post install commands as well as installing Docker Compose. These steps are taken care of automatically with Docker Desktop, but not on Linux.
Be sure to see the "Linux only" documentation in the docker-compose.dev.yml file. The extra_hosts param is required to be defined on Linux for proper DNS resolution.
You may also have to increase a virtual memory map count on the host system. It is required by Elasticsearch.
Add following line to /etc/sysctl.conf:
vm.max_map_count=262144
To enable Xdebug on linux, you'll also need to open port 9001 on the firewall with:
sudo iptables -A INPUT -p tcp --dport 9001 -j ACCEPT
These docker images have built-in support for Blackfire.io. To use it, first register your server ID and token with the Blackfire agent:
bin/root blackfire-agent --register --server-id={YOUR_SERVER_ID} --server-token={YOUR_SERVER_TOKEN}
Next, open up the bin/start helper script and uncomment the line:
#bin/root /etc/init.d/blackfire-agent start
Finally, restart the containers with bin/restart. After doing so, everything is now configured and you can use a browser extension to profile your Magento store with Blackfire.
This project has been possible thanks to the following resources: