Example Repo in https://github.com/maxgoffman/WordPressWithWPCli
All this crap needed to be installed manually in your computer eating disk space. Running the programs also took memory and CPU power. If you’re using your home computer to work (like I do) this can be pretty annoying. That’s until Docker came along.
What is docker?
Wikipedia says that it’s “a set of platform as a service (PaaS) products that use OS-level virtualization to deliver software in packages called containers”. Now, unless you are familiar with coding, all this techno mumbo-jumbo probably means nothing to you. So I’m going to tell you a short story:
A couple of weeks ago, I was tasked with the unfortunate job of reviving an old project. The project didn’t have Docker or any kind of containerization implemented. As I glanced through the installation instructions, I realized that I had to install all kinds of old dependencies and packages like Less, Bower, AngularJS, PHP 5, and weird deprecated libraries. Also, the installation instructions were definitely wrong. It was hell. So, after asserting everything that was needed, I set on dockerizing the project. In a couple of hours, I turned that long and confusing manual into a 2 lines instructions manual:
- Prerequisites: Docker and Docker Compose* (more on this later).
- To install, use docker-compose up –d.
So, what is Docker? It’s a tool that changes long and tiring deployments into a 5 minute installation. I’ve been using Docker for quite some time, so I’ve been aware of how good is Docker for Continuous Integration and Deployment environments. But, it wasn’t until this experience that I realized that the greatest thing is how it streamlines your development process, saving time in useless and meaningless tasks.
How it works?
Docker has these things called Images. These are instructions to build a Virtual Machine that is used to run a program. Also, Images are extensible, from a basic Image you can add stuff. So we can have very basic Images and very specialized. And believe me: there is an image for every need.
When you instruct Docker to run an image, it turns into a Container. A container is the Virtual Machine per se. Because you can make a container interact with other containers, these are also in turn very flexible. This is both really amazing and powerful: for example, it means you can have a Virtual Machine running a Web Server and another running a Database. You can then run these on demand in a cloud service, or run them all in your local computer. Best of all, is that containers are lightning fast.
A practical example
The best example I can think of for showcasing the practical use of Docker is this blog. That’s right, this tool is so useful that even this blog is running on Docker. To write an image, you need to define the instructions in a Dockerfile. Let’s take a look at the Dockerfile I wrote for this blog:
If you’re not familiar with bash scripting this may seem complex. However, it’s not that difficult to understand the core. I’m just using 3 docker commands: FROM, RUN and COPY.
- Using FROM, I start by extending WordPress official image.
- Then, I RUN a command that triggers a download of WP Cli (the WordPress official command line tool), and install it.
- Finally, I COPY my project files into the webserver folder, so they’re hosted “in the internet”.
That’s it. That’s the basic concept. Now we can tell Docker to run this image and it’ll create a container.
Most of the time, one container alone isn’t enough. For example, to run WordPress you need a container with a webserver that has WordPress installed. But you also need a container that has MySQL, which is the database were WordPress stores all the persistent data.
Making containers interact only using Docker can become annoying. That’s were Docker Compose comes along: it’s a tool to define and run multi container applications. Using a yaml structured file, the configuration and properties of a group of containers can be defined, and Docker Compose will take care of telling Docker how to run these containers. Returning to the example of this blog this is the configuration I use:
The basic concept Docker Compose is doing here is the following:
- Tell Docker to create 2 containers: db and wordpress
- Container db is based on Docker MySQL 5.7 image.
- Container wordpress is created from the Dockerfile I shared before.
- Container wordpress depends on the db container.
- Container db shares it’s data with the rest through the data volume.
Running the command “docker-compose up”, and everything will start up automagically.
Fast and neat, with this if you’re a developer in a few minutes you can have any kind of system running.
We’ve seen the Pros. What about the cons?
That’s the best part: there are no Cons! Well… to be honest, maybe a few, very relative cons: you may have heard that Docker is for Linux. That is the unfortunate truth. Docker runs natively on Linux. Don’t get me wrong, Linux is a great OS… to work with. The first time I used Docker, I installed an Ubuntu to get it running. But this can be pretty frightening for newbies. In spite of that, the good news is that Docker can run on Windows and Mac, and it can run almost as well as in Linux. On Windows 10 professional installations it can run semi natively using Windows WSL (Windows Subsystem for Linux). Since 2020, it also works on the newest Windows Home installations.
If you’re stuck developing with an older computer you can use Docker Toolbox, which runs an Oracle Virtualbox Docker Machine with Tiny Core Linux and docker installed inside to make it work. I’m still using that and it works great. At first, I had some trouble with how slow the performance was with Windows shared folders, but then I customized my Docker Machine with Docker Compose, an extra virtual disk and some bash commands of my own creation. Before running a Docker container, I run one of these commands to copy the files from the Windows Shared Folder to the internal virtual disk, then the same command creates container from there. With those instructions, I could achieve a similar performance than in Linux.
The second con could be that if you want to take advantage of Docker you need to know Unix/Linux Bash scripting. Well, if you’re a professional developer here is my advice: stop crying and learn Unix bash scripting.
Anyway, I hope you enjoyed reading this as much I enjoyed writing it!