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

created June 25, 2017, last updated February 10, 2021.

.
closeThis post was last updated 6 years 8 months 26 days ago, some of the information contained here may no longer be actual and any referenced software versions may have been updated!

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.2 (Feb 2021)

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.4 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 --user magento 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 –user magento magento2_php-apache_1 /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 –user magento 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

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

  1. Hamza Abdullah says:

    I’m running in WSL 2 Ubuntu 20.04 – How is the .dev.com domain being accessed? It’s not working for me. I tried 127.0.0.1 (localhost) in the env file but that breaks the install command.

    Can someone please help here?

    • PAJ says:

      magento2.dev.com should point to the Docker host. For development and testing simply update your clients local host file to access the site.

      • Lucas says:

        I have a question, how do i get the docker host?, i have installed the project on a already created magento, so its on m2-commerce.dev.com should be the url, i have pasted all the files inside the m2-commerce folder i have run docker-compose build and up and everything is ok., but when going to the url i get:
        DNS_PROBE_FINISHED_NXDOMAIN

  2. Robinson says:

    After install magento ( docker exec -i -t –user magento magento2_php-apache_1 install-magento) the data sub-folder (CONTAINERDATA) was empty, but the mysql and rabbitmq folders have data. Please, could you help me?

    • PAJ says:

      You will see in docker-compose.yml that some data volumes are commented out for the php-apache container.

      When you have magento 2 installed you should create a tar backup of the php-apache container /var/www folder. Extract this to ${CONTAINERDATA}/${PROJECT_NAME}/www and then uncomment the corresponding volume config in docker-compose.yml. After restarting containers your data will then persist in the external volume.

  3. Max Lee says:

    I followed all the steps, ran ‘docker-compose build’, and whe I tried starting it up with ‘docker-compose up -d’. I got this error:

    ERROR: for magento2_mysql_1 Cannot start service mysql: Mounts denied:
    The path /home/docker/data/magento2/data is not shared from the host and is not known to Docker.
    You can configure shared paths from Docker -> Preferences… -> Resources -> File Sharing.
    See https://docs.docker.com/docker-for-mac for more info.

    I’ve configured shared /home/docker/data/magento2/data to my shared paths but still getting the error. Would you be able to help me?

  4. Jordi says:

    Hi, it doesn´t work for me. Manager image doesn´t run.
    This is the error: “exec /usr/local/bin/startmanager.sh: no such file or directory” on magento2-manager-1.
    Could anyone help?
    Thanks

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