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

created June 25, 2017, last updated August 4, 2020.

.

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.4 and Apache 2.4 built on the latest PHUSION docker images
  • 2 Redis servers for Session and System (including FPC)
  • MYSQL 8
  • PhpMyAdmin
  • Varnish 6 for FPC
  • RabbitMQ
  • Management container and Scale Manager for scaled service dynamic load balancing
  • composer, n98-magerun2, PHP Redis Admin, XDEBUG
  • Magento 2.4.0 (July 2020)

Requirements

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

Base Images / Source for 2.4 deployment

Deployment

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

  • Clone the source file repository from, GitHub
    • 2.4
      • git clone https://github.com/gaiterjones/docker-magento2
  • 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

Script install

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

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

 

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.

manager container

In production I prefer to use a management container for all magento admin and cli commands. The manager container can be used as the main administration container for your production environment. You should move all cron jobs to the manager and you can also move web administration to the manager container. This will free up the php-apache container just to be used to serve frontend web requests and allows you to use the scale function. You will also notice that separating cron tasks into a separate container reduces the memory requirement of the php-apache container significantly.

To start an admin bash session in the management container use

docker exec -i -t --user magento magento2_php-apache_1 /bin/bash

References

Comments

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