Creating a Magento2 Docker Environment: A Complete Guide

Set up a clean Magento 2 development environment with Docker, step by step. Skip the manual install maze and get a working stack faster. Skip the manual.

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

Set up a clean Magento 2 development environment with Docker, step by step. Skip the manual install maze and get a working stack faster. Skip the manual.

On this page

Magento2 is one of the most powerful eCommerce platforms, but setting up its development environment can feel like navigating a maze. Manually installing Apache/Nginx, MySQL, PHP, Redis, and Elasticsearch is time-consuming and often leads to compatibility issues. That’s why at Raulji Technologies, we recommend using Docker to create a Magento2 environment.

As Yuvraj Raulji, our Magento2 expert, explains: “Docker simplifies Magento2 setup by providing isolated, pre-configured containers for each service, ensuring consistency and faster deployment.” In this guide, we’ll walk you through setting up a Magento2 Docker environment using Docker Compose in just a few steps.

Why Use Docker for Magento2?

  • Faster Setup: No need to install services manually – Docker does it for you.
  • Environment Consistency: Your setup works the same on every machine, eliminating the “it works on my machine” problem.
  • Modular Services: Separate containers for web, database, caching, and search make it easy to manage and scale.
  • Easier Scalability: Add or remove services like Redis or Elasticsearch with a single command.

Step 1: Install Docker & Docker Compose

Before creating a Magento2 environment, make sure Docker and Docker Compose are installed.

For Windows & macOS
  1. Download Docker Desktop from the https://www.docker.com
  2. Run the installer and follow the setup instructions.
  3. For Windows users, enable the WSL 2 Backend for better performance.
  4. Verify the installation by opening a terminal and running:
    docker –version
    docker-compose –version
For Linux (Ubuntu/Debian-based systems)
  1. Open your terminal and run the following commands:
    sudo apt update
    sudo apt install -y docker.io docker-compose
    sudo systemctl start docker
    sudo systemctl enable docker
  2. Verify the installation:
    docker –version
    docker-compose –version

Step 2: Create a Magento2 Project Directory

Create a directory for your Magento2 project. This folder will contain the Magento source code and the docker-compose.yml file.

mkdir magento-docker && cd magento-docker

Step 3: Set Up docker-compose.yml

Create a docker-compose.yml file inside the magento-docker directory and add the following configuration

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 4: Download Magento 2 Source Code

Clone the Magento2 repository into the app directory

mkdir app && cd app
git clone https://github.com/magento/magento2.git .

Step 5: Start Magento 2 Containers

Run the following command inside the magento-docker directory to start the containers

docker-compose up -d
This command will:
  • Pull and create the required containers.
  • Start the Magento application, MySQL, Redis, and Elasticsearch.
  • Mount the Magento 2 code inside the container.

Step 6: Install Magento 2 Inside the Container

Once the containers are running, execute the Magento installation command

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 install Magento2 and set up the database.

Step 7: Access Magento2 in Your Browser

Once the installation is complete, open your browser and visit

Storefront: http://localhost/
Admin Panel: http://localhost/admin

Step 8: Manage Magento2 via CLI

To run Magento commands inside the container, use

docker-compose up -d
To check logs
docker logs magento_app
To stop containers
docker-compose down

Final Thoughts

By using Docker, you can set up a Magento2 development environment quickly and efficiently. This setup ensures that all dependencies run smoothly in isolated containers, reducing compatibility issues.

At Raulji Technologies, we optimize Magento2 development using Docker for better performance and faster deployments. Yuvraj Raulji highly recommends this approach to streamline your workflow.

What’s Next?

Stay tuned for more Magento2 Docker tutorials and ready to supercharge your Magento2 development with Docker? Let Raulji Technologies help you build a faster, more efficient eCommerce store. Contact us today!

Frequently asked

Frequently Asked Questions

Answers to the questions we hear most often.

Which Magento version should this environment target today?

Pick the version before you write a line of the compose file, because it decides every image tag. Magento Open Source 2.4.9 reached general availability on 12 May 2026 and Adobe's supported stack for it is 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. Building an environment that mixes versions from two different matrices produces failures that look like Magento bugs but are not.

How should I structure the project folder?

Keep the environment beside the application, not inside it. A common layout is a docker folder holding the Dockerfiles and service configuration, a compose file at the repository root, an env file for values that differ per developer, and the Magento source in its own directory that gets bind mounted. That separation means you can update nginx configuration or a PHP extension in a reviewable commit without touching store code, and a new developer can read the whole environment in one place before running anything.

What belongs in the .env file versus the compose file?

The compose file describes the shape of the environment and is identical for everyone. The env file holds the values that legitimately vary or must not be committed: exposed port numbers, the project name, database credentials for local use, and Composer authentication keys. Commit an example env file with safe placeholder values so newcomers know what to fill in, and add the real one to gitignore. Anything secret that reaches production belongs in a proper secret store, not in either file.

Why pin image versions instead of using latest?

Because latest is a moving target. An image tagged latest can change under you between two builds, so a colleague who joins the project next month gets a different database or PHP release than you are running, and Magento's supported matrix is strict about both. Pinning to explicit versions makes the environment reproducible and makes upgrades a deliberate, reviewable change. It also makes a bisect possible when something breaks, because you can point at the commit where the version moved.

Should the Magento source live in a volume or a bind mount?

Bind mount the source so your editor changes appear immediately and your version control runs on the host. Use named volumes for the things containers own: database files, search indexes and message broker state. The only nuance is performance on Windows and macOS, where bind mounted source crosses a virtualisation boundary. Keep the project inside the WSL2 filesystem on Windows, and use VirtioFS or a synced volume mode on macOS, and the difference becomes small enough to ignore.

How do I install Magento into a freshly built environment?

Start the containers, then run Composer inside the PHP container to create the project from repo.magento.com using your Adobe authentication keys, then run bin/magento setup:install pointing the database host at the service name and the search host at the OpenSearch service. Follow with dependency injection compilation, static content deployment and a reindex. Doing all of this from inside the container matters, because the host has none of the required PHP extensions and will produce misleading errors.

Why does the install fail with a search engine connection error?

Because Magento validates the search connection during installation and refuses to continue without it. Either the OpenSearch container is not running, or you pointed Magento at localhost instead of the service name, or the container started and then died from insufficient memory. Check the search container's own logs first. Magento only reports that it could not connect, which tells you nothing about why, and people lose hours editing Magento configuration when the real problem is that the service exited seconds after boot.

How do I get the same environment onto a colleague's machine?

They clone the repository, copy the example env file, and run one setup script. That script should build the images, start the stack, install dependencies and either install Magento or import a shared database dump. If any step needs a wiki page to explain it, put it in the script instead. The measure of a good containerised environment is that a new developer reaches a working storefront on their first morning without asking anyone a question.

Should I commit a database dump for the team?

Commit a sanitised one, or store it where the team can fetch it, but never a raw production dump. Production databases contain customer names, addresses, order history and hashed credentials, and copying them onto laptops turns every developer machine into a data protection liability. Magento ships a sanitisation module for exactly this reason, and there are community tools that strip customer and order data during export. Sanitise once, share the safe artefact, and refresh it on a schedule.

How do I run cron and queue consumers locally?

Give them their own container running the same PHP image as the application, with a command that runs Magento's cron or a specific consumer in the foreground. Do not add cron to the web container, because a container should run one process and you want to be able to stop cron independently while debugging. Many developers leave cron off by default and start it only when testing indexing, order emails or scheduled imports, since it otherwise produces log noise on every save.

How do I completely reset the environment?

Stop the stack and remove its volumes, which destroys the database, the search indexes and any queued messages, then run the setup script again. Because everything is scripted, a wrecked environment is a rebuild rather than an investigation. Be aware of what you lose: local admin users, test orders and any configuration you set through the admin panel but never wrote down. Anything you want to survive a reset should be in the repository as a setup script or a data patch.

Can one machine run several Magento projects at once?

Yes, and it is one of the strongest arguments for this approach. Give each project a distinct compose project name so container names and networks do not collide, and map each to different host ports or use a shared reverse proxy that routes by hostname. Memory is the real limit, since each stack runs its own database and search engine. Stop the projects you are not actively working on and a normal development laptop handles two or three comfortably.

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