Performance Optimization with Docker & Magento2

Magento2 is a feature-rich and powerful eCommerce platform, but it can be resource-intensive. Running Magento2 inside Docker helps streamline development and deployment, but without proper optimization, it can slow down.
At Ariya InfoTech, we focus on boosting Magento2 performance within Docker, ensuring a fast and smooth experience. Yuvraj Raulji, a Magento2 expert, says:
“Optimizing Magento2 with Docker is crucial to achieve faster page loads, efficient caching, and seamless development workflows.”
In this guide, we’ll cover performance optimization techniques for Magento2 in Docker, including caching, database tuning, PHP configurations, and resource optimizations.
Why Optimize Magento2 in Docker?
✅ Faster page load times – Speed up Magento’s performance.
✅ Better resource utilization – Reduce CPU and memory usage.
✅ Efficient caching – Improve response times with Varnish, Redis, and OpCache.
✅ Smoother development – Reduce wait times for builds and commands.
✅ Scalability – Prepare for high traffic and production deployments.
Step 1: Optimize PHP Configuration
Magento2 heavily depends on PHP, so optimizing it inside Docker improves performance significantly.
1.1 Use a Custom php.ini File
CopyEdit
mkdir -p custom-config/php
nano custom-config/php/php.ini
Add these performance tweaks:
ini
CopyEdit
memory_limit = 2G
max_execution_time = 1800
upload_max_filesize = 128M
post_max_size = 128M
opcache.enable=1
opcache.memory_consumption=512
opcache.max_accelerated_files=100000
opcache.validate_timestamps=0
Modify docker-compose.yml to load this file:
yaml
CopyEdit
app:
volumes:
– ./custom-config/php/php.ini:/usr/local/etc/php/conf.d/custom-php.ini
Restart Docker:
sh
CopyEdit
docker-compose down && docker-compose up -d
1.2 Enable PHP-FPM Process Manager
Modify php-fpm.conf:
sh
CopyEdit
nano custom-config/php/php-fpm.confAdd:
ini
CopyEdit
pm = dynamic
pm.max_children = 75
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20
Restart Docker to apply changes.
Step 2: Improve Caching with Redis & Varnish
2.1 Use Redis for Session & Cache Storage
yaml
CopyEdit
redis:
image: redis:latest
container_name: magento_redis
restart: always
ports:
– “6379:6379″Set Magento to use Redis:
sh
CopyEdit
docker exec -it magento_app bin/magento setup:config:set \
–cache-backend=redis \
–cache-backend-redis-server=redis \
–cache-backend-redis-db=0
docker exec -it magento_app bin/magento setup:config:set \
–session-save=redis \
–session-save-redis-host=redis \
–session-save-redis-db=2
docker exec -it magento_app bin/magento cache:flush
2.2 Configure Varnish for Full-Page Caching
Modify docker-compose.yml to add Varnish:
yaml
CopyEdit
varnish:
image: varnish:6.5
container_name: magento_varnish
restart: always
depends_on:
– app
ports:
– “6081:6081″Create a Varnish configuration file (default.vcl):
sh
CopyEdit
mkdir -p custom-config/varnish
nano custom-config/varnish/default.vcl
Add:
vcl
CopyEdit
vcl 4.0;
backend default {
.host = “app”;
.port = “80”;
}
sub vcl_recv {
if (req.url ~ “^/admin”) {
return (pass);
}
}
Enable Varnish in Magento:
sh
CopyEdit
docker exec -it magento_app bin/magento config:set –scope=default –scope-code=0 system/full_page_cache/caching_application 2
docker exec -it magento_app bin/magento cache:flush
Step 3: Optimize MySQL Performance
Magento2 queries can be intensive. Fine-tuning MySQL inside Docker boosts database speed.
3.1 Create a Custom MySQL Config File
sh
CopyEdit
mkdir -p custom-config/mysql
nano custom-config/mysql/my.cnfAdd:
ini
CopyEdit
[mysqld]
innodb_buffer_pool_size=1G
innodb_log_file_size=256M
max_connections=200
query_cache_size=64M
Modify docker-compose.yml to use this config:
yaml
CopyEdit
db:
volumes:
– ./custom-config/mysql/my.cnf:/etc/mysql/my.cnf
Restart MySQL:
sh
CopyEdit
docker-compose restart db
Step 4: Use Elasticsearch for Search Performance
Magento2 uses Elasticsearch for fast product search.
Modify docker-compose.yml to add Elasticsearch:
CopyEdit
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″Set Magento to use Elasticsearch:
sh
CopyEdit
docker exec -it magento_app bin/magento config:set catalog/search/engine elasticsearch7
docker exec -it magento_app bin/magento indexer:reindex
Step 5: Use Nginx for Better Performance
Modify docker-compose.yml to include Nginx:
CopyEdit
nginx:
image: nginx:latest
container_name: magento_nginx
restart: always
depends_on:
– app
volumes:
– ./app:/var/www/html
– ./custom-config/nginx/default.conf:/etc/nginx/conf.d/default.conf
ports:
– “443:443”
Create a custom Nginx config file:
CopyEdit
mkdir -p custom-config/nginx
nano custom-config/nginx/default.conf
Add:
nginx
CopyEdit
server {
listen 443 ssl;
server_name localhost;root /var/www/html;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Restart Docker:
CopyEdit
docker-compose down && docker-compose up -d
Final Thoughts
Optimizing Magento2 inside Docker significantly improves performance, reduces resource usage, and speeds up development. Ariya InfoTech and Yuvraj Raulji highly recommend these optimizations for every Magento2 developer.
Next Steps: Stay tuned for our next guide on scaling Magento2 in Docker with Kubernetes and CI/CD pipelines!