Dockerized Magento example. Running Magento in a stack includes Nginx/PHP-FPM web server, Mysql, Redis, Varnish.
For database, we use mysql offical image. You can change the root password through environment variable MYSQL_ROOT_PASSWORD
. To keep the data persitence, we create a volume and mount it to a directory under var
: ./var/lib/mysql:/var/lib/mysql
To import the .sql
file to a database in the container, run this command in terminal:
docker exec -i <mysql_container_name> mysql -uroot -p<mysql_root_password> <datbase name> < dump.sql
Ex:
docker exec -i test_db_1 mysql -uroot -proot test < dump.sql
The docker-compose.yml
include a Varnish service. The default.vcl
file is generated by Turpentine - Varnish Cache extension for Magento and must be put in var
directory. The current Varnish version is 4.0
. If you don't want Varnish cache, just remove it from docker-compose.yml
- Find your host machine IP by running
ifconfig
(Mac, Linux) oripconfig
on Windows and add the value toXDEBUG_REMOTE_HOST
environment variable indocker-compose.yml
- Create a file named
server.location
inMagento root/var/nginx/
and put in that the lineserver_name your_virtual_host_url;
Ex:server_name test.local;
- Xdebug is configured to listen on port 9009, not the default 9000 (to avoid conflict with PHP-FPM). In PHPSTORM > Prefences > Languages & Frameworks > PHP > Debug, change the Debug port to 9009
- Also in PHP section > Servers, add a new server with name and host are same as the one in step 2. Check the option
User path mappings
and edit theAbsolute path on server
to/var/www/html
- Install the Xdebug bookmarlets so we can start and stop the debugging from browser. Link here https://www.jetbrains.com/phpstorm/marklets/
Happy debugging!
This is a script to shorten the docker-compose commands. It helps us much easier working with the containers. The script was copied from https://github.com/andreaskoch/dockerized-magento/blob/master/magento and customized to work with my stack.
To use it, run this command in the terminal: chmod +x magento && cp magento /usr/local/bin/dcw
. Then we will have a command called dcw
. It must be run from within the Magento root directory.
Available commands:
start Starts the docker containers (and triggers the
installation if magento is not yet installed)
stop Stops all docker containers
restart Restarts all docker containers
status Prints the status of all docker containers
stats Displays live resource usage statistics of all containers
magerun Executes magerun in the magento root directory
composer Executes composer in the magento root directory
enter Enters the bash of a given container type (e.g. php, mysql)
destroy Stops all containers and removes all data