Magento2 is a powerful eCommerce platform, but setting up and optimizing its development environment can be complex. While Docker Compose provides a convenient way to manage Magento2, customizing it to fit your needs can significantly improve performance and workflow.
At Raulji Technologies, we believe in optimizing Magento2 for the best development experience. Yuvraj Raulji, a Magento 2 expert, says:
“Customizing Docker for Magento2 allows developers to enhance performance, add new services, and tailor the setup to their specific project needs.”
In this guide, we’ll walk you through customizing Docker for Magento 2, including custom PHP settings, Varnish integration, SSL support, and performance optimizations.
Why Customize Docker for Magento2?
- Improve performance: Optimize PHP, MySQL, and caching settings.
- Add new services: Include Varnish, Redis, Mailhog, and Elasticsearch.
- Use custom PHP settings: Adjust memory limits, execution times, and extensions.
- Enable SSL support: Run Magento 2 with HTTPS.
Step 1: Customize PHP Configuration
Magento2 requires specific PHP configurations to run efficiently. Instead of using the default PHP settings in Docker, create a custom PHP.ini file.
1.1 Create a Custom php.ini File
- Inside your Magento project directory, create a php.ini file:
sh
CopyEdit
mkdir -p custom-config/php
nano custom-config/php/php.ini - Add the following Magento-optimized settings:
ini
CopyEdit
memory_limit = 2G
max_execution_time = 1800
upload_max_filesize = 128M
post_max_size = 128M
zlib.output_compression = On
display_errors = On
1.2 Modify docker-compose.yml to Load php.ini
- Update the docker-compose.yml file by adding a volume to the app service:
yaml
CopyEdit
services:
app:
image: magento/magento2
container_name: magento_app
restart: always
volumes:
– ./app:/var/www/html
– ./custom-config/php/php.ini:/usr/local/etc/php/conf.d/custom-php.ini - Restart Docker for changes to take effect:
sh
CopyEdit
docker-compose down && docker-compose up -d
Step 2: Add Varnish for Full-Page Caching
Varnish speeds up Magento2 by caching pages and serving them quickly.
2.1 Add Varnish Service to
docker-compose.yml
CopyEdit
varnish:
image: varnish:6.5
container_name: magento_varnish
depends_on:
– app
volumes:
– ./custom-config/varnish/default.vcl:/etc/varnish/default.vcl
ports:
– “6081:6081”
2.2 Create a Custom
default.vcl Configuration
CopyEdit
mkdir -p custom-config/varnish
nano custom-config/varnish/default.vcl
Add the following content:
CopyEdit
vcl 4.0;
backend default {
.host = “app”;
.port = “80”;
}
sub vcl_recv {
if (req.url ~ “^/admin”) {
return (pass);
}
}
Restart Docker and enable Varnish:
CopyEdit
docker-compose down && docker-compose up -d
docker exec -it magento_app bin/magento config:set –scope=default –scope-code=0 system/full_page_cache/caching_application 2
Step 3: Enable HTTPS with a Self-Signed SSL Certificate
3.1 Generate SSL Certificate
CopyEdit
mkdir -p custom-config/nginx
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout custom-config/nginx/server.key -out custom-config/nginx/server.crt -subj “/CN=localhost”
3.2 Modify docker-compose.yml to Include SSL
CopyEdit
nginx:
image: nginx:latest
container_name: magento_nginx
restart: always
depends_on:
– app
volumes:
– ./app:/var/www/html
– ./custom-config/nginx/server.crt:/etc/nginx/ssl/server.crt
– ./custom-config/nginx/server.key:/etc/nginx/ssl/server.key
– ./custom-config/nginx/default.conf:/etc/nginx/conf.d/default.conf
ports:
– “443:443”
3.3 Create a Custom Nginx Configuration
CopyEdit
nano custom-config/nginx/default.conf
Add the following:
nginx
CopyEdit
server {
listen 443 ssl;
server_name localhost;ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;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 and configure Magento to use HTTPS:
CopyEdit
docker-compose down && docker-compose up -d
docker exec -it magento_app bin/magento setup:store-config:set –base-url-secure=”https://localhost/”
Step 4: Optimize MySQL Performance
4.1 Create a Custom MySQL Config File
CopyEdit
mkdir -p custom-config/mysql
nano custom-config/mysql/my.cnf
Add these performance optimizations:
CopyEdit
[mysqld]
innodb_buffer_pool_size=1G
innodb_log_file_size=256M
max_connections=200
query_cache_size=64M
4.2 Update docker-compose.yml to Load my.cnf
CopyEdit
db:
image: mysql:5.7
container_name: magento_db
restart: always
volumes:
– ./custom-config/mysql/my.cnf:/etc/mysql/my.cnf
Restart the MySQL container:
CopyEdit
docker-compose restart db
Step 5: Add Mailhog for Email Testing
Magento sends transactional emails, but in development, you don’t want real emails sent. Mailhog captures emails for local testing.
5.1 Add Mailhog Service to docker-compose.yml
CopyEdit
mailhog:
image: mailhog/mailhog
container_name: magento_mailhog
ports:
– “1025:1025”
– “8025:8025”
Configure Magento to Use Mailhog
CopyEdit
docker exec -it magento_app bin/magento setup:config:set –smtp-host=”mailhog” –smtp-port=”1025″
Now, visit http://localhost:8025 to see captured emails.
Final Thoughts
Customizing Docker for Magento2 improves performance, security, and workflow efficiency. At Raulji Technologies, we use these optimizations in real-world projects to enhance Magento development. Yuvraj Raulji highly recommends these Docker customizations to Magento developers.
Next Steps: Stay tuned for more Magento2 Docker optimizations, including scaling with Kubernetes and CI/CD integration.