This is the third Docker post in a row and the sequel to Docker WordPress. For this Docker Magento 2 development environment I chose the prepared containers from mageinferno, who published this tutorial: magento2-docker-compose.
Unlike setting up Docker for WordPress, there are a little more steps to achieve a working Docker Magento 2 development environment:
Of course, first we need to create folder with a docker-compose.yml file with the following conent:
version: "3" services: app: image: mageinferno/magento2-nginx:1.11-1 links: - db - phpfpm volumes: &appvolumes - ~/.composer:/var/www/.composer - appdata:/var/www/html #- ./html/app/code:/var/www/html/app/code #- ./html/app/design:/var/www/html/app/design ports: - 8000:80 networks: &appnetworks - www phpfpm: image: mageinferno/magento2-php:7.0-fpm-1 links: - db volumes: *appvolumes networks: *appnetworks db: image: percona:5.7 volumes: - dbdata:/var/lib/mysql ports: - 8001:3306 networks: *appnetworks environment: - MYSQL_ROOT_PASSWORD=magento2 - MYSQL_DATABASE=magento2 - MYSQL_USER=magento2 - MYSQL_PASSWORD=magento2 setup: image: mageinferno/magento2-php:7.0-fpm-1 command: /usr/local/bin/mage-setup links: - db volumes: *appvolumes networks: *appnetworks environment: - M2SETUP_DB_HOST=db - M2SETUP_DB_NAME=magento2 - M2SETUP_DB_USER=magento2 - M2SETUP_DB_PASSWORD=magento2 - M2SETUP_BASE_URL=http://magetest:8000/ - M2SETUP_ADMIN_FIRSTNAME=Admin - M2SETUP_ADMIN_LASTNAME=User - M2SETUP_ADMIN_EMAIL=dummy@gmail.com - M2SETUP_ADMIN_USER=magento2 - M2SETUP_ADMIN_PASSWORD=magento2 - M2SETUP_CURRENCY=EUR - M2SETUP_LANGUAGE=en_US - M2SETUP_TIMEZONE=America/New_York - M2SETUP_USE_SAMPLE_DATA=FALSE - M2SETUP_USE_ARCHIVE=FALSE - M2SETUP_USE_COMPOSER_ENTERPRISE=FALSE - M2SETUP_VERSION=2.1.6 volumes: test: dbdata: appdata: networks: www:
Before starting the containers, we need to setup a Magento Marketplace authentication, which is needed to retrieve the Magento 2 files during the installation process. When you retrieve the mentioned username and password, put them in a file ~/.composer/auth.json on you local machine. Content of this file should be like this:
{ "http-basic": { "repo.magento.com": { "username": "MAGENTO_PUBLIC_KEY", "password": "MAGENTO_PRIVATE_KEY" } } }
Now we are ready to run:
docker-compose run --rm setup
The images will now start downloading, and after that Magento 2 will be automatically installed with the help of composer.
Ok, now Magento is installed. But before we are ready to use those containers, we need to stop all the running containers and than uncomment the following two lines insed docker-compose.yml file:
- ./html/app/code:/var/www/html/app/code - ./html/app/design:/var/www/html/app/design
Now we can use:
docker-compose up -d app
and the containers will start and be ready for usage.
All the customization has to be done through the volumes ./html/app/code and ./html/app/design. Try to avoid enabling (mounting) volume appdata (/var/www/html) because if you won’t have all the files in the mounted folder, that the container won’t see any files.
I usually first set an alias (you have to be in the directory where your docker-compose.yml file is located):
alias mage='docker-compose exec phpfpm'
And now all the magento CLI tools are accessed by
magento bin/magento [CLI-command]
Example:
mage bin/magento cache:flush
According to the mageinferno tutorial, you can use the following:
docker cp CONTAINERID:/var/www/html ./
Unfortunately, I was not able to achieve it, because of some permission problem popping out at some files, which terminated the copying process somewhere at ~50%. I found the following workaround:
docker exec -i -t [container ID] bash
tar -zcvf html.tar.gz html > /dev/null
exit
docker cp [container ID]:/var/www/html.tar.gz ./html.tar.gz