Deploying phpMyAdmin to a Dokku instance

While preparing to give a talk at a local PHP meetup group on deploying PHP applications with Dokku, I wondered if it were possible to deploy a working instance of phpMyAdmin, despite there being no real Dokku plugins readily available. It turns out, using the power of Docker, you can!

Here's how.

The steps

It basically amounts to performing a Docker image deployment from the Docker registry, using an image that contains phpMyAdmin.

First, shell into your Dokku server and pull the image:

$ docker pull phpmyadmin/phpmyadmin

Next, create a Dokku app to house phpMyAdmin:

$ dokku apps:create admin

Retag the Docker image:

$ docker tag phpmyadmin/phpmyadmin dokku/admin

Finally, deploy the tag:

$ dokku tags:deploy admin

At this point, you will have a working install of phpMyAdmin which you can access at http://admin.<your domain>, but we haven't yet connected it to a database.

Assuming you have already installed the Dokku MySql plugin, create a database (or skip this if you've already created one):

$ dokku mysql:create demodb

Now link it to your app:

$ dokku mysql:link demodb admin

One additional thing we need to take care of, is the host name that phpMyAdmin should use. Linking the app to the database installs the DATABASE_URL environment key into the app's container, but unfortunately phpMyAdmin doesn't use it. It instead uses a key called PMA_HOST, which is simply the host name of the MySql server that you want to connect to. Fortunately we can get this information from Dokku:

$ dokku mysql:info demodb

=====> Container Information
       Config dir:          /var/lib/dokku/services/mysql/demodb/config
       Data dir:            /var/lib/dokku/services/mysql/demodb/data
       Dsn:                 mysql://mysql:ad4bbdb819d5ae99@dokku-mysql-demodb:3306/demodb
       Exposed ports:       -
       Id:                  3b8a6d32e2f03b0ee581ee23e187191adb7e924691d6c36c3cd5089844bd6433
       Internal ip:
       Links:               -
       Service root:        /var/lib/dokku/services/mysql/demodb
       Status:              restarting
       Version:             mysql:5.7.12

Notice that Dokku will spit out the DSN that applications should use in order to connect to the database. We can extract the host name from that url, and set the right key on the phpMyAdmin container:

mysql info

$ dokku config:set demodb PMA_HOST=dokku-mysql-demodb

Now phpMyAdmin can connect to the right database. The only thing left to do is recover the password. Fortunately for us, this is also specified in the DSN for the database (given by dokku mysql:info):

mysql info with password

Copy this into your clipboard, and let's access the app:

phpMyAdmin on Dokku

And here we are inside phpMyAdmin!

phpMyAdmin