Magento 4 min read Feb 7, 2025

Running Magento2 with Docker Compose: A Step-by-Step Guide

Yuvraj Raulji
By Yuvraj Raulji
Raulji Technologies

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:

bash
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

yaml
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:

bash
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

bash
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:

bash
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:

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

Step 8: Manage Magento2 with Docker Compose

Restart Containers:
bash
Copy
docker-compose restart
Stop Containers:
bash
Copy
docker-compose down
Run Magento CLI Commands Inside the Container:
bash
Copy
docker exec -it magento_app bin/magento cache:flush
docker exec -it magento_app bin/magento indexer:reindex
View Logs:
bash
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:

yaml
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:

yaml
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! 🚀

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.

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 4.9

Clutch Verified Profile

Recognized on Clutch for excellence in Magento, Shopify, and AI-driven digital transformation.

View Clutch Profile
Google 4.9

Google Verified Profile

Reviewed by clients on Google across India, the Gulf, and worldwide for delivery and support.

Read Google Reviews
G2 4.9

G2 Verified Profile

Recognized on G2 for delivering reliable, scalable eCommerce and AI-powered solutions.

Explore G2 Profile