November 26, 2018 archive

New Github repo for using WordPress with Docker

As I wrote over on my Disruptive Conversations site, I’ve been playing around with using Docker as a way to easily test new WordPress versions and plugins.  As part of that testing, I was trying to use the official WordPress image found on Dockerhub.

However, I was struggling with getting started, because the WordPress container is just… WordPress. It also needs a database to work, and my Docker experience was not yet strong enough to sort out how to link various containers together. So I raised an issue on Github asking about a step-by-step tutorial.

Github user wglambert very kindly provided a simple docker-compose.yaml file that could launch both WordPress and MySQL in separate containers and set up the necessary network and links.

It works wonderfully! And it has now been added to the instructions on DockerHub. (Thank you to the DockerHub admins for merging it in.)

Because I want to easily use the file on different systems, I put it up in a Github repo:

https://github.com/danyork/wordpress-basic-docker

Any of you are welcome to use it, too!

As I noted in my Disruptive Conversations article, I’m planning to start writing here a good bit more about using Docker. I’m rather impressed by all that can be done – and want to capture my own experiments here for my own future knowledge… and if it helps any of you all out, too, all the better!

How to Run WordPress in a Docker Container, Part 1

Wordpress docker installation

Here is a quick 3-step process for launching WordPress in a Docker container. You can use this to easily launch a new WordPress instance on your local system to test out new versions, new plugins or anything else.

First, though, you need to have Docker installed on your system. The simplest way for Mac and Windows users is to install Docker Desktop. This desktop download also gives you Docker Compose, which you will need. If you are running Docker on a Linux system, you will need to manually install Docker and Docker Compose.

To run WordPress, you also need a database running. The steps here use Docker Compose to launch TWO containers: one for WordPress and one for MySQL.

Three steps

Step 1 - Create a directory (a.k.a. "folder") and install the docker-compose.yaml file found in this Github repository. You can get the file three ways:

The key is to have this all in a separate directory because your WordPress installation will store some plugins there (see the notes below).

Step 2 - In a terminal window[1] type 'docker-compose up

This will launch the two containers and link them together. You will see logging to your terminal window. You can press Ctrl+C to stop the containers and get your command prompt back. To launch the containers in the background add a "-d" option:

docker-compose up -d

Step 3 - Connect to your new WordPress server at http://localhost:8080/

That's it!

Wordpress installation 660px

Now you simply go through the normal WordPress installation process and within a few screens your new site will be fully active.

Next you can update WordPress to the latest version, install whatever plugins you want, etc.

For example, I installed the WordPress Beta Tester plugin, went into its settings and turned on "Bleeding edge nightlies", performed an upgrade... and now I'm running the very latest WordPress 5.0 build. Perfect for the testing I want to do.

Credit for the simplicity of this approach is due to Github user "wglambert" who answered a request I made about help using the WordPress Docker container. Thank you!

Notes

  • Stopping the containers - do 'docker-compose stop'. This will stop the containers from running. Doing 'docker-compose start' will start them up again.
  • A 'wp-content' directory is created is created inside the directory in which you put the docker-compose.yaml file. Any plugins or themes you add will be stored here. This allows you to do a reinstallation and have all the plugins and themes available.
  • WHEN YOU ARE DONE and want all this to go away, just type 'docker-compose down' and the services will be stopped and the containers removed.

There are many more things you can do with docker-compose. The command-line documentation can help you learn more.

Next parts

I labeled this as "Part 1" because I'm planning to write about my own ongoing testing with Docker and WordPress. In future parts of this series, I intend to cover:

  • How to load in an existing site for testing
  • How to save your changes in a Dockerfile (so you don't have to start at the very basic installation each time)
  • ... and other things I learn along the way.

I also expect I may update THIS article over time as I do more with using WordPress and Docker.

I hope you found this helpful. Please feel free to leave comments here (unfortunately I have to moderate due to spam, and so comments will not appear immediately).

I also welcome pointers to other "WordPress and Docker" tutorials that people have found helpful. If you want to follow along with some of my other experiments with Docker and containers in general, I'll be writing about that over on Code.DanYork.com.


[1] or "command shell" or "powershell" or whatever you call it...