How to Deploy Your WordPress Site with Docker

The beauty of Docker is how portable your applications become. Beyond that it also allows to version control our entire instance, providing a mechanism for quick rollbacks, updates, and audits. This tutorial will show you how to host your WordPress site with Docker by creating an image for it and deploying it.

Run WordPress in Docker

The simpliest example of running a WordPress site in Docker is too pull down the official WordPress image.

docker pull wordpress

If you prefer to use a specific version of WordPress, we can specify that when we pull an image. For example, we can pull down version 4.9.5.

docker pull wordpress:4.9.5

To run a WordPress container from the image we pulled down, we runt he following command.

docker run -d -p 80:80 wordpress:4.9.5

Verify that the image is running by using the docker ps command. The output will show you the containers state, name, and network information.

docker ps

Great. We can see our WordPress container is running. Let’s test that we can access the site from a web browser.

It looks like everything is working. We are able to deploy a new WordPress site, configure it, and post content. But what if you wanted a custom configuration out of the box, rather than a vanilla install? You will need to create your own Docker image based on the WordPress one you’re using.

Creating Your Dockerfile

  1. Create your workspace for building a new image.
    mkdir ~/Workspace/Docker/Wordpress
  2. Create a new empty file called Dockerfile in the workspace.
    touch ~/Workspace/Docker/Wordpress/Dockerfile
  3. Open the Dockerfile in a text editor.
    vi ~/Workspace/Docker/Wordpress/Dockerfile
  4. Add the following lines.
  5. Save your changes and exit the text editor.
  6. Build your custom WordPress image.
    docker build ~/Workspace/Docker/Wordpress

Running Your Custom WordPress Docker Container

Let’s run a container based on our new Docker image.

  1. Start a new container based on your image.
    docker run -d -p 80:80 my_wpsite:1.0.0
  2. Use docker ps to verify the container is running.
    docker ps
  3. Open a web browser and check the site is accessible.

Customize Your WordPress Image

We made a new image based on the official WordPress one. However, there’s no difference between the two. Let’s customize our to match our needs.

We’ll be seeding our new image with our theme pre-installed. We’ll also add a couple of plugins, too.

  1. In your Docker image’s workspace, create a new directory for your theme.
  2. Create a new directory for your plugins.
  3. Extract your theme into the themes directory.
  4. Extract each plugin in the plugin directory.

Making Your Themes, Plugins, and Uploads Persistent

The problem with seeding your WordPress image with your themes, plugins, and uploads is that any changes to them will not be persistent. This is because a Docker container is ephemeral, by default — all changes to it while its running will be lost when the container is stopped.
By using Docker’s volumes feature we can ensure all changes to certain directories are preserved when we stop a container and start a new one.

  1. Open your Dockerfile in a text editor.
  2. Add a volume for your themes WordPress directory.
  3. Add a volume for your plugins WordPress directory.
  4. Add a volume for your uploads directory.
  5. Save your changes and exit the text editor.
  6. Build a new version of your WordPress image.

Let’s run the new image and test that all changes to themes, plugins, or uploads are persistent.

  1. Start a new a container based on your latest image.
  2. Verify that the container is running.
  3. Open your WordPress site in a web browser.
  4. Install a plugin.
  5. Upload a few images.
  6. Stop your WordPress container.
  7. Verify that the container is no longer running.
  8. Start a new WordPress container based on your image.
  9. Open your WordPress site in a web browser.
  10. Verify that your plugins and images are still available.