If you're in the web development industry or the e-commerce industry, you've probably heard of Magento. Magento is essentially the power-house of shopping cart solutions, it is very feature-rich and extendible.

Now it has been a few years since I've used Magento as a platform. This is due to more recently having worked in places with internal development teams and integrated systems where a standalone e-commerce platform was not required. Today however I've become interested in trying out Magento 2.

For this we'll use Docker, more specifically Docker-Compose, and the images provided by fballiano.

First you'll need to get some access credentials from Magento - it's free and quick.

Go to https://marketplace.magento.com/ and either sign-in or register.

Then go to 'My Access Keys' under 'Marketplace' and 'Create a New Access Key'.

For this install your username will be your public key, and your password your private key.

Next create a directory called 'magento':

mkdir magento
cd magento

Next make sure you have composer installed. If you're on a Linux system you can normally install this through the repositories. For example on Ubuntu:

sudo apt-get install composer

You can also install composer manually by following the instructions here: https://getcomposer.org/download/

If installing manually you will need to install it in each directory you use composer in, or install it globally on your system.

Note: If you have installed it manually the command will be composer.phar instead of just composer.

Next we will install the Magento platform into a sub-directory using composer:

composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition magento2

Here it will ask for your Magento Repo username (ie. your public key) and password (ie. your private key), NOT your Magento account (formerly Magento Connect) username and password.

If like me the install complains about PHP requirements (mine complained that PHP version 7.1.1 was too high) you can override this by appending --ignore-platform-reqs to the above, meaning your composer command will now look like:

composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition magento2  --ignore-platform-reqs

Next create a file called docker-compose with the contents:

version: '2'

services:
    cron:
        image: fballiano/magento2-cron
        depends_on:
            - apache
            - varnish
        links:
            - db
            - cache
            - clusterdata
            - apache
            - varnish
        volumes:
            - ./magento2:/var/www/html
            - ./varnish.secret:/varnish.secret

    ssl:
        image: fballiano/nginx-ssl-for-magento2
        depends_on:
            - varnish
        links:
            - varnish
        ports:
            - "443:443"

    varnish:
        image: fballiano/varnish
        ports:
            - "80:80"
            - "6082:6082"
        depends_on:
            - apache
        links:
            - apache
        volumes:
            - ./varnish.vcl:/etc/varnish/default.vcl
            - ./varnish.secret:/etc/varnish/secret
        environment:
            - CACHE_SIZE=256M

    apache:
        image: fballiano/magento2-apache-php
        depends_on:
            - db
            - cache
            - clusterdata
        links:
            - db
            - cache
            - clusterdata
        volumes:
            - ./magento2:/var/www/html
            - ~/.composer/auth.json:/root/.composer/auth.json

    db:
        image: mariadb
        ports:
            - "3306:3306"
        volumes_from:
            - dbdata
        environment:
            - MYSQL_ROOT_PASSWORD=alovelylongandcomplexpassword
            - MYSQL_DATABASE=magento2
            - MYSQL_USER=magento2
            - MYSQL_PASSWORD=anotherlovelylongandcomplexpassword

    dbdata:
        image: tianon/true
        volumes:
            - /var/lib/mysql

    cache:
        image: fballiano/redis-volatile

    clusterdata:
        image: fballiano/redis-volatile

Save it, then run:

docker-compose up

This could take some time as we are using separate containers for cron jobs, cache, database, and a web server.

Run docker ps (likely in a new terminal session) to find your Apache container ID (eg. dockermagento2_apache_1):

docker ps

Once you have the ID, run the following to find the IP address of the Apache container:

docker inspect dockermagento2_apache_1

You are looking for the "IPAddress": "172.xx.xx.xx" line. For example, mine was "IPAddress": "172.20.0.5".

Open up this IP address in a browser and follow the instructions.

Your database host will not be the default localhost but is the name of your database Docker container. You can find this again by running:

docker ps

For example, mine was dockermagento2_db_1.

Do not use the database root credentials, but the one with the username set (eg. 'magento2').

The installation can take some time, so make yourself a coffee.