logo
Docker containers and cloud infrastructure

DevOps

Magento 2 with Docker: Local Development Made Easy

Priya PatelPriya Patel·2026-03-14·7 min read

The Problem with Local Magento Stacks

Running Magento locally means managing PHP, MySQL, Elasticsearch, Redis, and Nginx — often across multiple projects with different version requirements. Docker solves this by isolating each project into its own container stack.

docker-compose.yml for Magento 2

services:
  nginx:
    image: nginx:1.25-alpine
    ports:
      - "80:80"
    volumes:
      - ./:/var/www/html
      - ./docker/nginx.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
 
  php:
    build: ./docker/php
    volumes:
      - ./:/var/www/html
    environment:
      PHP_MEMORY_LIMIT: 2G
 
  mysql:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: magento
    volumes:
      - mysql_data:/var/lib/mysql
 
  elasticsearch:
    image: elasticsearch:8.11.0
    environment:
      - discovery.type=single-node
      - xpack.security.enabled=false
    ulimits:
      memlock:
        soft: -1
        hard: -1
 
  redis:
    image: redis:7-alpine
 
volumes:
  mysql_data:

PHP Dockerfile for Magento

FROM php:8.3-fpm-alpine
 
RUN apk add --no-cache \
    libzip-dev icu-dev libxml2-dev libxslt-dev freetype-dev libjpeg-turbo-dev libpng-dev
 
RUN docker-php-ext-install \
    bcmath intl pdo_mysql soap xsl zip \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install gd
 
RUN pecl install redis && docker-php-ext-enable redis
 
COPY php.ini /usr/local/etc/php/conf.d/magento.ini

Installing Magento via Composer

# Inside the php container
docker compose exec php bash
 
composer create-project --repository-url=https://repo.magento.com/ \
  magento/project-community-edition=2.4.7 .
 
bin/magento setup:install \
  --base-url=http://localhost \
  --db-host=mysql \
  --db-name=magento \
  --db-user=root \
  --db-password=root \
  --search-engine=elasticsearch8 \
  --elasticsearch-host=elasticsearch \
  --admin-user=admin \
  --admin-password=Admin123! \
  --admin-email=admin@example.com

Why Use MagentoEnv Instead

Managing this Docker setup for every developer on your team means:

  • Everyone needs Docker installed and configured
  • First-time setup takes 30–60 minutes
  • Composer auth credentials must be shared
  • Different machines behave slightly differently

MagentoEnv gives your whole team an identical, cloud-hosted environment in 90 seconds — no Docker knowledge required. Use Docker locally for advanced workflows, and MagentoEnv for everything else.