Way back when in 2014, I wrote a blog about setting up WordPress on IIS. At that time, Docker was only a year old and I hadn't even heard of it. It's now 2017 and things are different. Docker has matured and has gained enormous popularity for making developers' lives easier. One of Docker's uses we'll examine today is using Docker for development, particularly web development in WordPress. First, let's have a little Q&A session and then we'll jump right in!
What the Heck is Docker?
Docker acts very much like VirtualBox or VMware, but stripped down and more single-purposed. It runs tiny Linux VMs that are "snapshots" of applications with all the setup already completed for you (Windows VMs are actually supported now, too).
Why is Docker Useful?
Using Docker's extensive repository of prebuilt images, one can really quickly and easily get a variety of applications running on their system. Without Docker, there's usually a good amount of setup that must be done for any given application, such as WordPress or MySQL. With Docker, it only takes a few commands to get going. Beyond this, Docker can also be used to automate the setup/teardown of different software configurations - which is what we'll be doing today.
Why use Docker for Development?
Developers work with a lot of different technologies, languages, and system configurations. If one were to install all of the various servers, databases, languages, runtimes, and applications on their host machine, there's bound to be a conflict somewhere: a port being used by another application, resources being used when switching from one type of a development to another, etc. Since Docker is a self-contained environment, your host machine is not modified and things are much less likely to break because of the aforementioned issues.
Alright, enough questions! Let's get started setting up Docker with WordPress and MySQL. This can aid in development because the container(s) we create can be shared between developers.
Depending on your host machine, the setup instructions for Docker vary. All of my development was using Linux (specifically Ubuntu MATE 16.04.2 LTS).
- First things first, install Docker. Docker's documentation is quite good, so take a look and then continue on.
- We'll also be using something called Docker Compose which lets you easily setup and link multiple containers. Windows and Mac users should install Docker Toolbox, while Linux users should follow the instructions here.
- Once that's done, it's smooth sailing. Create a directory where you want your WordPress and MySQL database to live. Create a file named docker-compose.yml and paste the following inside:
- Save the file. This file describes the Docker images (mysql:5.7 and wordpress:latest) as well as where each of these images will store their data, and on what ports one can access them.
- Now let the magic happen. Open a terminal window where you created your docker-compose.yml file and type docker-compose up -d (the -d switch tells docker-compose to run in the background).
- Open up a browser window, navigate to http://localhost:8000 and you should be presented with the WordPress setup screen!
- Congrats. You've just used Docker to setup a development environment that you can assemble and disassemble within minutes. Get hacking!
Since this post was solely about setting up Docker for WordPress development, I've barely scratched the surface of all of the cool things Docker can do or how it can be used beyond just the development phase. If you'd like to see a particular post about Docker, let me know in the comments!
It's also worth mentioning that there's other software that shares the same space of Docker (containerization, virtualization, etc.). One particularly interesting software is Vagrant which essentially manages your Docker containers (or VirtualBox, VMWare, or even Amazon EC2 VMs). Vagrant can be used for provisioning (setting up VMs and handling configuration), and makes rapid deployment painless.
I encourage you to explore further: Docker, Vagrant
Learn more about DMC's web application development services.