Magento 2 Docker Development
Posted on 11 Jul 2016 Magento 2
Magento 2 Dockerised (OS X)
STANDARDISE ENVIRONMENTS
One of the main challenges of scaling an application and continuous integration across the lifecycle of an application is the consistency across all environments. Setting a Magento 2 Docker image for your development team to use across their development, testing, staging and production environments is a great way to setup the team and the site up for success.
PORTABILITY
Combining Magento with Docker gives you maximum flexibility in porting your eCommerce application across any number of cloud providers such as AWS, Rackspace, Google Computer Engine, Virtualbox and more. As long as the Operating System supports docker images, you can pull down the images and create your working environment quite swiftly.
SECURITY AND ISOLATION
Docker ensures that your development stack and it’s components have there own resources and ports which are isolated from each other. If you felt the need for example to install Jira which requires Java to run on your staging environment you could potentially do so along side Magento 2 even though it has quite a different architecture.
DEVELOPMENT DOCKER BASE
Let’s start by setting up a development environment for OS X which includes all the main services in their respective container. One of the challenges found when starting off with Docker was creating a basic structure to build upon but without going over board with the configuration & automation of the stack. Rather than provide the images let’s build it all out. By using docker compose for the backbone (internal/external ports, links) and individual Docker files to describe the inner details (libraries, init scripts etc) its a straight forward way to understand the architecture.
This base framework aims to succinctly allow you to expand it to your own needs. On your separate environments you could configure slightly different environmental parameters which are native to it such as base url, database/redis authentication, etc). With the docker install files and scripts we are going to create a base Magento 2 Development environment for Magento Community 2.1 which will support the following components in there own containers for OS X.
- PHP5.6, PHP FPM
- nGinx
- MariaDb
- Redis
PREPARATION
Firstly we want to install the necessary software on OS X to ensure our application runs swiftly and is a joy to work with. As Magento 2 is quite a resource hungry application, what we want to aim for is a 2-5 second page load running uncached with the full stack whilst creating features and or debugging issues. With caching enabled the performance is quite quick without the final glisten provided with a Varnish System level caching. In production this is acceptable for development we would like all caching removed.
Let’s start installing the following in roughly the following order:
- Docker Tools
- Docker Compose
- Vagrant virtual box
- Virtual Box
Initially I was surprised with some of the issues faced with getting a responsive Magento Development Environment with Docker running on a Mac vs Windows operating system. But then it dawned on me the reason why. As OS X is a Unix based core and not a Linux based core in which docker supports, we need to use Virutalbox to create a layer for docker to work with. Using dockers native file syncing to the stack was initially very slow yielding results of up to 10 second page loads. Becaus of the 60,000 files in the project build I ended up mounting just the /app folder into vagrant and page loads where 2-3 seconds which became a joy to work with.
brew update
brew install docker
brew install docker-compose
brew cask install virtualbox
brew cask install vagrant
INSTALLATION
After installing the above please checkout the Magento 2 Docker environment from GitHub into a directory of your preference. It includes:
- /docker-compose.yml
- /www (Holds Magento 2)
- /www/.gitignore
- /build
- /build/nginx (Dockerfile, shell scripts, .conf host file)
- /build/php (Dockerfile, shell scripts, .ini file)
# navigate to directory housing your websites: cd /Users/nameofuser/Sites/
git clone https://github.com/magescale/magento-2-docker magento2.dev
Let’s install Magento 2. Keeping on the topic of this post I will give you the outline on this the guide from the platform is great. We have 2 basic options checkout the code from Git or using composer to install the application. The latter offers the most flexiblity from a development standpoint.
- Magento 2 Install guide: http://devdocs.magento.com/guides/v2.0
Magento 2 Github: https://github.com/magento
create-project --repository-url=https://repo.magento.com/ magento/project-community-edition www
or
git clone https://github.com/magento www
One you added the platform to the www folder, let’s set the correct permissions and initiate our environment.
cd bin
sh permissions.sh
cd ..
docker-compose up -d
Let’s add the host name into your hosts file and also set up MySQL to work with the Magento 2 Docker environment.
docker ps -a
docker exec -it nameofcontainer_php
cd /var/www/html/magento2.dev/
bin/magento setup:install --admin-user="adminuser" --admin-firstname="admin_firstname" --admin-lastname="admin_firstname" \ --admin-email="user@admin.com" --admin-password="admin_password"