Setting Up a Docker Stack for Magento 2

Setting Up a Docker Stack for Magento 2

This guide outlines the steps to set up a Docker stack for Magento 2 using the latest installer. The setup includes various services like Varnish, Nginx, PHP, OpenSearch, MariaDB, Redis, MailHog, RabbitMQ, and Watchtower. By adjusting the .env file, you can manage multiple projects within a single stack.

Prerequisites

  1. Docker and Docker Compose: Ensure you have Docker and Docker Compose installed on your machine.
  2. Git: Required for cloning the repository.

Getting Started

Clone the Repository

To get started, clone the Magento 2 Docker setup repository:

git clone https://git.hhf.technology/hhf/magento2.docker.git
cd magento2.docker

Check for Updates

To keep your local repository up to date, run:

git fetch && git pull

Configuration

Adjusting the .env File

Before installation, configure your environment variables in the .env file. This file allows you to set various parameters such as database credentials and project directories.

Installation Steps

Fresh Installation or Existing Project

You can either perform a fresh installation of Magento 2 or work with an existing project located on your filesystem.

  1. Run Installation Script

    Execute the installation script:

    chmod +x bin/*
    bin/install
    

    During this process, you will be prompted to configure your installation settings. Note that using the command with config has known bugs; contributions to fix these are welcome.

  2. Post-Installation Configuration

    After installation, modify the .env file to change any values as needed. These changes will take effect upon restarting the containers.

Accessing Your Magento Store

Starting and Managing Containers

For starting your containers after a system reboot, use:

bin/start

To install sample data, run:

bin/sample-data

Using PHP Container

To access the PHP container for command-line operations:

docker exec -it mage2_php bash

Mailhog Configuration

The installation script configures Magento to work with MailHog for email testing. Access MailHog at http://localhost:8025.

Additional Features

  • Multiple Projects: You can run multiple projects in parallel by configuring a reverse proxy like Traefik or nginx-proxy.
  • Extra Composer Packages: The setup includes additional packages like yireo/magento2-webp2 for WebP conversion and mage2tv/magento-cache-clean for cache cleaning.
  • Xdebug Support: Xdebug can be configured by setting xdebug.idekey=PHPSTORM in your .env file.

Conclusion

By following these steps, you can successfully set up a Docker stack for Magento 2 that is flexible and scalable for development purposes. This setup allows for easy management of multiple projects and ensures that your development environment is consistent and portable.