Installing Docker for Magento2: A Step-by-Step Guide

Magento2 is a powerhouse for eCommerce, but setting up its development environment can feel like solving a puzzle. You need to install and configure multiple services like Apache/Nginx, MySQL, PHP, Elasticsearch, and Redis—each with its own quirks. This manual process is time-consuming and prone to errors.
Enter Docker, the ultimate solution for simplifying Magento2 development. Docker allows you to create an isolated, pre-configured environment in minutes, so you can focus on building your store instead of wrestling with configurations. In this guide, we’ll walk you through installing Docker for Magento2, step by step.
Why Use Docker for Magento2?
At Ariya InfoTech, we swear by Docker for Magento2 development. As Yuvraj Raulji, our Magento2 expert, puts it: “Docker eliminates the frustration of manual setups and ensures consistency across all environments.” Here’s why Docker is a game-changer
- Faster Setup: Get a Magento2 store running in minutes, not hours.
- Environment Consistency: Avoid the dreaded “it works on my machine” problem.
- Easy Scaling: Add or remove services like Redis or Elasticsearch with a single command.
- Less Configuration: Prebuilt Docker images save time and reduce errors.
Step 1: Install Docker & Docker Compose
Before we dive into Magento2, let’s install Docker and Docker Compose on your system. Docker Compose is a tool that helps you manage multiple containers (like PHP, MySQL, and Nginx) as a single service.
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 - You should see the installed versions of Docker and Docker Compose.
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: Set Up Magento2 with Docker
Now that Docker is installed, let’s set up a basic Magento2 environment using Docker Compose.
-
Create a New Directory for Magento2
Open your terminal and run:
mkdir magento-docker && cd magento-docker -
Create a docker-compose.yml File
This file defines all the services your Magento2 store needs. Create a file named docker-compose.yml and add the following content:
version: ‘3.7’
services:
app:
image: magento/magento2
ports:
– “80:80”
environment:
MYSQL_HOST: db
MYSQL_USER: magento
MYSQL_PASSWORD: magento
MYSQL_DATABASE: magento
volumes:
– ./app:/var/www/html
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: magento
MYSQL_USER: magento
MYSQL_PASSWORD: magento -
Start the Docker Containers
Run the following command to start the containers:
docker-compose up -d - This will download the necessary Docker images and start the Magento 2 and MySQL containers.
Step 3: Access Magento 2 in Your Browser
Once the setup is complete, open your browser and go to:
You should see your Magento2 store up and running!
What’s Happening Under the Hood?
-
Magento2 Container:
This container runs the Magento 2 application using PHP and Nginx.
-
MySQL Container:
This container handles the database for your Magento 2 store.
-
Volumes:
The ./app:/var/www/html line ensures that your Magento 2 files are stored on your local machine, so they persist even if the container is restarted.
-
Environment Variables:
These are used to configure database credentials and other settings.
Advanced Tips for Docker and Magento2
- Add More Services: Need Redis for caching or Elasticsearch for advanced search? Just add them to your docker-compose.yml file. For example
redis:
image: redis:latest
elasticsearch:
image: elasticsearch:7.9.3 - Use Custom Docker Images: If you need specific PHP extensions or configurations, create a custom Docker image. Here’s an example Dockerfile:
FROM php:7.4-fpm
RUN docker-php-ext-install pdo_mysql - Optimize Performance: Use Docker’s resource limits to allocate CPU and memory to your containers. For example
app:
image: magento/magento2
deploy:
resources:
limits:
cpus: ‘2’
memory: 4G - Backup Your Data: Use Docker volumes to back up your Magento2 database and files. For example:
docker-compose exec db mysqldump -u magento -pmagento magento > backup.sql
Conclusion
Congratulations! You’ve successfully installed Docker and set up Magento2 in a containerized environment. This setup ensures that your Magento2 development is faster, more reliable, and free from configuration issues.
At Ariya InfoTech, we use Docker to enhance our Magento2 development process, and we highly recommend it to every Magento developer. Stay tuned for more guides from Yuvraj Raulji on optimizing and deploying Magento2 with Docker.