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!

Yuvraj Raulji

Yuvraj Raulji

Founder & CEO

February 6, 2025
7 min read

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.

Ready to Transform Your Business?

Let's discuss how we can help you build scalable, AI-driven digital solutions that drive growth and success.