Magento2 Development in Docker: The Ultimate Guide

Docker makes Magento 2 development isolated and repeatable. This guide walks through the full workflow so your team ships faster. See how it works.

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

Docker makes Magento 2 development isolated and repeatable. This guide walks through the full workflow so your team ships faster. See how it works.

On this page

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 Raulji Technologies, 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
  1. Download Docker Desktop from https://www.docker.com
  2. Follow the installation instructions.
  3. Enable WSL 2 Backend (for Windows users).
  4. Verify installation:
    sh
    CopyEdit
    docker –version
    docker-compose –version
For Linux (Ubuntu/Debian-based systems)
sh
CopyEdit
sudo apt update
sudo apt install -y docker.io docker-compose
sudo systemctl start docker
sudo systemctl enable docker

Verify installation:

sh
CopyEdit
docker –version
docker-compose –version

Step 2: Set Up a Magento2 Development Environment

2.1 Create a Project Directory
sh
CopyEdit
mkdir magento-docker && cd magento-docker
2.2 Clone Magento 2 Source Code
sh
CopyEdit
mkdir app && cd app
git clone https://github.com/magento/magento2.git .
2.3 Create a docker-compose.yml File
Inside magento-docker, create docker-compose.yml:
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:

sh
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:

sh
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 \
–admin-email=admin@example.com \
–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
sh
CopyEdit
docker-compose restart
5.2 Stop Containers
sh
CopyEdit
docker-compose down
5.3 Run Magento CLI Commands Inside the Container
sh
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
sh
CopyEdit
docker logs magento_app

Step 6: Customizing Docker for Magento2 Development

6.1 Use a Custom php.ini File
Create php.ini inside custom-config/php/:
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
yaml
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
Modify docker-compose.yml:
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
  1. Configure PHP Remote Debugging in PHPStorm.
  2. Use Server Name: localhost and set Path Mappings to /var/www/html.
  3. Enable Breakpoints and start listening for PHP Debug Connections.

Final Thoughts

Using Docker for Magento2 development improves efficiency, ensures consistency, and simplifies debugging. At Raulji Technologies, 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!

Frequently asked

Frequently Asked Questions

Answers to the questions we hear most often.

Which Magento and PHP versions should a new development environment use?

Match Adobe's supported matrix for the release you are building against. Magento Open Source 2.4.9 reached general availability on 12 May 2026 and is supported on 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. The 2.4.8 line still covers PHP 8.3 and 8.4 with Elasticsearch 8. Developing on a PHP version your production host does not run is a reliable way to ship code that passes locally and fails on deployment.

Should I develop in developer mode or production mode?

Developer mode, almost always. It disables static content caching, shows real exceptions instead of a generic error page with a report ID, and regenerates compiled code as you work. Production mode exists to be fast, not to be diagnosable, and debugging in it means every change requires a compile and deploy cycle. The exception is when you are specifically testing performance or reproducing a bug that only appears in production mode, in which case switch deliberately and switch back.

How do I set up Xdebug so breakpoints actually stop?

Install the extension in the PHP image, set it to step debug mode, and point the client host back at your machine. On Docker Desktop that is host.docker.internal, and on Linux you add a host gateway entry. Set the IDE key your editor listens for and map the container path to your local path in the IDE, which is the step people miss and the reason breakpoints show as unverified. Keep Xdebug off by default and enable it per request, since always-on Xdebug slows Magento considerably.

How do I debug a CLI command or a queue consumer?

The same debugger works, but you have to trigger it explicitly for the CLI process rather than relying on a browser cookie. Set the Xdebug trigger environment variable when you run the command inside the container, and start listening in your IDE first. This is how you debug indexers, data patches, cron jobs and consumers, which is where the genuinely difficult Magento bugs live. Debugging a consumer also means running it in the foreground rather than under the supervisor that normally restarts it.

Why do my code changes not appear in the browser?

Work through the layers in order. Magento's own caches, then generated code in the generated folder, then static content in pub/static, then OPcache holding a stale compiled file, then Varnish if it is running. In developer mode most of those are handled for you, which is why developer mode exists. If a change genuinely will not appear after flushing caches, check that you edited the file inside the bind mounted path the container sees, not a copy somewhere else on your machine.

Do I need to run cron during development?

Only when the thing you are testing depends on it, which is more often than people expect. Indexer updates on schedule, order confirmation emails, scheduled imports, admin notifications and several third party integrations only happen when cron runs. Give cron its own container so it can be started and stopped independently, and leave it off while doing template work to keep the logs quiet. Then turn it on before you conclude a feature works, because plenty of bugs only appear on the cron path.

How should queue consumers run in a development environment?

In a dedicated container running in the foreground, one process per consumer group you care about, rather than relying on cron to spawn them. That makes their output visible and lets you restart a single consumer after a code change, which matters because consumers hold compiled code in memory and will keep running your old logic until restarted. Forgetting to restart a consumer after editing its class is one of the most common and most confusing wasted afternoons in Magento development.

How do I run Composer without breaking file permissions?

Run it inside the PHP container, as the same user that owns the files, never as root. Composer writes into vendor, generated and var, and a root-owned file there produces a permission error later that looks completely unrelated to the command that caused it. Building the PHP image with a user whose numeric ID matches your host account solves it permanently on Linux. Keep auth.json mounted and readable by that user, otherwise Composer fails against repo.magento.com with an unhelpful authentication error.

Can I run PHPUnit and static analysis inside the container?

Yes, and you should, because they need the same PHP version and extensions the application uses. Unit tests run against the code alone, while integration tests need a separate test database and a working search connection, which is exactly the kind of setup containers make repeatable. Running static analysis and code sniffing in the container too means the version everyone runs locally matches the version CI runs, so nobody has a pull request rejected by a tool that passed on their machine.

How do I work on several Magento projects at once?

Give each project its own compose project name and its own stack, so a client on an older Magento release with Elasticsearch and PHP 8.2 coexists with one on 2.4.9 with OpenSearch 3 and PHP 8.5. Nothing is installed on your host, so there is no version to switch. Stop the stacks you are not using, because each runs a database and a search engine and memory is the real constraint. A shared reverse proxy routing by hostname avoids juggling port numbers.

How do I bring production data in safely?

Sanitise it before it leaves production. A raw dump contains customer names, addresses, order history and hashed credentials, and copying it to laptops turns every developer machine into a data protection exposure. Magento ships a sanitisation module, and community tools can strip customer and sales data during export. Import the sanitised dump into your database container, reindex, and switch the base URLs to your local domain. Keep a refreshed sanitised dump somewhere the team can fetch rather than passing files around.

Why is Docker slow on my Windows or Mac machine?

Almost always file sharing rather than PHP. Magento reads a very large number of small files per request, and every one of those reads crosses a virtualisation boundary when your source lives on a shared host folder. On Windows, move the project inside the WSL2 filesystem instead of under the C drive, which alone can transform performance. On macOS, use VirtioFS file sharing or a synced volume mode offered by setups such as docker-magento. Databases and search indexes should live in named volumes, never on a bind mount.

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