Installing Magento on Windows with Docker

Author Lee Timmins Date 27th Jul 2022 Comments Comments

The following article explains two approaches to installing Magento on Windows with Docker. The first method is simpler to understand but runs extremely slowly, therefore it is recommended to use method 2.

For both methods you should first make sure you have Docker Desktop (with WSL 2) installed. Also you should setup your local domain by adding "127.0.0.1 magento.test" to your hosts file.

You will be prompted for your access keys. These can be created by doing the following:

  1. First go to https://marketplace.magento.com and sign up or login.
  2. Click on "My Profile" from the drop down in the top right.
  3. Under My Products click "Access Keys".

Method 1 (Extremely Poor Performance)

  1. Download or clone the Magento repository and run "composer install". Alternatively you can execute the following command which will do this for you:
    composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition <directory-name>
    
  2. Create a file called "docker-compose-yml" in the root of the project with the following content (you may need to stop local web/mysql servers running on the same ports):
    version: '3'
    services:
       web:
           image: webdevops/php-apache-dev:7.4
           container_name: web
           restart: always
           environment:
             - WEB_ALIAS_DOMAIN=magento.test
             - WEB_DOCUMENT_ROOT=/app/pub
             - PHP_DATE_TIMEZONE=EST
             - PHP_DISPLAY_ERRORS=1
             - PHP_MEMORY_LIMIT=2048M
             - PHP_MAX_EXECUTION_TIME=300
             - PHP_POST_MAX_SIZE=500M
             - PHP_UPLOAD_MAX_FILESIZE=1024M
           volumes:
             - .:/app:cached
           ports:
             - "80:80"
             - "443:443"
           links:
             - mysql
       mysql:
           image: mariadb:10.2
           container_name: mysql
           restart: always
           ports:
             - "3306:3306"
           environment:
             - MYSQL_ROOT_PASSWORD=root
             - MYSQL_DATABASE=magento
           volumes:
             - db-data:/var/lib/mysql
       phpmyadmin:
           container_name: phpmyadmin
           restart: always
           image: phpmyadmin/phpmyadmin:latest
           environment:
             - MYSQL_ROOT_PASSWORD=root
             - PMA_USER=root
             - PMA_PASSWORD=root
           ports:
             - "8080:80"
           links:
             - mysql:db
           depends_on:
             - mysql
       elasticsearch:
           image: docker.elastic.co/elasticsearch/elasticsearch:7.9.1
           environment:
             - discovery.type=single-node
           ports:
             - 9200:9200
           restart: always
     
    volumes:
       db-data:
           external: false
  3. Now open up the terminal at the project root and execute the following:
    docker-compose up
  4. You should now be able to access phpMyAdmin by going to http://localhost:8080. Before trying to access Magento we first need to install it.
  5. Access your command line that contains your Docker web container (note that closing the existing terminal will require you to restart the container in Docker Desktop):
    docker exec -it web bash
  6. Now access the web document root:
    cd app
  7. Now execute the following:
    php bin/magento setup:install \
    --admin-firstname=John \
    --admin-lastname=Doe \
    --admin-email=johndoe@example.com \
    --admin-user=admin \
    --admin-password='password123' \
    --base-url=http://magento.test \
    --base-url-secure=https://magento.test \
    --backend-frontname=admin \
    --db-host=mysql \
    --db-name=magento \
    --db-user=root \
    --db-password=root \
    --use-rewrites=1 \
    --language=en_US \
    --currency=USD \
    --timezone=America/New_York \
    --use-secure-admin=0 \
    --admin-use-security-key=1 \
    --session-save=files \
    --elasticsearch-host=elasticsearch
  8. Now execute the following to setup the permissions:
    chmod -R 777 *
  9. Finally navigate to http://magento.test to see your application, once it eventually loads.

Method 2

  1. Setup a local directory to install Magento.
  2. Now open your WSL terminal (Ubuntu in my case).
  3. Navigate to the directory created above. Please note that to access your user directory you would navigate to "/mnt/c/Users/<username>".
  4. Make sure Docker is accessible in your WSL distro. Click here for information on how to do this.
  5. Now execute the following (you may need to stop local web/mysql servers):
    curl -s https://raw.githubusercontent.com/markshust/docker-magento/master/lib/onelinesetup | bash -s -- magento.test 2.4.4
  6. Finally navigate to https://magento.test to see your application.

To access the admin panel navigate to https://magento.test/admin and login with the username "john.smith" and password "password123". The username can be changed in the "admin_user" table (see below for information on adding a phpMyAdmin container to your installation). You may need to remove the cache by executing:

bin/magento cache:flush

You'll also want to disable two factor authentication by executing the following (make sure you clear the cache afterwards as you did previously):

bin/magento module:disable Magento_TwoFactorAuth

If you run into permission issues make sure to run the "bin/fixowns" and "bin/fixperms" commands.

Add phpMyAdmin to your container

  1. Add the following to the "docker-compose.yml" within the root of the project:
    phpmyadmin:
        image: phpmyadmin/phpmyadmin
        ports:
          - 8336:80
        environment:
          PMA_HOST: db
          PMA_USER: magento
          PMA_PASSWORD: magento
  2. Execute the following:
    bin/restart