On this page
Magento2 is a powerful and flexible eCommerce platform, but deploying it can be challenging due to its multiple dependencies. Docker simplifies the process by allowing you to package Magento2, along with its required services, into portable and scalable containers.
At Raulji Technologies, we use Docker to deploy Magento2 quickly and efficiently. Yuvraj Raulji, a Magento2 expert, explains:
“With Docker, Magento2 deployment becomes a smooth, repeatable process, eliminating compatibility issues and reducing server setup time.”
In this guide, we will cover:
✅ Setting up a production-ready Magento2 Docker environment
✅ Configuring services like MySQL, Redis, Elasticsearch, and Varnish
✅ Running Magento2 using Docker Compose
✅ Deploying Magento2 on a cloud server
Why Use Docker for Magento2 Deployment?
✅ Quick deployment – No manual installations required.
✅ Scalability – Easily scale with Docker Swarm or Kubernetes.
✅ Consistency – Ensures the same setup across development, staging, and production.
✅ Portability – Deploy Magento2 on any cloud platform (AWS, DigitalOcean, Google Cloud).
✅ Easy rollback – Quickly revert to a previous version.
Step 1: Prepare the Server for Deployment
Before deploying Magento2 with Docker, ensure your cloud server has Docker and Docker Compose installed.
1.1 Install Docker & Docker Compose
CopyEdit
sudo apt update
sudo apt install -y docker.io docker-compose
sudo systemctl enable docker
Verify installation:
CopyEdit
docker –version
docker-compose –version
1.2 Set Up a Magento2 Directory
CopyEdit
mkdir magento-docker && cd magento-docker
Step 2: Create a docker-compose.yml File
This file defines all the services Magento2 needs: Nginx, PHP, MySQL, Redis, Elasticsearch, and Varnish.
2.1 Create the docker-compose.yml File
CopyEdit
version: ‘3.7’
services:
app:
image: magento/magento2
container_name: magento_app
restart: always
depends_on:
– db
– redis
– elasticsearch
volumes:
– ./app:/var/www/html
environment:
– MYSQL_HOST=db
– MYSQL_USER=magento
– MYSQL_PASSWORD=magento
– MYSQL_DATABASE=magento
ports:
– “9000:9000″nginx:
image: nginx:latest
container_name: magento_nginx
restart: always
depends_on:
– app
volumes:
– ./app:/var/www/html
– ./nginx/default.conf:/etc/nginx/conf.d/default.conf
ports:
– “80:80”
– “443:443″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″redis:
image: redis:latest
container_name: magento_redis
restart: always
ports:
– “6379:6379″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″varnish:
image: varnish:6.5
container_name: magento_varnish
restart: always
depends_on:
– nginx
ports:
– “6081:6081″volumes:
magento_data:
Step 3: Start the Magento2 Containers
Run the following command:
CopyEdit
docker-compose up -d
This will:
✅ Download and start all required services.
✅ Mount Magento2 source code inside the container.
✅ Expose Magento on port 80 and 443.
Step 4: Install Magento2 in Docker
Run the Magento2 installation command inside the app container:
CopyEdit
docker exec -it magento_app bin/magento setup:install \
–base-url=http://your-domain.com/ \
–db-host=db \
–db-name=magento \
–db-user=magento \
–db-password=magento \
–admin-firstname=Admin \
–admin-lastname=User \
–admin-email=admin@example.com \
–admin-user=admin \
–admin-password=Admin123 \
–language=en_US \
–currency=USD \
–timezone=America/New_York \
–use-rewrites=1
After installation, clear cache:
CopyEdit
docker exec -it magento_app bin/magento cache:flush
Step 5: Configure Magento 2 for Production
5.1 Enable Production Mode
CopyEdit
docker exec -it magento_app bin/magento deploy:mode:set production
5.2 Configure Redis for Caching
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
5.3 Configure Elasticsearch for Search
CopyEdit
docker exec -it magento_app bin/magento config:set catalog/search/engine elasticsearch7
docker exec -it magento_app bin/magento indexer:reindex
Step 6: Set Up SSL with Let's Encrypt (Optional)
If your server has a domain, install Certbot for free SSL:
CopyEdit
sudo apt install certbot python3-certbot-nginx
sudo certbot –nginx -d your-domain.com -d www.your-domain.com
Renew SSL automatically:
CopyEdit
sudo crontab -e
Add this line to renew every 90 days:
CopyEdit
0 3 * * * certbot renew –quiet
Step 7: Monitor and Manage Magento2 in Docker
7.1 Restart Containers
CopyEdit
docker-compose restart
7.2 Stop Containers
CopyEdit
docker-compose down
7.3 Run Magento CLI Commands
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
7.4 View Logs
CopyEdit
docker logs magento_app
Final Thoughts
Using Docker for Magento2 deployment ensures a fast, scalable, and repeatable setup. Raulji Technologies and Yuvraj Raulji recommend this approach for any Magento2 store, whether for development, staging, or production.
Next Steps: Stay tuned for our next guide on scaling Magento 2 with Kubernetes and CI/CD pipelines!
Frequently Asked Questions
Answers to the questions we hear most often.
Is Docker actually supported for Magento in production?
On your own infrastructure, yes, and many merchants run containerised Magento successfully. There is one important exception. If your store is on Adobe Commerce Cloud, Cloud Docker for Commerce is a local development and testing tool that mirrors the cloud pipeline, not the thing that runs your production site. Adobe manages that environment, and your compose file has no role in it. Confirm which situation you are in before designing a deployment process around containers you may not control.
Which versions should a production Magento container stack pin?
Whatever Adobe supports for your release. Magento Open Source 2.4.9 became generally available on 12 May 2026 with support for PHP 8.5, Composer 2.10, MySQL 8.4 or MariaDB 12.3, OpenSearch 3, Valkey 9, RabbitMQ 4.3, Varnish 8 and nginx 1.30. Running an unsupported combination in production means security patches may not apply cleanly and Adobe support has no obligation to help. Pin exact versions, never latest, so a rebuild during an incident does not silently change your platform.
Why does patching matter more than any deployment convenience?
Because unpatched Magento gets exploited fast. Sansec found that around 75 percent of Magento and Adobe Commerce stores were still unpatched a week after the fix for CosmicSting, tracked as CVE-2024-34102, and thousands were subsequently breached. Containers help here if you use them properly: rebuilding an image with the patched version and redeploying is faster and more auditable than patching a live server by hand. They hurt if a pinned base image quietly becomes the reason nobody upgrades.
Should the database run in a container in production?
Usually not. Stateless services such as PHP, nginx and Varnish are excellent container candidates because they can be replaced at will. A database is the opposite: it is the state, and it wants stable storage, tuned hardware, backups, replication and a careful upgrade path. Most production teams run Magento's application tier in containers and use a managed database and a managed search cluster. That combination keeps the deployment benefits without putting your only copy of the orders table inside an ephemeral process.
How should secrets be handled in a production container?
Not in the image and not in the compose file. Anything baked into an image is readable by anyone who can pull it, and image layers keep deleted files. Use your orchestrator's secret mechanism or an external secret manager, inject values at runtime, and keep Magento's env.php generated from those values rather than committed with real credentials. Rotate the Adobe Composer keys, database passwords and API credentials on a schedule, and make sure a leaked repository would not be enough to reach production.
How do I deploy without taking the store offline?
Build the artefact before you switch traffic. Compile dependency injection and deploy static content during the image build or on a separate release path, run database schema and data upgrades once, then move traffic to the new containers and retire the old ones. Magento's maintenance mode should cover only the schema upgrade window, not the whole build. The failure mode to avoid is compiling on the live server, which is slow, memory hungry and leaves the store in a half updated state if it fails.
What has to persist outside the containers?
Media files, the var directory contents you actually need, the database, the search indexes and your logs. Anything a container writes that you would miss after a redeploy must live on a volume, an object store or a managed service. Product images are the classic mistake: written into the container filesystem, they vanish on the next deployment and the catalogue fills with broken images. Object storage or a shared filesystem for pub/media is the standard answer for multi-container setups.
How do I handle SSL certificates in production?
Terminate TLS at a load balancer, a reverse proxy or a CDN rather than inside the application container, and automate renewal. Let's Encrypt certificates are valid for 90 days and are designed to be renewed automatically, so a manual process will eventually expire on a weekend. Make sure Magento knows it is behind a proxy, otherwise it generates HTTP URLs, produces mixed content warnings and can enter redirect loops between the secure and insecure base URL.
How should cron and queue consumers run in production?
As their own containers or scheduled tasks, with supervision and monitoring, never inside the web container. Cron drives indexing, emails, imports and cleanup, and a store where cron has quietly stopped looks fine for hours and then behaves inexplicably. Consumers should be restarted on each deployment, because they hold compiled code in memory and would otherwise keep running the previous release. Alert on both being alive, since neither failure produces a visible error on the storefront.
What should I monitor on a containerised Magento store?
Container health and restarts, memory and CPU per service, database connections and slow queries, search engine cluster status, queue depth, cron last run time, and application errors from Magento's own logs. On top of that, monitor the storefront from outside with uptime and real user performance data, because a container can be perfectly healthy while checkout is broken. Ship logs off the host, since container logs disappear with the container and the incident you need them for is the one that killed it.
How do I back up a containerised Magento store?
Back up the database, the media files and your configuration repository, then test a restore. The containers themselves need no backup because they are rebuildable from the image and the compose or manifest files, which is exactly the point. What people forget is media, which lives outside the database, and env.php, which holds configuration that may not exist anywhere else. A backup nobody has ever restored is a hypothesis, so schedule a real restore drill.
When is container orchestration worth the complexity?
When you need more than one application node, automated failover or repeatable scaling, and when someone on the team can operate it. For a single store on a single server, Docker Compose with a solid deployment script is honest, understandable and enough. Kubernetes adds real capability and real operational cost, and a poorly understood cluster is less reliable than a well understood server. Choose based on the traffic and the team you actually have, not on the architecture you would like to describe.






