889 Followers
·
Follow

Image for post
Image for post

Host your Node app on Dokku & DigitalOcean

Cheap, easy to use hosting, entirely under your control.

DigitalOcean are a superbly cheap and remarkably frictionless platform to run your servers (a cut-down and honed AWS competitor) in the cloud. If you want quick easy hosting, they’re fantastic. The one downside with IaaS platforms like this though is that you have to manage them yourself, and they can’t give you the same effortless deployment experience as a PaaS product like Heroku. There’s a solution: Dokku.

Dokku is a Heroku implementation. It’s based on Docker, although you don’t really need to care about that. You start Dokku, tell it what services you need (a database please), and then just push deployments of your application to it, and it handles all the rest. In addition, DigitalOcean provide one-click setup for it, giving you a basic configured instance straight out of the box.

There’s still a tiny bit of initial setup required to get it usable though, and unfortunately DigitalOcean’s own Dokku docs come with a big deprecation warning. Fortunately, once you know the magic steps, doing the whole thing yourself from scratch isn’t too hard:

  1. Create a DigitalOcean account. For bonus points, use my referral link and we both get $10 free credit (= 2 months of hosting).
  2. Do the standard setup steps (confirm your email, add a credit card)
  3. Launch a Dokku instance (v0.4.14 at the time of writing) from the ‘One-Click Apps’ tab https://cloud.digitalocean.com/droplets/new. Make sure to add your SSH key!
  4. One the instance starts up 60 seconds later, open its IP (available on the droplets page) in a browser to get the Dokku setup page. This page should already have your public key and know the instance’s IP. Add your server’s hostname, enable virtual hosting, and click ‘Finish Setup’.
  5. This will then redirect you to the full Dokku setup documentation. That’ll be useful for reference and more complex setup later, but you don’t need it for now.
  6. SSH directly into your new Droplet: ssh root@droplet-ip
  7. Create your dokku app: dokku apps:create app-name
  8. On your local machine, in your project’s Git repository, add your droplet as a remote: git remote add dokku dokku@droplet-ip:app-name
  9. To have your Node app work in Dokku by default, it’ll need to start up when ‘npm start’ is run, and to then listen on the port specified by the PORT environmental variable. You can either make the changes to do that, tweak this yourself with your own Procfile (as on Heroku), or control everything completely with your own Dockerfile.
  10. If you need environmental variables set for your service, ssh in as root and set them with: dokku config:set app-name KEY=VALUE
  11. Once those changes are made you can deploy your code to the remote: git push dokku master. By default it’ll be auto-detected as a Node project, started, and have the given port exposed.
  12. Once your deploy is complete, it’ll print its URL. Go there to see your service go live! (Note that if you may need to update the DNS for your domain name to point towards your new Dokku server first)
  13. By default your service will be available at app-name.your-hostname. You can add more domains (such as your full root domain) by running: dokku domains:add app-name new-domain

Hopefully that’s it! With this you should have your service up and running, on your domain of choice, and trivially deployable in future with just a git push.

(Wondering what I’m running on Dokku? Watch this space.)

Image for post
Image for post

Written by

Creator of HTTP Toolkit

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store