Dokku: A Heroku alternative
July 16, 2020•609 words
Dokku: A Heroku alternative
**TL;DR: Needed a way to easily deploy and manage legacy and new sites without spending a lot of money. Dokku gives me the awesome Heroku developer experience on a cheap VPS box.
Since I started working on the web, I've been publishing small sites. Usually I use these pet projects to try out new technologies, so most of them don't share the same technology. Some are almost 10 years old. Of course, I don't have the time to maintain them and their owners are not interested in spend more resources to bring them up-to-date. So I'm stuck maintaining the hosting for very different projects.
To save on expenses and time I used to host them all in one cheap VPS, but things were becoming complicated as language versions become deprecated. Keeping them in a shared environment is a challenge because different applications use different versions of PHP, databases (some use mysql, others Postgres). I even have .NET core sites running.
This year I decided to find a better solution, where I can keep all together and still make it easy to deploy or create resources on the server. So I tried Azure, AWS, Heroku and several others.
The best ones, of course, were not cheap. I think Heroku is still one of the best one in terms of developer experience. So looking around I came to know Dokku, which is an open source platform similar to Heroku (and it even uses their Buildpacks).
What's in a Buildpack
Heroku works using Buildpacks. You can picture them as the instructions on how to build and deploy an application. Dokku use these Buildpacks too to create container images and run them.
But, what about the ones with specific requirements? I have some sites that require wkhtmltopdf
. Well, it turns out that it is possible to customize the image that Dokku builds by using deploy scripts that run at different stages in the deployment process.
OK, How does it look like?
To deploy a new PHP application that uses a MySQL database, this would be enough (assuming you installed and configured the Dokku bash client):
# Create a database
dokku mysql:create myphp_db
# Create a new application
dokku apps:create myphp_app
# Allow the application access the database
dokku mysql:link myphp_db myphp_app
# Deply the application
git push dokku master
Dokku identifies it is a PHP application if it finds a composer.json
file. In it, you can specify the PHP version to use and if you need special extensions, like bcmath
.
For example:
"require": {
"php": ">=7.1",
"ext-bcmath": "*"
}
You can then configure your PHP application to read the database connection details using environment variables. Once a database is linked to your application you will have in the DATABASE_URL
environment variable a full DSN to connect to it. If you need to, you can declare more environment variables like this, not necessarily related to the database:
dokku config:set MYSQL_DBNAME=myphpapp MYSQL_USERNAME=mysql ...
Advantages
The developer experience. It allows me to manage sites and its resources without leaving the terminal. The experience is very similar to Heroku.
It can be fully automated. For example, pulling database dumps to a development environment, schedule nightly backups and SSL certificate renewals.
Zero downtime deployments.
Kubernetes
But... Dokku works using docker... that's so 2014. Dokku is already working on Kubernetes support (and should be functional at this point), although I don't mind to use and old technology if it works. However, I wish it could use podman
to avoid the security risk of having a docker daemon running.