Performance Optimization with Docker & Magento2

Magento 2 is resource-heavy. Learn how running it in Docker streamlines development and improves performance, with the tweaks that matter most.

Yuvraj RauljiYuvraj RauljiRaulji Technologies Feb 10, 2025 4 min read Updated Jun 1, 2026 Intermediate
Quick Answer

Magento 2 is resource-heavy. Learn how running it in Docker streamlines development and improves performance, with the tweaks that matter most.

On this page

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 Raulji Technologies, 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
sh
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
Modify docker-compose.yml to include Redis:
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:
yaml
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:
yaml
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:
sh
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:
sh
CopyEdit
docker-compose down && docker-compose up -d

Final Thoughts

Optimizing Magento2 inside Docker significantly improves performance, reduces resource usage, and speeds up development. Raulji Technologies 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!

Frequently asked

Frequently Asked Questions

Answers to the questions we hear most often.

Is Elasticsearch still the right search engine to optimise for?

Not for a current build. Adobe's supported matrix for Magento 2.4.9, generally available since 12 May 2026, lists OpenSearch 3 and no longer lists Elasticsearch, which the 2.4.8 line was the last to include. The same release replaces Redis with Valkey 9 in the supported stack and moves Varnish to 8. Performance advice written against Elasticsearch 7 still applies conceptually, since OpenSearch is a fork of it, but tune against the engine your Magento version actually supports.

How much does site speed really affect eCommerce revenue?

Enough to fund the work. Google and Deloitte's Milliseconds Make Millions study found that a 0.1 second improvement in mobile site speed produced an 8.4 percent increase in retail conversions and a 9.2 percent increase in average order value. Portent's analysis of ecommerce sites found conversion rates falling around 4.42 percent for every additional second of load time. Those are averages across many sites rather than a guarantee for yours, but they establish that speed work is a revenue project, not a technical vanity project.

What are the Core Web Vitals thresholds a Magento store should hit?

Largest Contentful Paint at 2.5 seconds or less, Interaction to Next Paint at 200 milliseconds or less, and Cumulative Layout Shift at 0.1 or less, each measured at the 75th percentile of real visits. Note that these are field measurements from actual users, so a perfect score in a local Docker container proves nothing. Interaction to Next Paint replaced First Input Delay in March 2024 and is much harder to pass, because it measures every interaction rather than only the first one.

Can I trust performance numbers measured inside Docker?

Use them for comparison, not as absolute figures. A container on a laptop has different CPU, different disk behaviour, no CDN, no real network latency and a tiny dataset, so the numbers will flatter you. What Docker is genuinely good for is A versus B testing: change one setting, rerun the same benchmark, and see the direction of travel. Absolute performance verdicts belong on staging infrastructure that resembles production, validated afterwards against field data from real visitors.

Which single change gives the biggest Magento speed win?

Full page caching in front of PHP. A cached category or product page served by Varnish never reaches Magento at all, which turns a request that costs hundreds of milliseconds into one that costs a few. Everything else, meaning OPcache, database tuning and search configuration, improves the requests that do reach PHP. Get caching right first, then measure what is left. Sites that skip this and tune PHP settings instead are optimising the slow path while ignoring the one that eliminates it.

What PHP settings matter most for Magento performance?

OPcache above all, sized generously enough to hold Magento's very large codebase without evicting entries, with a realpath cache to match because Magento resolves an enormous number of file paths per request. Memory limit matters for CLI operations such as Composer and static content deployment rather than for page speed. Do not expect the JIT to help, since Magento requests are dominated by database, cache and I/O work rather than by the compute bound code JIT accelerates.

Should I use Redis or Valkey for Magento caching?

On 2.4.8 and earlier, Redis is supported and works well. On 2.4.9 Adobe's matrix lists Valkey 9, the community fork that continued from Redis's last open source release. It speaks the same protocol, so Magento's cache, session and page cache backends work against it without code changes and the switch is essentially an image change. Whichever you run, separate the cache and session databases, set a sensible eviction policy, and give the container enough memory that it is not evicting hot entries constantly.

How should I tune MySQL for Magento in a container?

The InnoDB buffer pool is the setting that matters, because it determines how much of a large schema lives in memory instead of on disk. After that, log file sizing, maximum allowed packet for big imports, and connection limits if you run many consumers. Do not copy a production configuration into a laptop container: a buffer pool sized for a dedicated database server will starve OpenSearch and PHP on the same machine and produce failures that look like a Magento problem.

How do I stop OpenSearch from becoming the bottleneck?

Give it enough memory, since it reserves a Java heap on startup and will exit or thrash if the container is starved, and keep its data on a named volume rather than a bind mount. Beyond resources, the wins are in Magento: limit which attributes are searchable and filterable, because every one of them expands the index, and avoid running a full reindex on every save during development. A search engine that keeps dying is a resourcing problem, not a tuning problem.

What nginx settings actually help a Magento site?

Start from the sample configuration Magento ships, which already handles the URL rewriting and static file routing correctly, and resist rewriting it from scratch. The useful additions are compression, long cache lifetimes with immutable headers on versioned static assets, sensible client body limits for admin uploads, and HTTP/2 or HTTP/3. Most real front end gains after that come from the theme, meaning image formats and sizes, fewer render blocking requests and less JavaScript, not from the web server.

Why is my Docker environment slow even after tuning?

Check file sharing before blaming configuration. Magento reads thousands of small files per request, and on Windows and macOS every one of those reads crosses a virtualisation boundary when the source lives on a shared host folder. Moving a project into the WSL2 filesystem on Windows, or using VirtioFS or a synced volume mode on macOS, often produces a larger improvement than every PHP and MySQL setting combined. On native Linux this problem does not exist.

Do local optimisations carry over to production?

The configuration does, the results do not. Committing your tuned PHP, cache, database and web server settings to the repository means production runs exactly what you tested, which is the real value of containerising. What will not carry over is the absolute speed, because production has real traffic, a real catalogue, a CDN and network latency. Validate with field data from real users after deployment rather than assuming a fast local environment predicts a fast store.

Yuvraj Raulji

Yuvraj Raulji

Verified expert

Founder

Founder of Raulji Technologies with expertise in enterprise eCommerce solutions. Specialized in Magento 2, Shopify, and headless commerce architecture. Driving growth through CRO, SEO, and performance engineering. Helping businesses turn technology into measurable revenue.
Share
Ready When You Are

Turn your store into a revenue machine

Our team has helped 150+ brands scale with Magento, Shopify and AI-powered solutions.

Get a Free Growth Plan
Stay in the loop

Get our latest insights by email

Practical eCommerce, Magento, Shopify and AI growth strategies. No spam, unsubscribe any time.

By subscribing you agree to our Privacy Policy.

Book Free Consultation

We're Trusted By Businesses Across The Globe

Discover why 100+ global brands choose Raulji Technologies for AI-driven eCommerce, web development, and digital transformation, scaling their digital growth with innovation, performance, and trust.

100+
Brands Served
150+
Projects Delivered
12+
Years Experience
4.9
Average Rating
Clutch 5.0

Clutch Verified Profile

Rated 5.0 by verified clients on Clutch for Magento, Shopify, and AI-driven digital transformation.

View Clutch Profile
DesignRush 5.0

DesignRush Verified Profile

Listed and reviewed on DesignRush as a top eCommerce and web development agency.

View DesignRush Profile
Google 5.0

Google Verified Profile

Reviewed by clients on Google across India, the Gulf, and worldwide for delivery and support.

Read Google Reviews