Here’s a hands-on guide to WordPress deployment using Docker.

preface

We all love WordPress, where you can get a nice, functional dynamic site without (almost) writing a line of code.

It’s perfect for a perfunctory way of helping friends who come to you to write weird little websites. Usually, in this scenario, you can ask your friend to open a new server, initialize a LAMP, and simply add WordPress to Wget to complete the configuration in your browser. Sometimes, for example, if your friends are using Aliyun, Baidu Cloud or other large cloud service providers, you can even initialize a WordPress application image and start setting directly in the browser.

However, the other day I needed to deploy a WordPress service on my own server. Of course, the best programming language in the world, PHP, is out of the question for a konjak like me. No PHP, no WordPress. Install a PHP, usually do not need, but also increased the security risk, not cost-effective.

It’s nice to have a brand new LAMP server, but it’s impossible to buy another server. So I thought — Docker, just put it in a container.

Let’s go ahead and see how to deploy WordPress with Docker.

So, by now, you should have Docker installed on your server or PC. On most non-Windows normal systems, installing Docker is just a few simple commands.

Before continuing……

By law, any article exploring containers must be accompanied by a picture of a container ship full of containers, like the one below:

(I learned this tradition from this article on IBM Developer. I don’t know why, but a good league member is bound to follow the rules.)

Pull the mirror

For things that are so common in WordPress, of course, there are ready-made images, so we don’t have to build them ourselves.

Pull up a wordpress image:

$ docker pull wordpress:latest
Copy the code

Then, you should know that WordPress requires MySQL.

Here we have two options. One is to use the MySQL database on the host or any other server. Use a MySQL Docker image. In order to facilitate and practice the use of Docker more, we simply pull a mysql image and let the whole service run completely in Docker:

$ docker pull mysql:latest
Copy the code

Note that this article was written in spring 2020, so here mysql: Latest is mysql V8.0.19.

Start the service

Without further comment, with the mirror, we directly open the service:

$ docker run -d --privileged=true--name Mysql_Test -v /data/mysql:/var/lib/mysql -e MYSQL_DATABASE=wordpress -e MYSQL_ROOT_PASSWORD=233333 mysql $ docker  run -d --name WordPress_Test -e WORDPRESS_DB_HOST=mysql -e WORDPRESS_DB_PASSWORD=233333 -p 2020:80 --link Mysql_Test:mysql wordpressCopy the code

OK, this is the magic of Docker, do not install PHP, do not need to consider carefully for security, a lot of Settings, front and back after the four commands! (Of course, I’m only running a simple service and hardly anyone is using it, so there’s almost no security risk, but take the time to think about security when you deploy it.)

The next step is to go to http://xxx:2020/wp-admin/index.php in your browser and complete the “famous” 5 minute wordpress installation!

But…

When you happily open your new site, you’ll find wordpress notifying you that you can’t connect to mysql. (I forgot the screenshot, it will write a bunch of English to tell you this, you can understand)

Google will tell you that the problem is due to a change in the default user authentication method of mysql 8, which iS not recognized by wordpress. To solve the problem is not difficult, open mysql Settings, to change the authentication mode to the appearance of WordPress:

$ docker exec -it Mysql_Test mysql -p
Copy the code

Run the mysql command:

mysql> use mysql;
mysql> select host, user.plugin from user;
mysql> ALTER USER 'root'@The '%' IDENTIFIED WITH mysql_native_password BY '233333';
mysql> select host, user.plugin from user;
Copy the code

Let’s change root’s plugin from caching_sha2_password to mysql_native_password, and we’ll be fine.

Then go to http://xxx:2020/wp-admin/index.php again, and this time it should be the “famous” wordpress installation in 5 minutes, you can do it yourself 🙂