In this article you can learn how to set up Magento 2 in a docker environment and we will highlight the benefits and downsides of it. We created a ready to run repository for you in order to quickly install Magento 2 on docker. This tutorial is written for Mac OS users but is equivalent for any other OS. As Mac OS does not have a native virtualization platform we use Virtual Box for it. In order to sync files into the docker container you can use shared folders (vboxsf) but as it is very slow you can’t use it for development. It will definitely frustrate you Instead we use gulp and file watching which does a great job! Let’s start with docker and feel free to share your experience in the comments! Also let us know if you use other techniques to set up Magento 2!
Get the repository
- Head over to https://github.com/Magenerds/docker-magento2 and checkout the repository on your machine
Install and setup the docker machine
- If you haven’t installed Docker beforehand you need to install VirtualBox and DockerToolBox. As Mac OS does not support native virtualization you need to run your docker machine in a virtual machine. DockerToolBox is a set of commands for the command line.
- Next you have to create a virtual machine (which is a simple VirtualBox VM):
docker-machine create --driver virtualbox \ --virtualbox-cpu-count "2" \ --virtualbox-memory "4096" \ --virtualbox-disk-size "64000" \ dev
You now have a VM running in the background which has the name dev.
- In order to publish the VM information to your environment you have to run:
eval "$(docker-machine env dev)"
- Now it is time to build our containers. Therefore we declared a Dockerfile in the root of the repository. If you open it you can see that we extend from the nginx image and run our own installations commands on top. In order to execute them just run:
docker-compose buildYour containers will now be built.
- Finally we use the docker-compose binary in order to orchestrate all our containers and environment. If you open docker-compose.yml in the repository you can see that we declared three containers: web (built by the Dockerfile), mysql and elasticsearch (Magento 2.1 can use elasticsearch). Run the following command in order to start your containers:
docker-compose up -d
- Finally we add a virtual host for convenience. Therefore we add the ip of our VM into our hosts file:
echo "$(docker-machine ip dev) magento2.docker" | sudo tee -a /etc/hostsThe web server is now available if you enter http://magento2.docker into your browser, but at the current state there is only the default nginx page
Setup Magento 2 in your docker environment
- The Magento 2 and the sample data github repositories are added as git submodules to our repository. In order to initialize them, enter the following command:
git submodule update --init
- Afterwards enter the source directory, show the tags and decide which version you want to checkout. The current version is 2.1:
cd sources/magento2 git tag git checkout 2.1.0
- Deploy the magento sources into a local www directory which is the copy of the web root in the docker container later on:
- As an optional task you can also checkout the sample data and deploy them into the www directory:
cd sources/sample-data git tag git checkout 2.0.0 ant deploy-sample-data
- Now it is time to install Magento 2. The build script is written in ANT and it copies the www directory into the docker container, executes composer install and copies the sources back as composer downloaded several further files into the docker container which you also want to have locally. Afterwards it runs the install script of Magento. Run the following command for installation:
- Magento 2 is now available:
- Frontend: http://magento2.docker/
- Backend: http://magento2.docker/admin (User: admin, Pwd: password123)
Developing your own extension
After the successful installation of Magento 2 in your docker environment it is now time to develop an extension. If you already have an extension on github you can easily check it out:
git clone firstname.lastname@example.org:group/module-name.git src
Otherwise just create a src directory inside the docker repository and develop your extension in it. The directory structure is flat and all the Blocks, Helper, Model directories are directly underneath the src directory:
As we use gulp as file watcher for our local environment, open the gulpfile.js and edit it like the following: Inside the options array edit
app/code/Vendor/MyModule/to your extension’s name.
In order to start the file watcher execute gulp on command line. It can be that you have to install some node modules on your machine, if you need further help check out this helpful tutorial. The file watcher is now listening to file and directory changes inside your src directory and will copy, move, remove, rename everything to your docker container and to the www directory. The www directory is your local copy of the whole Magento 2 source which is also inside the docker container.
For our docker container we have some configuration files for PHP, Nginx etc. inside the conf directory. We also added configuration for xdebug so you can debug your Magento 2 installation. You can edit those configuration files and they will be immediately updated inside the docker container as the conf directory is a shared folder (Note: we use vboxsf here as performance doesn’t matter for configuration files). A sample PHP Storm debug configuration could look like the following:
In order to start debugging you have to activate it in PHP Storm clicking on the phone:
After adding break points in the source code inside the www directory, you only need to tell the browser that you want to start debugging by adding the following to any URL you want to debug: http://magento2.docker?XDEBUG_SESSION_START=true
Advantages of docker
- Quickly set up any environment you wish
- Change environment and software versions quickly
- Try out different environments without a big set up process
- Good scaling as hardware resources are only used if container is actually in service by request
- There are plenty of images available on docker hub so you don’t need to worry about setting up the most common software
- As Magento 1 and 2 need different PHP/MySQL versions, docker solves the problem about having several versions available. This makes it easy to support both Magento 1 and 2 on the same machine.
Downsides of docker
- As Mac OS has no native virtualization, we need Virtual Box which arises the need for work arounds for syncing files to the docker container.
- If you use shared folder by Virtual Box you have to accept the poor performance of it.
- If you make changes on the source code inside the container (i.e. by composer update) they are not available locally in your www directory. But we added an ANT target for that: copy-www-to-local
That’s a wrap! Hopefully this article helped setting up Magento 2 quickly and easily. Let us know about your experiences and also please leave a comment if you have other ideas how to set up Magento 2. Maybe you have another solution without docker?