Bookmark

Setting Up Magento 2 Development Environment With Docker & Warden on Windows

Setting Up Magento 2 Development Environment With Docker & Warden on Windows

Introduction

Magento 2 is a powerful e-commerce platform that benefits greatly from a well-configured development environment. Using Docker and Warden can streamline the setup process by containerizing your environment and managing it efficiently. This guide will walk you through setting up a Magento 2 development environment on Windows using Docker and Warden, including the installation of WSL2, configuring host entries, and setting up Magento 2.

Prerequisites

1. Windows 10/11 Pro or Enterprise: Docker requires Hyper-V and WSL 2, which are available in these editions.
2. Docker Desktop: Manage containers with Docker Desktop. Download Docker Desktop for Windows.
3. Warden: A tool for managing local Magento development environments. Install it from Warden's official site.
4. Magento 2: Have a Magento 2 project or be prepared to create one.

Step 1: Install Docker Desktop

1. Download Docker Desktop:
Go to the Docker Desktop for Windows page and download the installer.
2. Run the Installer:
Follow the installation prompts and enable Hyper-V and WSL 2 if prompted.
3. Configure Docker:
Open Docker Desktop and navigate to Settings > General. Ensure “Use the WSL 2 based engine” is checked.

Step 2: Install WSL2

1. Install WSL2 in Windows:
Open PowerShell as Administrator and run:

wsl --install

This installs WSL and the default Linux distribution. For specific distributions or versions, download them from the Windows Store or manually from the distribution’s website.
2. Install a Linux Distribution:
After installing WSL2, install Ubuntu 20.04 or another compatible Linux distribution. Search for "Ubuntu 20.04" in the Windows Store and install it.
3. Configure Docker for Windows:
Open Docker Desktop for Windows.
Go to Settings > Resources > WSL Integration.
Enable integration for the installed Linux distributions.

Step 3: Install Warden

1. Install Homebrew (Package Manager for Linux):
In the WSL terminal, run:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

2. Install Warden:
Once Homebrew is installed, use it to install Warden:
brew install wardenenv/warden/warden

3. Start Warden Services:
Run the following to start Warden services:
warden svc up

Step 4: Add Hosts Entries

To interact with the various Warden services through their UIs, add entries to the hosts file:
1. Open Hosts File:
Open Notepad or your preferred text editor as an Administrator.
Navigate to C:\Windows\System32\drivers\etc\hosts.
2. Add Entries:
Add the following lines to the hosts file:

127.0.0.1 traefik.warden.test
127.0.0.1 portainer.warden.test
127.0.0.1 dnsmasq.warden.test
127.0.0.1 mailhog.warden.test

3. Save the File:
Save and close the hosts file. This will allow you to access the Warden services via the provided URLs.
4. Access Warden Services:
After running
warden svc up
, interact with the following services:
- Traefik
- Portainer
- DNSMasq
- MailHog

Step 5: Install Magento 2

The following example demonstrates setting up Magento 2 from scratch for local development. This assumes that Warden has been started with

warden svc up
as part of the installation procedure.
1. Create a Project Directory: Create a new directory for the project and navigate into it:
mkdir -p ~/Sites/ecomproject
cd ~/Sites/ecomproject

2. Initialize Warden Environment: From the root of your new project directory, run:
warden env-init ecomproject magento2

This creates a .env file in the project root with configuration needed for Warden and Docker.
3. Review `.env` File: The .env file will contain configurations like:
WARDEN_ENV_NAME=ecomproject
WARDEN_ENV_TYPE=magento2
WARDEN_WEB_ROOT=/

TRAEFIK_DOMAIN=ecomproject.test
TRAEFIK_SUBDOMAIN=app

WARDEN_DB=1
WARDEN_ELASTICSEARCH=0
WARDEN_OPENSEARCH=1
WARDEN_ELASTICHQ=0
WARDEN_VARNISH=1
WARDEN_RABBITMQ=1
WARDEN_REDIS=1

OPENSEARCH_VERSION=2.12
MYSQL_DISTRIBUTION=mariadb
MYSQL_DISTRIBUTION_VERSION=10.6
NODE_VERSION=20
COMPOSER_VERSION=2
PHP_VERSION=8.3
PHP_XDEBUG_3=1
RABBITMQ_VERSION=3.13
REDIS_VERSION=7.2
VARNISH_VERSION=7.5

WARDEN_SYNC_IGNORE=

WARDEN_ALLURE=0
WARDEN_SELENIUM=0
WARDEN_SELENIUM_DEBUG=0
WARDEN_BLACKFIRE=0
WARDEN_SPLIT_SALES=0
WARDEN_SPLIT_CHECKOUT=0
WARDEN_TEST_DB=0
WARDEN_MAGEPACK=0

MAGEPACK_VERSION=2.11

BLACKFIRE_CLIENT_ID=
BLACKFIRE_CLIENT_TOKEN=
BLACKFIRE_SERVER_ID=
BLACKFIRE_SERVER_TOKEN=

4. Sign SSL Certificate:
Sign an SSL certificate for your project:
warden sign-certificate ecomproject.test

5. Start Project Environment:
Start the environment with:
warden env up

Warning: If you encounter a “Mounts denied” error, follow the instructions in the error message and try running
warden env up
again.
6. Enter the Project Shell:
Drop into a shell within the project environment:
warden shell

7. Configure Global Magento Marketplace Credentials:
Set up Magento Marketplace credentials:
composer global config http-basic.repo.magento.com <username> <password>

Use the Public key as your username and the Private key as your password. Refer to Magento DevDocs for more details.
8. Initialize Project Source Files:
Use Composer to create the project files:
META_PACKAGE=magento/project-community-edition META_VERSION=2.4.x
composer create-project --repository-url=https://repo.magento.com/ \
"${META_PACKAGE}" /tmp/ecomproject "${META_VERSION}"
rsync -a /tmp/ecomproject/ /var/www/html/
rm -rf /tmp/ecomproject/

9. Install and Configure Magento:
Install the application:
bin/magento setup:install \
--backend-frontname=backend \
--amqp-host=rabbitmq \
--amqp-port=5672 \
--amqp-user=guest \
--amqp-password=guest \
--db-host=db \
--db-name=magento \
--db-user=magento \
--db-password=magento \
--search-engine=opensearch \
--opensearch-host=opensearch \
--opensearch-port=9200 \
--opensearch-index-prefix=magento2 \
--opensearch-enable-auth=0 \
--opensearch-timeout=15 \
--http-cache-hosts=varnish:80

10. Generate Admin User:
Create an admin user:
ADMIN_PASS="$(pwgen -n1 16)"
ADMIN_USER=localadmin
bin/magento admin:user:create \
--admin-password="${ADMIN_PASS}" \
--admin-user="${ADMIN_USER}" \
--admin-firstname="Local" \
--admin-lastname="Admin" \
--admin-email="${ADMIN_USER}@example.com"
printf "u: %s\np: %s\n" "${ADMIN_USER}" "${ADMIN_PASS}"

Step 6: Launch the Application

Open the following URLs in your browser to access Magento and other services:
- Magento Storefront: https://ecomproject.test/
- Magento Admin: https://ecomproject.test/backend
- RabbitMQ: https://rabbitmq.warden.test/
- Elasticsearch: https://elasticsearch.warden.test/

Conclusion

By following these steps, you will have a fully configured Magento 2 development environment running on Windows using Docker and Warden. This setup ensures a consistent development environment, allowing you to efficiently build and customize your Magento store.

Post a Comment

Post a Comment