Building WordPress on Docker With Windows, Linux and OS X
“Containerization” is a relatively new buzzword in big-company enterprise software circles. Just put a batch of environments in the cloud, or on a server, that just have important applications and little else. The name you hear most often when containers come up is Docker, an open source project that has become a monster.
You don’t necessarily have to own a server farm to gain some benefit from containerization. What if you just want a test environment for WordPress that allows you to experiment with themes and plugins (perhaps even developing your own) before posting everything on the web where the world can see it?
Docker can help you live that dream – and I can help you get there.
What is Docker?
Setting up a testing environment for experiments with software can be a pain. This can be especially true with WordPress. You have to set up a web server, database, credentials and all that stuff. Then the web server stays on, sucking up memory, because we’re often too lazy to only have it running when we need it.
Wouldn’t it be great if we could just have WordPress up only when we wanted to work on it, with a simple install and everything else?
James Governor at industry analyst blog Red Monk describes the idea behind Docker this way:
Docker makes it simple to spin up a container which contains everything needed to run an app – the code itself, the runtimes, systems tools etc. Develop on your laptop, then in theory deploy to any server. Unlike virtual machines, containers include the application and all of its dependencies, but share the kernel with other containers, an efficient model which maps cleanly to current development thinking in areas such as continuous integration and microservices.
Note: If you’re curious, Docker is written in the easy-to-learn Python programming language. If you’re inclined, you can poke around the code at Github.
Installing Docker on Windows
Docker was originally a Linux program, but its success among system administrators led Microsoft to pony up some cash to port Docker to Windows, which went live in summer 2016. The first iteration of Docker for Windows, however, is designed strictly for the data center. To run it, you need Windows 10 Pro with hardware-assisted virtualization and the Hyper-V virtualization client. Until Docker for Windows comes down to us mortals, we get a slightly more complicated install, with several third-party tools to set up.
First, you’ll need VirtualBox, an open source virtual machine manager owned by Oracle, but available to download at no charge. The latest version is 5.1.10.
When you open VirtualBox the first time, you’ll be asked to install an Extension Pack. Do this; especially if you want to be able to open WordPress in your real machine’s browser. More on that later.
Next up, download Docker Toolbox. This application (also free) will, among other things, create a tiny version of Linux to run Docker in a virtual machine.
Now, technically, the current version of Docker Toolbox will install VirtualBox on its own, if you don’t already have it. It also includes these applications:
- Docker Machine
- Docker Compose (more about this later),
- A GUI container management tool called Kitematic (alpha software that currently doesn’t work very well), and
- The distributed source-control system, Git, which also comes with its own command line environment and text editor, Atom.
Run the Toolbox to create the virtual machine in VirtualBox. The VM creates a command-line only version of Linux with the Docker commands and some basic shell tools. Nothing fancy like a graphical interface. All of these files are placed in the
/local/bin directory of your Linux VM.
An interactive shell appears once you’ve finished the QuickStart process. To confirm that all is running properly, type
docker run hello-world. If this completes, you are ready to use Docker to set up WordPress.
Installing Docker on Linux
Because Docker was originally built for Linux, it’s considerably easier to run in there directly. Chances are very good that a reasonably current version of your distribution (Ubuntu, Fedora, openSUSE, Debian and all their descendants) will include the Docker engine, the Docker Machine application, and the Docker Compose feature that simplifies image creation by way of a nice configuration file. The nice thing is that Linux distributions also have a graphical browser to test on. That’s how you’ll install WordPress this time.
Use your package manager to install Docker, Docker-bash-completion and Docker Compose.
Really, that’s it!
Installing Docker on Mac OS X
Docker for OS X runs on Yosemite (Mac 10.10), so chances are better you can install and run the new product on your Mac than Windows users. Read the instructions carefully before installing the application.
Docker for Mac runs inside the HyperKit VM system, and includes the Docker Engine, the command-line interface, Docker Compose and Docker Machine
Setting Up WordPress Using the Command Line
Next up, let’s set up WordPress in the Docker VM. There’s a couple of ways to do this. The first was suggested to me by blogger James Wade.
The good news is that from here on out, it doesn’t matter what operating system your computer runs. Once you’re living in the Docker environment, it all works the same way.
Create a new directory for the WordPress environment by entering the command:
Change directories to the one you just created, cd wptest.
Now the fun starts. You’re going to make a database from the command line. Type:
Here’s where Docker can seem like magic. Install WordPress with just three words:
docker pull wordpress
Docker will check to see if WordPress and all its dependencies are installed. If it isn’t, it will automatically download a zip archive from the Docker Hub website, unzip it, and install (pull) to your system. In Docker-speak, it creates an “image” of the application on your system.
One more task: Building the container. If you’ve ever installed WordPress, you’ll see some familiar commands in this string:
Now you have a Docker container named wptest linked to the MySQL database called wordpressdb. It will run in the background (because of the
-d switch), and use the WordPress image we pulled a few minutes ago.
To confirm that wptest is running, and locate its IP address (so you can run it in a browser), type:
I’ll bet the result looks a lot like this:
Now if you open that address in a browser, you’ll see the familiar Five Minute WordPress install screen. You’re in business!
That’s kind of a manual way to do it, and it’s cool to make strictly from the command line. Let’s see what we can do in a more programmed way.
Setting Up WordPress with Docker-Compose
Docker Compose uses a configuration file to create the container, making it easy to replicate as often as needed. The configuration file described here is part of the official Docker documentation.
Again, you should create a project directory to store the container and its configuration file.
Confirm that you have docker-compose installed with this command:
Use a text editor to create a docker-compose.yml file that will start your WordPress site and a separate MySQL instance with a volume mount for data persistence.
In the services section, you create the WORDPRESS database and define the credentials to access it:
MYSQL_PASSWORD. Then you tell Docker to pull the latest WordPress image from Docker Hub, display any content on Port 8000 and connect it to the WORDPRESS database it just created.
NOTE: The docker volume
db_data will persist any updates made by WordPress to the database.
Once you have the YAML file in the project directory, run
docker-compose up -d from your project directory.
As with the manual version, the Compose file does the same thing that Docker pull WordPress does – pulls the needed images, and starts the WordPress and database containers, as shown in the example below.
Last step: Let’s run WordPress. You may want to wait a few minutes after running Compose. The WordPress site will not be immediately available because the containers are still being initialized.
While you’re waiting, typing
MACHINE_VM will give you the machine address. You can then open your browser and type the results of the machine address command: http://MACHINE_VM_IP:8000.
At this point, WordPress should be running on port 8000 of your Docker Host, and you can complete the “famous five-minute installation” as a WordPress administrator.
WIN a Share of $5K
Subscribe to our blog this #hostingmonth for a chance to win one of 5 prizes of $1,000 WPMU Dev credit! Learn More.