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
- gaiterjones/magento2 for 2.4
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
- 2.4
- 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
- APPDOMAIN=dev.com
- 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
- docker-compose up -d
- 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.
- docker exec -it magento2_php-apache_1 install-sampledata
- 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"
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
- http://magento2.dev.com/admin/
- 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
Jeff J says:
Great guide! I was able to get up and running in minutes.
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
Martin says:
Could you elaborate more on this pointing to the docker host? I don’t get this….
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.
Robinson Mioshi says:
I got it. Thank you!
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?
PAJ says:
I take it you are using docker for mac? there seems to be restrictions on the paths you can use to map volumes, this link may help you https://stackoverflow.com/questions/45122459/mounts-denied-the-paths-are-not-shared-from-os-x-and-are-not-known-to-docke/45123074#45123074
The enabled volumes mapped to the host let you persist container data, or copy data from container to host, for testing you can comment them all out.
Esequiel says:
HI, to change https,and install certificates? It is posible?
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
roland says:
I followed steps and varnish is exited(255) Whats wrong? Windows 10
roland says:
I followed steps and varnish is exited(255) What’s wrong? Windows10
PAJ says:
There is an issue with Unix LF line endings in Windows, see this issue for more info : https://github.com/gaiterjones/docker-magento2/issues/1#issuecomment-1430884593
vishnu says:
how can i install different magento version? on default it installs2.4.6
PAJ says:
You can see all previous Magento versions on the releases page of the GitHub