Originally written for Gun.io, this post details how to use Dokku as a Heroku replacement for deploying your Django App.
What is Dokku?
A few days ago I was pointed towards the Dokku project, which is a “Docker powered mini-Heroku” that you can deploy on your own server to serve as your own private PaaS.
Why would you want your own mini-Heroku?
- Well, Heroku can cost A Lot of money; it’s hosted in the cloud and you may not want your application to leave the room just yet; and you don’t have 100% control over all aspects of the platform.
- Or maybe you’re just the DIY kind of person.
Regardless, Jeff Lindsay hacked Dokku together in less than 100 lines of bash code!
What You Need To Get Started
You could use anything from AWS to a computer on your own private network. I decided to use Digital Ocean as my cloud hosting service for this little project.
The requirements for hosting Dokku are simple:
- Ubuntu 14.10 x64
- SSH Capabilities
Digital Ocean has a pre-configured Droplet that you can use that’s already provisioned for a Dokku environment. Feel free to use this. We’ll be using a fresh server so you can recreate this process on any server, not just one on Digital Ocean.
- First, sign up for an account on Digital Ocean, making sure to add a public-key to the account. You can follow steps one through three in this guide to help get you set up if you need to create a new key. Step four will come in handy later.
- Next, create a “droplet” (spin up a node) by clicking “Create Droplet”. Make sure you pick “Ubuntu 14.10 x64” as your image. I initially picked the x32 version and Dokku wouldn’t install (see https://github.com/progrium/dokku/issues/51). Add your ssh public-key to the droplet so you can ssh into the machine without having to enter a password every time you log in. Digital Ocean takes about a minute to spin up your machine.
- After it’s ready, Digital Ocean will either email you saying so and include the machine’s IP address in the email or the machine will appear under your droplets panel. Use the IP address to SSH into the machine and follow step four in Digital Ocean’s ssh guide.
Now that our host is all set up, it’s time to install and configure Dokku. SSH back into your host machine and run the following command:
Make sure you use
sudoregardless of whether you are logged in as the root or not. For more info, see the “Wrapping Up” section below.
Installation could take anywhere from two to five minutes. Log out of your host machine when done.
Be sure to upload a public-key for the user using the following format:
<machine-address> with the IP address or the domain name of your host machine and
<your-app-name> with the name of your Django Project.
Deploying a Django Application to Dokku
For this tutorial, let’s follow the Getting Started with Django on Heroku guide to get an initial Django application set up.
Again, Dokku uses Buildstep which uses Heroku buildpacks to build your applications. It comes with the Heroku Python Buildpack built in, which is enough to run a Django or Flask application off of, right out of the box. However if you’d like to add a custom buildpack you can.
Create a Django Project and add a local Git repo:
1 2 3 4 5 6 7 8 9 10 11
We have to add Dokku on our host machine as a Git remote:
Now we can PUSH our code:
<machine-address> with the address or domain name of your host machine. If all went well, you should see an application deployed message in your terminal:
Next, go and visit
http://<machine-address>:49153 and you should see the familiar “Welcome to Django” page. Now you can work on your application locally and then push it to your own mini-heroku!.
Initially I installed Dokku without the ‘sudo’:
When I tried to push to Dokku the python build pack would fail trying to download/build python. The solution to this was to uninstall Dokku and then reinstall it the right way, using sudo:
Unfortunately, Dokku isn’t quite as far along as Heroku is.
For example, all commands need to run directly on the host server since Dokku does not have a client-side app like Heroku.
So, in order to run a command like this-
-you need to first SSH into the server. The easiest way to achieve this is to create a
Now you can run the following command to sync the database:
Dokku does allow you to configure environment variables separately for each application. Simply create/edit
/home/git/APP_NAME/ENV and fill it with things like:
Dokku is still a young platform so hopefully it keeps growing and becoming more useful. It’s open source as well so if you want to contribute, make a pull request or open an issue on Github.