Docker Magento CE v2.x Development Deployment with PHP7.2, Apache2.4, REDIS, VARNISH, Scaleable

created June 25, 2017, last updated February 14, 2019.

.

Installing Magento 2 in a development environment can be time consuming. This is my Magento 2 development deployment for Docker which can be used to quickly create a new Magento 2 Open Source (formerly known as CE) environment, or switch between persistent development environment volumes.

Features

  • PHP 7.2 and Apache 2.4 built on the latest PHUSION docker images
  • 2 Redis servers for Session and System (including FPC)
  • MYSQL
  • PhpMyAdmin
  • Varnish 4.x or 5.2 for FPC
  • Scale Manager for scaled service dynamic load balancing
  • composer, n98-magerun2, PHP Redis Admin, XDEBUG
  • Magento 2.1.12 (April 2018)
  • Magento 2.3.9 (February 2019)

Requirements

  • Host server with min 2GB RAM
  • Docker Engine
  • Docker Compose 3.1

Base Images / Source for 2.1 and 2.3 deployment

Deployment

Here are the steps to deploy this Magento 2 development environment:

  • Clone the source file repository from, GitHub
    • 2.1
      • git clone https://github.com/gaiterjones/docker-magento2
    • 2.3
      • git clone https://github.com/gaiterjones/docker-magento2-2
  • Change into the Magento 2 directory
    • cd magento2
  • Edit the .env file and set the working environment for your deployment :
    • APPDOMAIN=dev.com
      • the domain name for the magento installation
    • SMTP=XXX
      • the smtp gateway for your deployment, required for Magento email.
    • MAGENTO_URL=http://magento2.dev.com
      • the Magento URL for your deployment
    • MAGENTO_REPO_USERNAME/PASSWORD
      • your Magento repo authentication details required for Magento installation
    • CONTAINERDATA=/home/docker/data/
      • Used for persistent container data
  • Edit docker-compose.yml
    • You can remove the Manager and Memcache service here if you do not want to deploy the scale manager.
    • for mysql persistent data uncomment the following from the mysql image
      • #– “${CONTAINERDATA}/${PROJECT_NAME}/mysql/dev1:/var/lib/mysql”
    • If you are using Nginx as a container reverse proxy change the ports and networks for the varnish service accordingly.
  • Generate a Varnish Secret
    • You can create a new varnish secret by running newsecret.sh in the varnish folder.
  • BUILD
    • docker-compose build
  • START
    • docker-compose up -d
      • Allow 60 seconds after starting for composer to initialise
  • Install Magento Sample Data (optional)
    • docker exec -it magento2_php-apache_1 install-sampledata
      • ignore error Can’t run this operation: deployment configuration is absent.
  • Install Magento
    • docker exec -it magento2_php-apache_1 install-magento
  • Change to developer mode
    • docker exec -it magento2_php-apache_1 su www-data -c “php ./bin/magento deploy:mode:set developer”
  • Update composer
    • docker exec -it magento2_php-apache_1 su www-data -c “composer update”
  • Configure varnish cache
    • docker exec -it magento2_php-apache_1 su www-data -c “php ./bin/magento setup:config:set –http-cache-hosts=varnish”
  • Manually edit env.php to enable redis system, session and FPC
    • see env.txt for example config

Script install

For a quick test, spin up a Type 2 packet.net server running Ubuntu LTS 16.04, install Docker CE and docker compose and then run

docker exec -it magento2_php-apache_1 su root -c "curl -o- http://gaiterjones.com/dropbox/magento2/docker/install.sh | bash"

With an Intel Xeon E5, 96 Cores and 252GB RAM you will really see Magento 2 fly.

magento 2 docker containers install video

Testing

  • frontend
      • http://magento2.dev.com
  • backend
      • http://magento2.dev.com/admin/
        • login using the credentials you set in .env
      • Configure Varnish FPC
        • Stores-> Configuration -> Advanced -> Full Page Cache
    • Test from containers with curl (optional)
      • curl -I -H “host: magento2.dev.com” magento2_php-apache_1:80
      • curl -H “host: magento2.dev.com” magento2_php-apache_1:80/healthcheck.php
    • n98-magerun2
      • docker exec -it magento2_php-apache_1 su www-data -c “/usr/local/bin/n98-magerun2.phar”

Persistent volumes

Your development deployment data will be lost when you restart the php-apache or sql containers. If you want data to persist after installation perform the following steps:

  • Tar the Magento source files in the php-apache container
    • docker exec -it magento2_php-apache_1 tar -cvf /home/data/magento2.tar /var/www/dev
    • The tar file will be saved to the docker container data folder specified in .env
  • Stop the containers
    • docker-compose down -v
  • Untar to host
    • tar -xvf magento2.tar
  • Move the /var/www/dev folder to your persistent data folder i.e. /home/docker/data/magento2/dev1
  • Uncomment the data volume in docker-compose.yml
    • #- “${CONTAINERDATA}/${PROJECT_NAME}/dev1:/var/www/dev”
  • Restart containers, Magento data is now persistent.

Scaling php-apache

You can scale the php-apache Magento service with

docker-compose scale php-apache=X

Note the scale manager is only working with Varnish 4. See this post for more information.

References

Comments

This site uses Akismet to reduce spam. Learn how your comment data is processed.