Containers / Data

Docker Makes Deploying WordPress Easier Than Ever

7 Aug 2020 12:50pm, by

Although the installation of a full-blown WordPress site on a Linux server isn’t terribly challenging, modern tech trends and needs might have you thinking you’d rather containerize that deployment. The idea makes sense. After all, why not opt to go a route that’ll allow you to unleash WordPress on any platform that supports the Docker runtime engine or that will allow you to scale the deployment to meet your needs.

And although you might think this would require a complete Kubernetes cluster, you’d be surprised at just how easy it is to deploy WordPress with nothing but Docker. You might also be wondering what purpose this could serve. Outside of deploying a WordPress site, doing so with Docker makes perfect sense for developers who are working on extensions and themes for WordPress. If that’s your use case, you don’t want to constantly have to jump through the hoops of installing the world’s most widely used blogging platform using the regular LAMP-based route.

So whether you’re a developer looking to make your WordPress work a bit easier, a blogger with the right amount of tech skills, or a company that needs to quickly deploy a scalable WordPress site, I have a neat trick for you.

Deploying WordPress with Docker.

Let’s make this happen.

I’ll be demonstrating this on Ubuntu Server 20.04, but the process will work on any platform that supports the Docker runtime.

Installing Docker

First, we need to install Docker. As I mentioned, I’m going to demonstrate on Ubuntu Server, so the following instructions will vary, depending on the platform you use.

In order to install the Docker runtime engine on Ubuntu Server, log into your Ubuntu server and issue the command:

sudo apt-get install docker.io -y

Once the runtime is installed, you need to start and enable it with the following commands:

sudo systemctl start docker
sudo systemctl enable docker

Next you need to add your user to the Docker group, so you can run the docker command without sudo privileges (which would be a security risk). To do this, issue the command:

sudo usermod -aG docker $USER

Finally, make the system aware of the group addition with the command:

newgrp docker

You’re now ready to deploy WordPress.

Deploying the Database Container

The first thing we must do is deploy a database container. For this, we’ll use MariaDB. Before we actually deploy the database container, we’ll pull the image with the command:

docker pull mariadb

Once the image has been pulled, we’ll create a few directories to house the WordPress data. Do this with the following commands:

sudo mkdir /opt/wordpress

sudo mkdir -p /opt/wordpress/database

sudo mkdir -p /opt/wordpress/html

Now let’s deploy our database, using the newly-created /opt/wordpress/database directory to house the data. This command will also create a wpuser account and the wordpress_db database. The command for this deployment is:

docker run -e MYSQL_ROOT_PASSWORD=PASSWORD1 -e MYSQL_USER=wpuser -e MYSQL_PASSWORD=PASSWORD2 -e MYSQL_DATABASE=wordpress_db -v /opt/wordpress/database:/var/lib/mysql --name wordpressdb -d mariadb

Where PASSWORD1 and PASSWORD2 are strong, unique passwords.

Next, we need to find out the IP address of our database container. For this we issue the command:

docker inspect -f '{{ .NetworkSettings.IPAddress }}' wordpressdb

The above command will print out an IP address, which you’ll use to connect to the database server within the container. Make sure you can connect with the database server with the command:

mysql -u wpuser -h ADDRESS -p

Where ADDRESS is the IP address of the database container. You’ll be prompted for a password. Make sure to use the password you created for the wpuser account in the database container deployment command (PASSWORD2 from above).

Exit from the MySQL console with the command:

exit

How to deploy WordPress

It’s now time to deploy WordPress. The first thing to do is pull down the latest WordPress image with the command:

docker pull wordpress:latest

Once the image has been pulled, deploy the new WordPress container with the command:

docker run -e WORDPRESS_DB_USER=wpuser -e WORDPRESS_DB_PASSWORD=PASSWORD2 -e WORDPRESS_DB_NAME=wordpress_db -p 8081:80 -v /opt/wordpress/html:/var/www/html --link wordpressdb:mysql --name wpcontainer -d wordpress

Where PASSWORD2 is the password you created for the wpuser account.

The deployment should happen almost immediately and will report back the container ID. Once that happens, you can then point a web browser to the IP address of the machine hosting Docker, like so:

http://ADDRESS:8081

Where ADDRESS is the IP address of the server hosting Docker.

You will be greeted by the language selection page (Figure 1). Select your language and click Continue.

Figure 1: Selecting the language for your WordPress installation.

In the next screen (Figure 2), fill out the necessary information and click Install WordPress.

Figure 2: Filling out the details for your installation.

Finally, when prompted, click Login. you will then be greeted by the standard WordPress login screen. Once you’ve successfully authenticated, you can start using WordPress as though you’ve installed it on bare metal.

An Even Faster Method

That’s all fine and good, but what if you want to be able to deploy WordPress even faster? You can, with the help of the docker-compose command. Of course, you first have to install this command. To do this, head back to your terminal window and issue the following commands:

curl -L https://github.com/docker/compose/releases/download/1.26.2/docker-compose-uname -s-uname -m > ~/docker-compose

chmod +x ~/docker-compose

sudo mv ~/docker-compose /usr/local/bin/docker-compose

With docker-compose on your machine, create a new directory to work in with the command:

mkdir WORDPRESS

Change into that directory with the command:

cd WORDPRESS

Create a docker-compose file for WordPress with the command:

nano docker-compose.yml

In that file, paste the following:

Save and close the file. Now you can deploy WordPress with the command:

docker-compose up -d

When your command prompt is returned, you can point your browser to http://ADDRESS:PORT (Where ADDRESS is the IP address of the hosting server and PORT is the port you assigned in the YAML file). If you want to assign a different external port, modify the “8000:80” line in the YAML file (external port is the port number on the left). By doing this, you can deploy multiple instances of WordPress on the same machine.

And that’s all there is to install WordPress using the Docker engine and the docker-compose command. Enjoy the ability to quickly roll out.

Feature image by Tyler Lastovich on Unsplash.

A newsletter digest of the week’s most important stories & analyses.