My Dokku Hosting Setup

For me, one imposition of creating web applications was always how to host them. Each project could have its own virtual private server, but that would get expensive quick and have a lot of overhead in terms of setting up the server and installing all the dependencies to run a web app. There are commerical products like Heroku and Render, but you are relying on the generosity of their free tier, which doesn't always last forever.

I wanted the ease of deploying from a git commit, but I didn't want to get locked into a commercial platform. I wanted an easy way to host multiple web apps to experiment with but didn't want to shell out for each one. I wanted them to be always on and not wait for a service to spin up before my apps were ready. So what's one to do? Setup Dokku!

Dokku is a Platform as a Service that allows you to run multiple web apps from a single server. You can deploy apps simply using git commands. Dokku supports a variety of apps; node, rails, etc. I'm sharing this guide that shows how to start a simple Node + Express server.

Prerequisites

Setup Process

Virtual Private Server - Dokku Install

I created a Dokku Docker container on a Digital Ocean Droplet following the 1-Click Install.

Dokku | DigitalOcean Marketplace 1-Click App

At first I got the lowest tier droplet but later I upgraded to the $12 tier for performance.

I followed the guide to taste deploy the Rails test app, but it wouldn’t work for me. I decided to forge ahead with deploying a Node.js app.

Domain Setup

I bought a inexpensive domain with NameCheap and pointed the DNS to Digital Ocean.

the dns nameservers pointed to digital ocean

On Digital Ocean I setup networking for the site.

screenshot from digital ocean domain setup

A subdomain is needed for each app.

screenshot showing a recrods for subdomains pointed to droplet ip

Connecting to Dokku VPS

From the terminal, I can connect to the Dokku server using ssh. I already had my private keys added to the Digital Ocean Droplet. Here are instructions on how to do that.

Command to connect to server.

ssh root@###.###.###.###

This will create a secure connection between the local computer and the Droplet.

Connect Domain to Dokku

Now that I’m connected to the Dokku service, I can link the domain name setup above to Dokku

# on Dokku Host
# replace your.domain.come with a hostname pointed at your server
dokku domains:set-global your.domain.com

Setup the Dokku App

#on the Dokku host
dokku apps:create <YOUR APP NAME>

Change <Your App Name> to whatever you want your app to be called. App names cannot have space, numbers, or special characters.

# set a custom domain that you own for your application
dokku domains:set <YOUR APP NAME> <YOUR APP NAME>.your.domain.com

Let’s Encrypt

It’s important to have an SSL certificate for every app. Some services will only work over SSL, for example Mongo will not allow any connections unless the connection is secured.

Here are the instructions for setting up Let’s Encrypt to secure your apps.

# on the Dokku host
# install the letsencrypt plugin
# plugin installation requires root, hence the user change
sudo dokku plugin:install https://github.com/dokku/dokku-letsencrypt.git

# set global email for letsencrypt
dokku letsencrypt:set --global email your-email@your.domain.com

# set a custom domain that you own for your application
# you might have already done this above
dokku domains:set <YOUR APP NAME> <YOUR APP NAME>.your.domain.com

# enable letsencrypt
dokku letsencrypt:enable <YOUR APP NAME>

# enable auto-renewal
dokku letsencrypt:cron-job --add

You can do this after deploying an app but I had it setup in advance.

Hello Dokku App

Clone this repo for testing Dokku - GitHub - awdriggs/hellodokku

  • Don’t forget to npm install
  • Run node server.js to test it is working locally!
  • Notice the main node file is called server.js and not index.js. This will avoid problems later on.

Deploying with git

Setup new remote branch for deployment!

# from your local machine, in the app folder
# the remote username *must* be dokku or pushes will fail
git remote add dokku dokku@your.domain.com:<Your App Name>
git push dokku main

When you do the last command, you’ll see a lot of terminal logs showing what is happening on the Dokku Server. When the build is finished, you will either see a sucess message.

screenshot showing a successful deployment

Or an error message, which won’t be very useful.

screenshot showing dokku error message

Scroll through the logs and look for what went wrong.

See it live!

If the push was sucessful, you should not see your app at the url you setup!

screenshot showing a message served from the dokku app

Bonus - Add Environment Variables

If you are adding services that need access to environment variables, you need to set those up with dokku using this command.

dokku config:set appname ENV_NAME=whatever

Going Further

Use this guide to see how I connect to MongoDB with Mongoose.

Resources