Follow these instructions to set up Magento 2 in a Docker development environment using Apache and PHP 8.1.
-
Clone the Repository:
git clone git@github.com:whilesmart/magento-docker.git cd magento-docker mkdir code # Required for Magento2 code files
-
Place your Magento 2 project in the
codedirectory:- Copy or clone your Magento 2 project into the
codedirectory. Thecodedirectory will serve as the root of your Magento 2 installation. - You can use git tracking in the
codedirectory for your actual Magento project if you like, since a.gitignorefile is included with the linecode/in it.
- Copy or clone your Magento 2 project into the
-
Create
info.phpfor testing:-
Create an
info.phpfile in thecode/pubdirectory with the following content:<?php phpinfo(); ?>
-
Access this file in your browser at
http://localhost:8000/info.phpto verify your PHP settings and configuration.
-
-
Build and start the Docker containers:
docker compose up --build
-
Build and start the Docker containers:
docker compose up exec web composer install -
Set correct permissions:
docker compose exec web find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} + docker compose exec web find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} + docker compose exec web chown -R :www-data . # Ubuntu docker compose exec web chmod u+x bin/magento -
Run Magento setup:
After the containers are up and running, execute the Magento setup within the web container:
docker compose exec web bin/magento setup:install \ --base-url=http://localhost:8000 \ --db-host=db \ --db-name=magento \ --db-user=magento \ --db-password=magento \ --admin-firstname=admin \ --admin-lastname=admin \ --admin-email=admin@admin.com \ --admin-user=admin \ --admin-password=admin123 \ --language=en_US \ --currency=USD \ --timezone=America/Chicago \ --use-rewrites=1 \ --search-engine=elasticsearch7 \ --elasticsearch-host=elasticsearch \ --elasticsearch-port=9200 -
Access Magento:
- Frontend: http://localhost:8000
Your project directory should have the following structure:
project-root/
├── Dockerfile
├── docker-compose.yml
├── 000-default.conf
├── .gitignore
└── code/
└── pub/
└── info.php
The provided Docker setup includes:
- Apache: Configured with mod_php to serve PHP files.
- PHP 8.1: Installed with necessary extensions for running Magento 2.
- MySQL (MariaDB): As the database server.
- Elasticsearch: For the Magento search engine.
project-root/
├── Dockerfile
├── docker-compose.yml
├── 000-default.conf
├── .gitignore
└── code/
├── app/
├── bin/
├── lib/
├── pub/
│ └── info.php
├── var/
└── vendor/
This setup utilizes the install-php-extensions tool from mlocati/docker-php-extension-installer.
This project is licensed under the MIT License.