Running Wordpress with Docker Containers

Written on
November
5th,
2017
by
Karl Hughes

The first exposure I had to PHP code was Wordpress,
and ever since then the CMS has held a soft spot in my heart. Wordpress is used
by millions of websites, so knowing how to set up a new instance and run it
within Docker containers is a valuable skill for PHP developers.

1. Starting up a Database Container

Before we run Wordpress, let’s get a MySQL instance started so we can link it to
our Wordpress container. As we explained in a previous
tutorial,
we’ll use MySQL 5 and a non-root user for the database. The only additional
option we are passing in for this example is a database name (see -e
MYSQL_DATABASE=wordpress).

When you run the above command, you should see some terminal output, but once
that slows down, head over to http://localhost:8080/ and check it out.
Wordpress is ready to finish its installation.

Once the Wordpress container is started you can complete the installation at
http://localhost:8080

What’s going on here?

Let’s dig into this Docker command in more detail so you can optimize it for
your own use later.

docker run is Docker’s command to run a new container.

--rm will remove the container after it shuts down. Note that if you don’t
save your files in a volume, you may want to keep the container so you can
restart it later.

--name wp-local is a name for this container. You can choose anything you
like, but it helps us identify the container if we want to log into it later.

--link database:mysql tells the new Wordpress container to link to the
existing database container, but to map it to the hostname mysql. This is
the default database host name.

-e WORDPRESS_DB_USER=... -e WORDPRESS_DB_PASSWORD=... -e WORDPRESS_DB_NAME=...
sets environmental variables for our database connection in the Wordpress
container. You could also use a .env file (see section 2 in this
article
for details).

Using a Volume for File Uploads

You may be wondering what happens if a user uploads a file. Well, the file gets
uploaded to the container, but it’s not visible anywhere in your host system. We
can fix that by mounting Wordpress’ file upload directory as a volume:

The new part of this command mounts a volume from our local filesystem into the
Wordpress container and vice-versa. Now when we upload a file in our local
Wordpress instance it should show up in the /wp-content/uploads directory on
our host machine.

Using a Volume for Plugins and Themes

Similarly, we can mount the whole wp-content directory to keep plugins,
themes, and uploads synced on our local machine and in our container. It just
takes a slight modification to the above command:

Now any changes we make to any plugins on our local system or any new themes we
add will show up in the running Wordpress container.

As you can see, this method of setup can actually make installing and
configuring new instances of Wordpress much faster and even safer. Because devs
cannot modify core files, it can prevent consistency problems and make upgrading
Wordpress simpler.

Like this Post? Get My eBook for FREE!

Learn to build your first Dockerized PHP application.

In this book, PHP developers will learn everything they need to know to start building their applications on Docker, including: