Composing a WordPress Development Environment with Docker

petersuhmThis post was contributed by guest author Peter Suhm. Peter is a web developer from the Land of the Danes. He is the creator of WP Pusher and a huge travel addict, bringing his work along with him as he goes.
 


In the last few years, a wave of virtualization technologies have swept through our WordPress development environments. The one that’s sounded the most promising to me has been Docker: lightweight and flexible. Yet, until recently, getting Docker up and running was an overwhelming task – especially on a non-Linux machine. If you managed to get it up and running in a virtual machine (using Vagrant or similar), getting port-forwarding to work would make you give up and just use Vagrant instead.

Now it’s different.

With (a stable) Docker for Mac and Windows and Docker Compose at hand, getting Docker up and running is easy and pain-free. With Docker Compose you can tell Docker exactly what you want your WordPress development environment to look like and it will take care of it.

What is Docker?

Docker is a technology that makes it really simple to create isolated containers for your applications and websites to run in. These containers can be combined and modified to fit the needs of your applications. Docker is utilizing the Linux Containers technology (LXC) where multiple isolated environments can share the same Linux kernel – making it very lightweight compared to something like Vagrant.

The Docker ecosystem is built around containers. In the Docker Hub, you can find an endless number of containers that other people have built or you can build your own using a Dockerfile. When building your own, you can start from scratch using the base Ubuntu image or extend someone else’s image.

You can share local directories with your containers and link the networks, so they can talk to each other – just like you know it from other virtualization technologies. However, this is where it gets complicated which leads me to Docker Compose:

What is Docker Compose?

Docker Compose is what makes Docker available to mortals like you and me. As the name implies, Docker Compose is a tool for composing Docker containers. That means defining your services (containers), setting up the network between them, sharing local directories with them, and a few more things.

With Docker Compose you create a simple file in the root of your project that describes the setup required by your application/website. For a WordPress theme that might mean a container to run WordPress, a container to run MySQL and a container to run Gulp or Grunt. This can very easily be defined in a docker-compose.yml file that can then be shared with your team members. This means that you can now share your WordPress theme, including an isolated WordPress environment to run it in. Hurray for virtualization!

Why use Docker?

There are a few reasons why Docker is an attractive technology for me. Here are the most important requirements I have for my development environment and how Docker solves them:

  • Clean Mac: In an ideal world, I prefer not to install anything related to my development environment directly on my Mac. I work on so many different projects that this gets unmanageable. When one thing works, another doesn’t. I also travel a lot and should something happen to my computer, I want to be able to set up a new machine in minutes.
  • Shareable: I often work in teams, so sharing my development environment with teammates is crucial. This is possible with Vagrant, but it’s still very tricky to keep environments in sync across teams.
  • Lightweight: This is important, especially when on the road. Try running a few Vagrant boxes compared to a few Docker containers and see what I mean.
  • Extendable: Extending Docker is very easy. For example, I could extend the official WordPress container and build it with WP Pusher pre-installed, since I (obviously) always use it.
  • Mirror production: My development environment needs to be as close to production as possible. With Docker this is easy, since Docker can be used in production as well.

My Docker development environment

This is the very simple Docker setup I use for development of my WP Pusher plugin: A WordPress and a MySQL container. Both of them use the official Docker Hub images, so setting it up is very easy.

My docker-compose.yml file looks like this:

It describes two services: a MySQL 5.7 database and WordPress running on PHP 5.6 and Apache. The database is using a volume on my local machine, so data will be persisted every time I shut off the container. My current directory (in this case a plugin) is mounted into the wp-content/plugins directory. This allows me to work on my plugin in a completely isolated WordPress environment – without installing anything, besides Docker, on my Mac. The WordPress container forwards port 80 to my local machine, so I can access it as “localhost” in my browser.

If you want to try it for yourself, and have Docker installed on your machine, just add the file to your plugin (or theme) and run:

$ docker-compose up -d

In order to see which containers are running, just run:

$ docker ps

This a very simple setup that is easy to extend and build upon.

I hope this post made you curious about Docker and WordPress. Thanks for reading along!

Links

17

17 responses to “Composing a WordPress Development Environment with Docker”

  1. This is something I’ve been trying to get to doing myself. Have been running Apache and company locally on my Windows machine and while they work, I don’t necessarily want them running locally – touched upon already in the cleanliness and portability points.

    Docker can run directly on Windows 10 Pro, which I have, so have had some success running both the official WordPress image and the Bitnami version (I kind of like the latter as it directly offers the ability to map config and file directories locally). The downside is that each instance needs to run on a different port for localhost, and that’s not very memorable.

    It’s possible to use reverse-proxying to redirect localdomains such as mydomain.com.dev to localhost:port. I’ve been trying to make this work with the nginx-proxy container with limited success so far. If I can get that last step working, then it’s possible to have up-to-date dev software running locally-but-not-locally. Not just WordPress, but other web projects.

  2. Interesting stuff here. I’m myself a Vagrant user and I totally agree that it is a bit slow, especially when I fire more than one Vagrant VM.

    Moving to Docker would be interesting if it is much faster. But I’m still not sure if it is production friendly.

  3. Probably the most advanced and stable Docker development environment I’ve come across is LaraDock which can also be used for setting up a WordPress project but it’s not specifically for WordPress development like Varying Vagrant Vagrants (VVV) is.
    Does anyone know of a Docker configuration focused on WordPress development?

  4. I absolutely need to get me some new virtual machines to start a more convenient and mobile developing environment. I am always traveling and risking the chance of something having to be saved to my personal laptop. This has made me more interested in exploring the world of virtual machines even more!

  5. Interesting post. I’ve been looking for a better development environment on Windows. On Mac, I’m using Valet, which is super fast and lightweight. On Windows, I’m still using XAMPP, but it’s hard to work with command line tools and the setup process is long.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newsletter

Subscribe Via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Discover more from WP Tavern

Subscribe now to keep reading and get access to the full archive.

Continue reading