Last Updated: 20th Sep 2017
WPTavern: Composing a WordPress Development Environment with Docker
This 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.
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!