As WordPress matures towards becoming a fully-fledged application framework, the tooling surrounding the platform continues to come on in leaps and bounds. Over the last three years in particular, developers have moved quickly to embrace best practices which have long since been standard in other platforms.
A big part of that overall push has involved fine-tuning approaches to local development environments. We’ve covered classic approaches such as local LAMP installations and experimenting with Vagrant here on the blog before, but one solution, in particular, is gaining an increasing amount of ground – Docker.
In this piece, we’ll introduce Docker as a technology, explain why you might want to use it, and cover a very basic setup for getting started with local WordPress development. By the time we’re finished, you should be in great shape for further exploration on your own.
Let’s start with a general introduction.
Just What Is Docker Anyway?
The folks behind Docker aren’t backwards about coming forwards – take a quick visit to the project homepage and you’ll see it advertised as “the world’s leading software containerization platform”. Scroll a little further down the page and you’ll be informed that “Docker enables developers and IT admins to build, ship, and run any application, anywhere”.
If you’ve ever struggled to port a local WordPress install to a live environment, your eyes may be beginning to light up at this point. To put it simply, Docker provides a set of tools that enables you to package up absolutely everything to do with your application and make it both tightly defined and trivially easy to reproduce.
Docker does all this largely by leveraging the power of Linux Containers under the hood. What this means is that you get a lightweight, performant alternative to full virtualization, and are able to define and run every part of your stack locally without your machine falling over repeatedly. You’re also able to easily reproduce that exact environment elsewhere. As the project page succinctly puts it:
Docker containers wrap a piece of software in a complete filesystem that contains everything needed to run: code, runtime, system tools, system libraries – anything that can be installed on a server. This guarantees that the software will always run the same, regardless of its environment.
Let’s briefly step through why all this is such a big deal.
The Huge Benefits Docker Brings
The key advantage Docker dangles in front of frazzled developers is that of certainty – rather than hoping everything fits together a certain way on a server, you’ll know it does.
Even in the context of the simplest possible WordPress install, there is a terrifying number of moving parts that could break or be misconfigured. A short list would include the underlying operating system, PHP, your web server, your database of choice, everything to do with WordPress itself, and so on and so forth.
You’re talking about literally thousands of individual settings and configuration details that have the potential to go wrong if they’re not identical across environments. As the main Docker site points out, by being able to encapsulate all of that potential chaos into a single known state, you gain a number of huge benefits:
- Agility: Developers can move more quickly in general, and the prospect of small changes in the application stack is no longer a source of terror.
- Portability: Whether you’re working on a single local machine or across a network of servers, you can be confident you’re dealing with the exact same environment.
- Control: Managing and operating applications becomes substantially simpler once you’re dealing with easily replicable environments.
Introducing Key Docker Concepts
Docker is doing some very clever things under the hood, and it provides a solution that’s capable of managing entire datacenters with ease. We’re necessarily just going to be scratching the surface here.
In addition to consulting the comprehensive online documentation for the project, Digital Ocean’s excellent introduction to Docker is also a great starting point for getting familiar with key concepts involving its use. There are three main areas to get your head around early:
- Docker containers. These are basically large, pre-packaged directories which contain your entire application stack. The key concept in Docker is that these are layered containers which enables them to be kept lightweight and easily version controlled.
- Docker images. Images are the building blocks of containers. They give you a dependable base from which to construct your application. You’ll find thousands of existing images on the Docker Hub.
- Dockerfiles. These are scripts that describe how a new docker image or container is to be built. Rather than laboriously typing in error-prone commands each time you set up a new environment, let your Dockerfiles do the heavy lifting.
Let’s look at actually firing this thing up locally.
Getting Docker Up and Running Locally
Though its roots are obviously in Linux, Docker has quickly matured in terms of cross-platform portability. Native apps are available for Linux, Mac, and Windows. In our case, we’re running on a Mac so we’ll be referring to the handy Getting Started guide for that platform.
Assuming you match the system requirements, installation itself is fairly straightforward. The most onerous thing you’ll be required to do is authorize with your system password during the install.
Docker will start by default upon installation and is instantly accessible via the command line. If we quickly run docker version , we should get some data back:
At this point, it’s probably worth your time getting familiar with the Docker command line to explore simple options for interacting with containers. You might also want to run a few of their sample applications to get more of a feel for things.
Let’s move on to look at a basic WordPress setup.
Creating a Quick Local WordPress Environment
Rather than reinvent the wheel, we’ll point you in the direction of Tate Barber’s handy local development with Docker tutorial. Tate uses the power of Docker Compose and Docker volumes to get things up and running quickly. Let’s briefly explain those terms in turn:
- Docker Compose. Docker Compose enables you to easily define and run multi-container Docker applications. With just a few lines of configuration, you can quickly put together complex scenarios. Check out the quickstart guide for Compose and WordPress for more info.
- Docker volumes. These are a handy way of defining particular directories that can be used to share and persist data locally. Basically, they give you a way of keeping key directories under version control on your own machine while still using them in the context of your Docker application.
Tate provides a handy Docker Compose file that we can use to quickly get an environment going:
my-wpdb: image: mariadb ports: - "8081:3306" environment: MYSQL_ROOT_PASSWORD: ChangeMeIfYouWant my-wp: image: wordpress volumes: - ./:/var/www/html ports: - "8080:80" links: - my-wpdb:mysql environment: WORDPRESS_DB_PASSWORD: ChangeMeIfYouWant
If we create a new folder locally, copy this file in as docker-compose.yml and then run docker-compose up -d, we should expect it to do the following:
- Download and run the official mariadb image and map ports.
- Download and run the official WordPress image and map the application’s web directory to our local folder.
Bear in mind that both images use custom scripts to perform some housekeeping behind the scenes. If we give all that a go in our new directory, we should see some magic taking place:
A quick trip into the browser on https://torquemag.io/ and we should see the familiar install screen.
If we pop into our local directory, we should see our WordPress files nicely isolated from the rest of the stack so we can work on them locally as we wish.
Presto! We’ve got a local development environment that can be easily spun up and down, and which lends itself easily to version control. Not bad for just a few lines of code! Tate’s quick example makes an excellent starting point for further exploration on your own. When you’re looking to build out more complex examples, Tomaž Zaman’s WordPress and Docker series is also well worth diving into in depth.
Though it can take a bit of time to fully grok the background concepts involved, it’s not hard to see why Docker is gaining increasing ground worldwide. The combination of performance and predictability it delivers hits a sweet spot for developers, and removes entire classes of day-to-day hassles and dilemmas.
Let’s recap the main points we covered to start exploring Docker’s power with WordPress:
- Take some time to grapple with the core concepts involved.
- Get familiar with the project documentation and quickstart guides.
- Use Tate Barber’s local WordPress setup as a jumping-off point for your own further tinkering.
We’d love to know if you’re already exploring Docker options, and whether it’s been plain sailing so far or heavy going. Get in touch via the comments below and let us know!
Featured image: hennievg
Join the conversation