To build and run a web application, you need to install few software’s/tools and configure them. You can, of course, install those software’s and tools manually on your machine (your own operating system). But this can lead to some troubles! and has some limitations, such as reinstalling everything whenever you change your development machine (computer, laptop), switching between software versions, using the same port for two different software’s, sharing the application with other people when you have made some special configurations to the environment, etc…
The solution to all of the mentioned troubles would be to ship the development environment with the application code! so when someone gets the code, he can boot the environment and have everything ready.
Tools like Vagrant and Docker, are built to help with this, they both creates virtual environments, which can be used to build and run your applications. That aside, each of them does the job in a different way, and each has its own advantages.
Vagrant is a virtual machine manager, means it helps you create, provision and shares a virtual machine. (Which you can SSH into and install everything you need to start coding).
A virtual machine (like VirtualBox and VMWare) is a virtual operating system running on top of your native operating system. Yes! just like that. They basically virtualize your machine hardware for the operating systems, so you can create as many operating systems as you need and run them all on a shared hardware.
However, virtual machines takes a lot of resources to run, so don’t expect to run five operating systems on top of yours. As well as it doesn’t make sense to run a virtual operating system on production, which means if you are using virtual machines technologies (like Vagrant) you can only share the environment with other developers and team members, but not with production servers.
Likely, there’s a smart trick for using virtual machines on production, the trick is you can provision your virtual machine with configuration management tools (like Puppet, Chef and Ansible) and then run the same tool on production to provision your production environment.
Now let’s talk about how to bypass the virtual machines limitations using different technology like Docker.
Docker creates virtual containers. (not virtual machines). Docker virtualize your operating system for the software’s, so you can create as many software’s (containers) as you need and run them all on a shared operating system.
Docker is much more flexible and scalable than virtual machines and it runs everywhere!! (development, production, staging, Q/A, CI).
A Container is just the part of the operating system (Linux of almost any distribution) that is responsible for running a software (like a DB engine “MySQL” or Cache system “Redis”). Means it’s just a portion of the operating system! All the other portions are shared between all the containers. Which makes running a virtual container 10x faster than running a full virtual machine.
Docker can run multiple containers, and it’s very recommended to run single software per container, in order to kill the dependencies between the software’s (since each software will be having it’s own isolated Linux operating system). So if you mess up with anything in a Container, it’s no big deal! you can easily restart the Container and get back to the original state, without affecting any other software running on the other Containers.
Virtual Machines offers greater isolation at the cost of greater overhead, as each virtual machine runs its own full kernel and operating system instance. While Virtual Containers, offers less isolation but lower overhead through sharing certain portions of the host kernel and operating system.
So using tools like Vagrant and Docker will save you time preparing the environment and will make your environment portable and reproducible anytime and almost anywhere.
One of the great things about both the Vagrant and Docker ecosystems is that there are loads of great Vagrant “boxes” and Docker “images” already available to use, so you don’t have to build them by yourself, which speeds things even more.
As a PHP developer myself, I prefer using Docker over Vagrant! because it’s much faster and I can get my environment with me to production.
What I like mostly about Docker is the ability to switch between software versions (PHP 5.5, 5.6 and 7.0), Database Engines (MySQL, PostgreSQL and MariaDB) and cache systems (Memcached and Redis) without affecting anything else (this is not possible with Vagrant!).
However, if you are using different PHP framework, you can still use LaraDock and edit few simple variables to make it work with your framework.
Thanks for reading. Follow me on Twitter, to stay up to date @Mahmoud_Zalt