Every developer, sooner or later, had to deal with WordPress, given it is one of the most popular Blog/CMS platform, if not the most popular.
According to Wikipedia, roughly 22% of the web sites run on it, (it means one web site in five) it is widely know by users, it has a large community (over 30 thousand contributed plugins) and it is easily supported by designers.
Unfortunately WP was targeted at non-developer people, it had a great success as hosted platform, but working with it from the developer perspective, especially if we look at the workflow, looks clunky and outdated.
Usually it involves:
- downloading a tar ball of the latest WordPress stable version
- if you’re using
git(and you should!), add the
- open a connection (possibly not FTP, but probably it will be FTP) and (slowly) upload everything on the dest server
- create a remote
wp-config.phpwith the production configuration
- launch the installer
- hope nobody will overwrite
wp-config.phpwith the local copy
On the other hand, modern workflows are built around some kind of version control system (usually
git) where the deploy is managed just by pushing a branch on the public server.
This is called push-to-deploy and is the one used by Heroku.
Fortunately, some smart guys created Docker and Dokku, two projects that make possible to build you own personal-heroku-like PaaS in a matter of minutes (If you want to try it, Digital Ocean offers cloud servers with Dokku preinstalled at a starting price of 5$/month).
Let’s see how it applies to WordPress.
In the rest of the article I’m going to use a few placeholders
appis the application name
dokkuis the address (or hostanme if configured) of the destination server running Docker & Dokku
dokku-useris the user running Dokku on the remote machine
First of all we’re going to clone WP from github
git clone email@example.com:WordPress/WordPress.git app
Then we create
wp-config.php, replace the configuration parameters with environment variables, and commit it.
This way we won’t have to hardcode them.
same technique can be used to configure the security keys, I didn’t, to keep things short.
define('DB_NAME', getenv('WP_DB_NAME'));/** MySQL database username */define('DB_USER', getenv('WP_DB_USER'));/** MySQL database password */define('DB_PASSWORD', getenv('WP_DB_PASS'));/** MySQL hostname */define('DB_HOST', getenv('WP_DB_HOST'));
git add wp-config.phpgit commit -m 'added WordPress configuration'
If you use Apache, you can set the values with
SetEnv, if you’re running Nginx and phpf-pm, you can use the
ENV section of your application pool.
You don’t need any of that when deploying through Dokku.
For our first deploy, we need to add a new
remote pointing at the Dokku server
git remote add dokku dokku-user@dokku:app
and push the code
git push dokku master
You will see something like this
Counting objects: 163187, done.Delta compression using up to 4 threads.Compressing objects: 100% done.,Writing objects: 100% | 4.95 MiB/s, done., 84.87 MiBTotal 163187 , reused 163156-----> Building app ...PHP app detected-----> Bundling NGINX 1.4.3-----> Bundling PHP 5.5.5-----> Bundling extensionsphpredismongo-----> Setting up default configuration-----> Vendoring binaries into slug-----> Discovering process typesDefault process types
As you can see, everything is already bundled with the PHP buildpack.
Dokku has detected the PHP app and instructed Docker to create an isolated container that could run the application.
Our wp-config is empty right now, the server will reply with
That’s good news, it means it is actually responding to our request.
To finish our setup we need a couple more things:
- create a database
- configure the app environment with the credentials
To create a db in our app container, we’ll use the MariaDB plugin for Dokku.
There’s also a MySQL plugin, but it has some annoying bug and since MySQL and MariaDB
are virtually identical, we’ll stick with the last one.
Installing a plugin for Dokku is as easy as running
cd /var/lib/dokku/pluginsgit clone https://github.com/Kloadut/dokku-md-plugin mariadbdokku plugins-install
Some of them don’t require the final
plugins-install step, but it won’t hurt if you run it anyway.
Tip: you can run dokku commands on your local machine and execute them on the remote one with:
ssh dokku-host dokku-command(i.e.
ssh dokku help)
We can now create the database
ssh dokku mariadb:create app-----> Creating /home/dokku/app/ENV-----> Setting config vars and restarting appDATABASE_URL: mysql2://root:VQpzDZRrEUAkUuAI@172.17.42.1:49170/db-----> Releasing app ...-----> Release complete!-----> Deploying app ...-----> Deploy complete!-----> app linked to mariadb/app database-----> MariaDB container created: mariadb/appHost: 172.17.42.1Port: 49170User: 'root'Password: 'VQpzDZRrEUAkUuAI'Database: 'db'
and set set the Environment variables
# the format is dokku config:set app key=value key=value# I splitted up the command on different lines for clarityssh dokku config:set app WP_DB_HOST='172.17.42.1:49170'ssh dokku config:set app WP_DB_NAME='db'ssh dokku config:set app WP_DB_USER='root'ssh dokku config:set app WP_DB_PASS='VQpzDZRrEUAkUuAI'
If everything went right, you should now see the standard WordPress install.
Choose a title, create an admin user and you’re ready to go.
You can work on your local copy, add plugins, work on your theme, and when you’re happy with it, you push all the changes and the app is automatically deployed and configured.
We just scratched the surface of what is possible with Docker & Dokku.
In a next article we’ll see how to create a Dokku plugin to automate the entire process.