Setting up Magento2 manually can feel like solving a Rubik’s Cube—every dependency (PHP, MySQL, Elasticsearch, Redis, Nginx/Apache) needs to align perfectly. Enter Docker Compose, a tool that simplifies this process by managing all services with a single configuration file.
At Raulji Technologies, we use Docker Compose to create efficient Magento2 development environments. As Yuvraj Raulji, our Magento2 expert, explains: “Docker Compose allows Magento2 developers to launch a fully functional environment with just one command, saving time and effort.” In this guide, we’ll walk you through running Magento2 with Docker Compose from scratch.
Why Use Docker Compose for Magento2?
- Quick Setup: No need to install services manually—Docker Compose does it for you.
- Consistency: Your setup works identically across all environments, eliminating the “it works on my machine” problem.
- Modularity: Easily add or remove services like Redis or Elasticsearch.
- Easier Management: Start, stop, and rebuild services with simple commands.
Step 2: Set Up a Magento2 Project with Docker Compose
Create a new directory for your Magento2 project and navigate into it:
Copy
mkdir magento-docker && cd magento-docker
Step 3: Define Services in docker-compose.yml
Create a docker-compose.yml file inside the magento-docker directory and add the following configuration
Copy
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”
What This File Does
- app: Runs Magento2 using an official Magento Docker image.
- db: Sets up a MySQL 5.7 database for Magento.
- elasticsearch: Required for Magento 2.4+ search functionality.
- redis: Improves caching and performance.
Step 4: Clone Magento2 Source Code
Run the following command inside the magento-docker directory to download the Magento2 source code:
Copy
mkdir app && cd app
git clone https://github.com/magento/magento2.git .
Step 5: Start Magento2 with Docker Compose
Run the following command in the magento-docker directory to start the containers
Copy
docker-compose up -d
This will:
- Download the necessary Docker images.
- Start Magento2 and its required services.
- Mount the Magento2 source code inside the container.
Step 6: Install Magento2 in the Container
After starting the containers, install Magento2 by running:
Copy
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
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 with Docker Compose
Restart Containers:
Copy
docker-compose restart
Stop Containers:
Copy
docker-compose down
Run Magento CLI Commands Inside the Container:
Copy
docker exec -it magento_app bin/magento cache:flush
docker exec -it magento_app bin/magento indexer:reindex
View Logs:
Copy
docker logs magento_app
Step 9: Configure Additional Services (Optional)
Adding Varnish for Full-Page Caching
Add the following to your docker-compose.yml file:
Copy
varnish:
image: varnish:6.5
container_name: magento_varnish
depends_on:
– app
ports:
– “6081:6081”
Varnish speeds up page loading times by caching responses.
Adding Mailhog for Email Testing
Add the following to your docker-compose.yml file:
Copy
mailhog:
image: mailhog/mailhog
container_name: magento_mailhog
ports:
– “1025:1025”
– “8025:8025”
Mailhog catches outgoing emails so you can test them in a browser.
Final Thoughts
Using Docker Compose to run Magento2 is the fastest and most reliable way to set up a local development environment. This method ensures that your Magento store runs smoothly, with all necessary services in isolated containers.
At Raulji Technologies, we rely on Docker for efficient Magento development. Yuvraj Raulji highly recommends this approach to developers looking for an easy-to-manage environment.
What’s Next?
Stay tuned for our upcoming tutorials on:
- Optimizing Magento2 Performance with Docker: Fine-tune your setup for maximum speed.
- Deploying Magento2 with Docker: Learn how to take your Dockerized Magento store to production.
- Advanced Docker Compose Configurations: Explore custom images, multi-container orchestration, and more.
Ready to supercharge your Magento2 development with Docker? Let Raulji Technologies help you build a faster, more efficient eCommerce store. Contact us today! 🚀