Creating a Magento2 Docker Environment: A Complete Guide

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 Ariya InfoTech, 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
- Download Docker Desktop from the https://www.docker.com
- Run the installer and follow the setup instructions.
- For Windows users, enable the WSL 2 Backend for better performance.
- Verify the installation by opening a terminal and running:
docker –version
docker-compose –version
For Linux (Ubuntu/Debian-based systems)
- 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 - 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.
Step 3: Set Up docker-compose.yml
Create a docker-compose.yml file inside the magento-docker directory and add the following configuration
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
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
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
–base-url=http://localhost/ \
–db-host=db \
–db-name=magento \
–db-user=magento \
–db-password=magento \
–admin-firstname=Admin \
–admin-lastname=User \
–[email protected] \
–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
Admin Panel: http://localhost/admin
Step 8: Manage Magento2 via CLI
To run Magento commands inside the container, use
To check logs
To stop containers
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 Ariya InfoTech, 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 Ariya InfoTech help you build a faster, more efficient eCommerce store. Contact us today!