Magento2 Development in Docker: The Ultimate Guide

Magento2 is a powerful eCommerce platform, but setting up a development environment can be complex. Docker simplifies this by providing an isolated, pre-configured environment where developers can build, test, and debug Magento2 efficiently.
At Ariya InfoTech, we use Docker to streamline Magento2 development. Yuvraj Raulji, a Magento2 expert, says:
“Docker eliminates the ‘it works on my machine’ problem by ensuring a consistent Magento2 development environment for all developers.”
In this guide, we’ll explore how to set up, configure, and optimize Magento2 development in Docker.
Why Use Docker for Magento2 Development?
- Easy Setup: No need to install services manually.
- Consistency: Works identically across different environments.
- Faster development : Quickly create and destroy environments.
- Modular: Easily add or remove services like Redis, Elasticsearch, or RabbitMQ.
- Isolation: Avoid conflicts with local system dependencies.
Step 1: Install Docker & Docker Compose
For Windows & macOS
- Download Docker Desktop from https://www.docker.com
- Follow the installation instructions.
- Enable WSL 2 Backend (for Windows users).
- Verify installation:
sh
CopyEdit
docker –version
docker-compose –version
For Linux (Ubuntu/Debian-based systems)
CopyEdit
sudo apt update
sudo apt install -y docker.io docker-compose
sudo systemctl start docker
sudo systemctl enable docker
Verify installation:
CopyEdit
docker –version
docker-compose –version
Step 2: Set Up a Magento2 Development Environment
2.1 Create a Project Directory
CopyEdit
mkdir magento-docker && cd magento-docker
2.2 Clone Magento 2 Source Code
CopyEdit
mkdir app && cd app
git clone https://github.com/magento/magento2.git .
2.3 Create a docker-compose.yml File
yaml
CopyEdit
version: ‘3.7’
services:
app:
image: magento/magento2
container_name: magento_app
restart: always
depends_on:
– db
– redis
volumes:
– ./app:/var/www/html
environment:
– MYSQL_HOST=db
– MYSQL_USER=magento
– MYSQL_PASSWORD=magento
– MYSQL_DATABASE=magento
ports:
– “80:80″db:
image: mysql:5.7
container_name: magento_db
restart: always
environment:
– MYSQL_ROOT_PASSWORD=root
– MYSQL_DATABASE=magento
– MYSQL_USER=magento
– MYSQL_PASSWORD=magento
ports:
– “3306:3306″elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.9.3
container_name: magento_elasticsearch
environment:
– discovery.type=single-node
– ES_JAVA_OPTS=-Xms512m -Xmx512m
ports:
– “9200:9200″redis:
image: redis:latest
container_name: magento_redis
restart: always
ports:
– “6379:6379”
Step 3: Start Magento2 in Docker
Run the following command in the magento-docker directory:
CopyEdit
docker-compose up -d
This will:
✅ Download necessary Docker images.
✅ Start Magento2 and its required services.
✅ Mount the Magento2 source code inside the container.
Step 4: Install Magento2 in the Container
After starting the containers, install Magento2 by running:
CopyEdit
docker exec -it magento_app bin/magento setup:install \
–base-url=http://localhost/ \
–db-host=db \
–db-name=magento \
–db-user=magento \
–db-password=magento \
–admin-firstname=Admin \
–admin-lastname=User \
–[email protected] \
–admin-user=admin \
–admin-password=Admin123 \
–language=en_US \
–currency=USD \
–timezone=America/New_York \
–use-rewrites=1
This will:
✅ Download necessary Docker images.
✅ Start Magento2 and its required services.
✅ Mount the Magento2 source code inside the container.
Step 5: Develop & Manage Magento2 in Docker
5.1 Restart Containers
CopyEdit
docker-compose restart
5.2 Stop Containers
CopyEdit
docker-compose down
5.3 Run Magento CLI Commands Inside the Container
CopyEdit
docker exec -it magento_app bin/magento cache:flush
docker exec -it magento_app bin/magento indexer:reindex
docker exec -it magento_app bin/magento setup:upgrade
5.4 View Logs
CopyEdit
docker logs magento_app
Step 6: Customizing Docker for Magento2 Development
6.1 Use a Custom php.ini File
ini
CopyEdit
memory_limit = 2G
max_execution_time = 1800
upload_max_filesize = 128M
post_max_size = 128MModify docker-compose.yml to include:
yaml
CopyEdit
volumes:
– ./custom-config/php/php.ini:/usr/local/etc/php/conf.d/custom-php.iniRestart Docker:
sh
CopyEdit
docker-compose down && docker-compose up -d
6.2 Add Mailhog for Email Testing
CopyEdit
mailhog:
image: mailhog/mailhog
container_name: magento_mailhog
ports:
– “1025:1025”
– “8025:8025”
Magento Email Testing URL: http://localhost:8025
Step 7: Debugging Magento2 in Docker
7.1 Enable Xdebug
yaml
CopyEdit
environment:
XDEBUG_MODE: debug
XDEBUG_CONFIG: “client_host=host.docker.internal”Restart containers:
sh
CopyEdit
docker-compose down && docker-compose up -d
7.2 Debug with PHPStorm
- Configure PHP Remote Debugging in PHPStorm.
- Use Server Name: localhost and set Path Mappings to /var/www/html.
- Enable Breakpoints and start listening for PHP Debug Connections.
Final Thoughts
Using Docker for Magento2 development improves efficiency, ensures consistency, and simplifies debugging. At Ariya InfoTech, we use Docker to create a smooth Magento development workflow. Yuvraj Raulji highly recommends this approach for Magento developers.
Next Steps: Stay tuned for our next guide on optimizing Magento2 performance in Docker!