How to Install WordPress on Docker (Windows, MacOS and Linux)

Introduction

If you’ve been paying attention to the recent rumblings in the web/app development space, there’s a good chance you’ve encountered the term ‘containers.’ And for good reason. Containerization has multiple use-cases: whether you’re looking for an isolated environment to test your application, or want to make your application portable for easy deployment on any machine.

One of the most famous containerization solutions is Docker. It is so popular, that in fact, it is difficult not to bring up Docker when discussing containers.

In this tutorial, we will show you how to utilize Docker for your WordPress projects. This guide will prove to be beneficial for those who want a sound yet minimal environment to run isolated tests on their projects. It will also be useful for serving as a playground for experimentation with various WordPress plugins and themes.

What you’ll need

Before starting this tutorial you’ll need:

Access to the command line

First, A Word on Docker

Containers could easily be understood through the concept of virtual machines. Similar to virtual machines, containers provide security by running separate instances of operating systems simultaneously with no interaction with each other. Also, like virtual machines, containers enhance the portability and flexibility of your projects, as you are not dependent on any specific hardware, and can migrate over to any other cloud, local environment etc.

But in contrast to virtual machines, which require an installation of fully-featured operating systems along with their often-bulky overhead, containers share the kernel of a single operating system while still maintaining isolation with respect to other containers. In short, you get the same benefits of virtual machines, without the overhead costs.

Docker uses the same scheme to create containers on a Linux-based VM. In a single Docker container, you get access to all of your development needs: source code, dependencies, and runtimes.

This is especially useful for WordPress developers. Not only is it a chore to setup a test environment for WordPress (database, dependencies, server), but you also end up throwing away valuable system resources like server space, memory etc. Docker can vastly improve this, by setting up a minimal environment, that you can take anywhere with you.

Step 1 – Installing Docker

Fortunately, Docker is available on all the major operating systems: Windows, macOS, and Linux. Less fortunately, the level of Docker support fluctuates between these operating systems. So, depending on which operating system you’re using, your mileage may vary.

Option 1.1 Linux Installation

Linux is the recommended operating system when it comes to Docker deployment. This is due to the fact that Docker was originally released on Linux back in 2013. Docker installation on different Linux distributions will be different. Steps below show how to install Docker on Ubuntu 14.04 LTS, if you are running different Linux distribution, see official Docker documentation.

During configuration, Docker will ask you to enter your password. Click OK and enter the password.

Docker will now launch for the first time (it can take up to a few minutes). The green light at the bottom will indicate whether it’s running or not.

Option 1.3 Windows Installation

Due to sufficient market demand, Docker was ported to mainstream Windows versions in June 2016. Before this, only Windows Server users were able to use Docker on Microsoft’s platform. Now, you only need to have a 64-bit version of Windows 10. In addition to the OS requirement, you also need to enable Hyper-V, which you can yourself by booting into your BIOS or let Docker’s Installer do it for you.

Speaking of which, download and run the Docker Installer for Windows. We recommend going with the Stable release, though you are welcome to choose the Edge or Legacy Edge versions. During the installation phase, you will be asked for your system password, which is necessary to grant Docker certain privileges.

Once the installation is complete, Docker will launch automatically. You should be able to see the Docker’s Welcome screen at this point. You can verify your installation by opening the Windows command shell (cmd.exe) and running the following commands:

docker –version
docker-compose –version
docker-machine –version

If these are able to run successfully, you are good to go.

Now that you know that Docker has installed properly, you might want to test if it works just as intended. There are a few ways you can do this. The easiest and most common method of testing Docker is to execute”

docker run hello-world

This command pulls a‘Hello World’ sample image from Docker Hub (more on this later) and creates a new container using that image. As a result of executing the command, you should see the message:

Hello from Docker. This message shows that your installation appears to be working correctly.

At this point, you are done with Docker’s installation. If you wish to play further with what you can do, read Docker’s official Getting Started page.

Step 2 – Setting-Up WordPress on Docker

Now that you have a working distribution of Docker running on your operating system, we can proceed with deploying WordPress to gain the benefits of containers. Note that from this point onwards, it does not matter which operating system you’re using.

There are two methods of setting up WordPress on Docker. The first is the manual method, which uses the CLI to run WordPress. The second is the cleaner, more systematic method of getting WordPress to run on Docker using Docker Compose, therefore it will be used as an example in this Docker WordPress tutorial.

Running WordPress with Docker Compose

This is the recommended method for creating a WordPress container, or any container, in Docker. It uses Docker’s very own Docker Compose tool. Each container created via Docker Compose uses a config file, making it easier to port.

If you followed the instructions in Step 1 correctly, you should already have Docker Compose. You should probably still run a check:

docker-compose --version

Next, you need to create a new directory for WordPress:

mkdir wordpress

Just like the previous section. Once inside the directory, open any text editor and create a new file called docker-compose.yml.

Paste the following in the .yml file and save changes:

This will instantiate a MySQL database service, provide credentials to the database, and pull the WordPress image from Docker Hub.

Conclusion

Docker is a great tool if you want to reap the benefits of containerization. It could be particularly useful for WordPress developers, as their work requires constant experimentation with plugins, themes etc.

In this WordPress for Docker tutorial, you have learned how to install Docker on Linux, macOS, and Windows. You also learned how to setup WordPress both manually using the CLI, and through the Docker Compose utility. By following this tutorial, we hope developers shun virtual machines in favor of containers when crafting WordPress applications.

1 Comment

Great tutorial. Thanks for putting this out there. Once someone is finished developing their wordpress based site, do you provide the container to a host or export the wordpress and associated database in the traditional sense? Maybe I’m picturing this inaccurately, but I’m assuming you shove the docker container into a slot on some docker compliant host and it just runs from there. Any guidance appreciated! Thanks.