Setting WordPress up using Docker — Geneshki.com

Nikola Geneshki
6 min readJan 11, 2022
A WordPress install screen

When you wonder if you want to start an online shop one questions inevitably appears. What platform should I use? Usually the choice is between Shopify and Woo-commerce on WordPress. One is a Software-as-a-Service company which charges you a monthly fee, while the other one represents a “do-it-yourself, hack it somehow and deal with” it approach.

Most of the resources on the Internet would tell you that starting up is easier with Shopify, but you need to comply with their rules. They would also tell you that you need to cough up some cash from the start with both approaches. This is all fine, and in general sounds true to me too.

What isn’t that obvious however is, that a very well known technology in the IT world makes most of the troubles of setting up Woo-commerce go away in roughly an hour. Most of the additional work it requires is simply automated and all you need to do is run a few Docker containers. But first, let’s see what we actually need.

Bill of materials

First I’m going to list exactly what I used to set up Woo-commerce. Then I’m going to talk about your options.

A Linux OS

This is my preferred choice for operating system. It is also a preferred choice of the entire IT industry to run servers on, so it is a great match for running WordPress and, by extension, Woo-commerce on it. It is however by no means compulsory to use it.

Even if you’re using Windows, you can follow this guide, as long as you use the Windows Subsystem for Linux (WSL), or have Docker installed somehow.

Docker

To simplify, Docker is a tool-chain for working with Containers. That’s a lot of tech-speak, but I’ll try to explain it in a few words.

A container is an isolation mechanism. It allows us to run an application isolated from the rest of the operating system. This helps with preventing different problems due to influence from other applications. Or at least it started this way.

Nowadays it is also used for packaging the application for distribution. This is actually a natural development from the isolation feature it needs to provide.

What this means is you can package an application once and then just run the container in different places. Further, people started sharing these packaged applications and you can simply download a specification for the container and run it locally. Someone else already did the job. And they did it well.

That’s all! I didn’t really use anything else. Note that I didn’t list WordPress, Apache server, PHP, MySQL and especially not Woo-commerce.

All of these are taken care of by the lovely and generous people who prepared the Docker containers. Except for Woo-commerce, which is simply a plug-in for WordPress. Therefore in this article we’ll concentrate on running WordPress.

Run some docker containers

For starters, we need to find an image which has all the necessary software packaged in it. To do this, we’ll go to Docker Hub Container Image Library and search for WordPress. There we can see the information about this image and how to use it. There we can see an example of how to orchestrate our containers. We can see something like this:

version: '3.1'services:
wordpress:
image:
wordpress
restart: always
ports:
- 8080:80
volumes:
- wordpress:/var/www/html
db:
image:
mysql:5.7
restart: always
volumes:
- db:/var/lib/mysql
volumes:
wordpress:
db:

The containers we’ll be running

From the example configuration above we can deduce, that we need two separate containers — for WordPress itself and a database container. They images are listed right under the sections.

Under the volumes configuration we can see two separate volumes, one for WordPress and one for the database. We need to set these up first.

Creating docker volumes

Creating the docker volumes is quite easy using the docker volume create command:

$ docker volume create wordpress
$ docker volume create db

Start the containers

Now that the prerequisites are all done, what’s needed is to run the database and wordpress containers.

The database

$ docker run --name mysql --mount source=db,target=/var/lib/mysql mysql:5.7

Here we name the container mysql, we mount the db volume to /var/lib/mysql and we use mysql:5.7 as image for the container.

And the container starts to run! But there is a little problem:

2022-01-10 09:09:31+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified You need to specify one of the following: - MYSQL_ROOT_PASSWORD - MYSQL_ALLOW_EMPTY_PASSWORD - MYSQL_RANDOM_ROOT_PASSWORD

I guess we need to specify database password… And while we’re at it, it is probably better to define a wordpress database name, user name and password. Here we go:

$ docker run --name mysql -e MYSQL_RANDOM_ROOT_PASSWORD=1 \ -e MYSQL_DATABASE=wordpress_example \ -e MYSQL_USER=wordpress_example_user \ -e MYSQL_PASSWORD=wordpress_example_secret \ --mount source=db,target=/var/lib/mysql \ mysql:5.7

And the database is up. Perhaps it is a good idea to run the container detached, using the -d option, so we can reuse this console. But for debugging purposes this is fine for now.

The WordPress container

We can finally run our WordPress installation. This will follow quite a similar syntax like the one for the database container. Pay attention to the environment variables. Their values should match the ones in the previous command.

$ docker run --name wordpress \ -e WORDPRESS_DB_HOST:db \ -e WORDPRESS_DB_USER=wordpress_example_user \ -e WORDPRESS_DB_PASSWORD=wordpress_example_secret \ -e WORDPRESS_DB_NAME=wordpress_example \ -p 8080:80 \ --mount source=wordpress,target=/var/www/html \ wordpress:latest

And our WordPress is running! Unfortunately, it isn’t yet able to connect to the database, so it isn’t very functional.

You can see this when you open localhost:8080:

Error page when accessing WordPress reads “Error establishing a database connection”
Error page when accessing WordPress

All seems fine if I run a separate mysql container and try to connect to the first one using:

$ mysql -h <ip address of first db container> -P 3306 -u wordpress_example_user -pwordpress_example_secret

I get to connect and there are no errors. Which means perhaps the WordPress installation isn’t properly setup. However, if I access the WordPress container and check its configuration I find all is fine there too.

Container networking

After quite a lot of research, I found out that there is such a thing called Docker Bridge Network. It connects the created containers into a single network, so they can access each other. Containers in a single network don’t even need to publish ports to access services from each other. A network even allows containers to use hostnames instead of IP addresses (usually).

And even if you don’t configure a network, the containers are added to a default one called bridge. However, this bridge network doesn't have the DNS capabilities of the usual networks. That's why I can access the database in the db container, but only when I specify it by IP address.

Creating a docker network

Well, at least this is as simple as a single command:

$ docker network create wp

And now we should have a docker network called wp of type bridge:

$ docker network list
NETWORK ID NAME DRIVER SCOPE
97782e28e9f8 bridge bridge local
e4ce4dccf57a host host local
c65a65e61651 none null local
b81ac4bac237 wp bridge local

Connecting the containers to the network

After we already have a user-defined bridge network all we need to do is to connect the containers to it. You can also do this when running them at first, but because we already have them, we just need to add them in the wp network:

$ docker network connect wp db 
$ docker network connect wp wordpress

Now let’s check if that worked:

Wordpress on localhost:8080, after fixing the container networking issues.
WordPress on localhost:8080 after fixing the container networking issues

There you go! The WordPress install screen shows up!

What next?

Well, you have a running WordPress on you own machine. You can install a Woo-commerce plugin on it if you like. Or create your own theme and use it on it. The world is your oyster!

Edit: I published another post explaining how to install WooCommerce on your local instance of WordPress. Have a look!

Originally published at https://geneshki.com on January 11, 2022.

--

--

Nikola Geneshki

A Software Developer and Electrical Engineer. DIY enthusiast in the same areas and more. https://geneshki.com