Mirko Stocker

Add a Database to your own PaaS

This is the second part of our series on how to be your own PaaS provider using Dokku and RunAbove. The first part covered the basic installation and enabled us to deploy simple Play applications. In this part, we’ll deploy a more interesting application that uses a database.

Adding support for databases and other additional services is handled by Dokku plugins. Our goal is to add a MariaDB database instance and to deploy a Play application that uses it. So first we need to install one of the MariaDB plug-ins.

% ssh admin@ownpaas.tk
cd /var/lib/dokku/plugins
sudo git clone --recursive https://github.com/Kloadut/dokku-md-plugin mariadb
cd mariadb/dockerfiles
sudo git checkout master
sudo dokku plugins-install

Everything is well if the last command finishes with no output. You can verify that the plugin got installed by running dokku help:

admin@my-own-paas:~$ dokku help
...
    mariadb:console <app>     Open mysql-console to MariaDB container
    mariadb:create <app>      Create a MariaDB container
    mariadb:delete <app>      Delete specified MariaDB container
    mariadb:dump <app> <file> Dump default db database into file
    mariadb:dumpraw <app>     Dump default db database to std out 
    mariadb:info <app>        Display database informations
    mariadb:link <app> <db>   Link an app to a MariaDB database
    mariadb:logs <app>        Display last logs from MariaDB container

So let’s create a database!

admin@my-own-paas:~$ dokku mariadb:create computers

-----> MariaDB container created: mariadb/computers

       Host: 172.17.42.1
       Port: 49154
       User: 'root'
       Password: 'XXX'
       Database: 'db'

You don’t have to write the configuration down, we will later inject them into our application container as environment variables. First, create a new application and do the usual dance to deploy it:

% activator new computer-database computer-database-scala
% cd computer-database/
% git init && git add . && git commit -m "init"
% git remote add dokku dokku@ownpaas.tk:computer-database
% git push dokku HEAD 
...
=====> Application deployed:
       http://computer-database.ownpaas.tk

Don’t try to access the application, it won’t work. Why?

admin@my-own-paas:~$ dokku apps
=== My Apps
computer-database
play-scala
admin@my-own-paas:~$ dokku logs computer-database
Picked up JAVA_TOOL_OPTIONS:  -Djava.rmi.server.useCodebaseOnly=true
@6ki18l5gj: Database 'default' needs evolution!
...
[warn] play - Run with -DapplyEvolutions.default=true if you want to run them automatically (be careful)

Not only does it need evolution, we also need to configure it to use our MariaDB instead of the H2 in-memory database it is currently configured to use. We can pass the evolutions-option using an environment variable:

admin@my-own-paas:~$ dokku config:set computer-database "JAVA_OPTS=-DapplyEvolutions.default=true"

Now, let’s link the application container to the database container:

admin@my-own-paas:~$ dokku mariadb:link computer-database computers
...
-----> Setting config vars and restarting computer-database
       DB_HOST:     172.17.42.1$
       DB_PASSWORD: 18BZYCAANmepCbPD$
       DB_NAME:     db$
       DB_PORT:     49154$
       DB_USER:     root
...
-----> computer-database linked to mariadb/computers database

We can use those environment variables in the application configuration, so edit conf/application.conf and change the database section to this:

db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://"${DB_HOST}":"${DB_PORT}"/"${DB_NAME}
db.default.user=${DB_USER}
db.default.password=${DB_PASSWORD}

Also don’t forget to update the build configuration to add the JDBC MySQL connector:

libraryDependencies ++= Seq(
  ...
  "mysql" % "mysql-connector-java" % "5.1.30"
)

Ideally, we would be done now, but the SQL script in the evolutions aren’t valid for MySQL/MariaDB, so we need to patch the sample application. I’ve prepared a patch that you can apply to your application (based on this fork):

% curl http://misto.ch/wp-content/mysql-compatible-evolutions.patch | git apply -

Add and commit to the repository to deploy:

% git commit -a -m "db config"
% git push dokku HEAD

Now it’s time to visit your application! This concludes this series. Let me know in the comments if you have any suggestions for further articles, providers to evaluate or things to deploy.

1 Comment

Leave a Comment