How To Use the Dokku One-Click DigitalOcean Image to Deploy a Python/Flask App

Note: The Dokku project has changed significantly since this guide was written. The instructions below may not reflect the current state of the Dokku project.

Introduction

A significant hurdle in developing an application is providing a sane and easy way to deploy your finished product. Dokku is a Platform as a Service solution that enables you to quickly deploy and configure an application to a production environment on a separate server.

Dokku is similar to Heroku in that you can deploy to a remote server. The difference is that Dokku is built to deploy to a single, personal server and is extremely lightweight. Dokku uses Docker, a Linux container system, to easily manage its deployments.

In this guide, we will cover how to deploy a Python/Flask app with Dokku using the DigitalOcean Dokku one-click installation image.

Step One –– Create the Dokku Droplet

The first thing we need to do is create the VPS instance that contains our Dokku installation. This is simple to set up using the DigitalOcean Dokku application.

Click on the "Create" button to create a new droplet:

Name your droplet, and select the size and region that you would like to use:

Scroll down and click on the "Applications" tab. Select the Dokku application image:

Step Two –– Access the Droplet To Complete Configuration

You can complete your Dokku configuration by accessing your VPS from a web browser.

If you configured a domain name to point to your Dokku installation, you should visit your domain name with your favorite web browser. If you do not have a domain name configured, you can use your droplet's IP address.

You will be given a simple configuration page. There are a few parts that you need to configure here.

First, check that the Public Key matches the computer that you will be deploying from. This means that if your project is on your home computer, you should use the public key that corresponds to that set up.

If you selected multiple SSH keys to embed during droplet creation, only the first will be available here. Modify it as necessary.

Next, modify the Hostname field to match your domain name. Leave this as your IP address if you do not have a domain name configured.

Choose the way that you want your applications to be referenced. By default, the application will be served like this:

http://your_domain.com:app_specific_port_number

If you select the "Use virtualhost naming for apps" check box, your apps will be accessible using a virtualhost instead:

http://app_name.your_domain.com

Click the "Finish Setup" button to complete the configuration.

Step Three –– Deploy a Sample Flask Application to your Dokku Droplet

At this point, your Dokku droplet is configured and ready to start receiving git repositories, which it will deploy automatically.

This process is as simple as pushing a local git repository to your Dokku droplet.

What Requirements do Dokku Apps Have?

Besides functioning in its own right, for your flask application to be deployed successfully with Dokku (or Heroku, for that matter), it needs to have two components to know how to deploy the app.

Procfile: A file called "Procfile" is required to be in the root application directory. The purpose of this file is to specify how the application should be run once deployed.

Basically, it specifies the commands necessary to start the web process for your project.

For many Flask projects, this can be as simple as:

web: gunicorn app_name:app

Dependencies File: You need to include a file that will tell Dokku which libraries and applications are needed to run your app. This can either be a requirements.txt file or a setup.py file.

If you do not already have a requirements.txt file and you've been using Python's pip installer to get your dependencies, you can try to auto-generate a requirements.txt file by issuing this command:

pip freeze > requirements.txt

This works best if you have built your app using virtualenv to isolate your application environment.

Deploy a Flask App Database

If the application you are deploying does not use a database, you can skip to the next section. The sample app that we will deploy will use a database.

Dokku and Heroku both adhere to a philosophy that application resources, such as databases, should be handled as separate entities so that they can easily be swapped out without changes to the application code.

Because of this, databases must be deployed separately and then associated with the application. This makes it easy to change a backend if there are problems.

Dokku handles databases through a plugin system. If your application relies on a database, you can install the related Dokku plugin, which will handle the database deployment.

We will deploy a PostgreSQL database for our application. Log into your Dokku droplet as root.

You now have the Dokku PostgreSQL installed. We will deploy a database instance to associate with our application. We can see that with our plugin installation, the available commands for Dokku have been modified:

We have some additional PostgreSQL commands available. We will have to decide right now what we want to call our application. This will have implications on the URL we will access our app from if we are using virtualhost naming.

When you know what you'd like to call your application, you can create the associated database by typing:

dokku postgres:create app_name

Your database will be created and you can now deploy your application easily.

Deploy your Flask Application

To avoid having to build an entire application complex enough to use a database, we will use a sample found on GitHub. We will clone the Flask-Social example application.

On the computer that has the RSA key that matched the one you deployed with Dokku, we will clone the repository. You will need to install git on the computer. The installation procedures will differ based on the operating system, but you can get help here.

Change into the directory where you want the project directory. Type this command:

git clone https://github.com/mattupstate/flask-social-example.git

Change into the directory that was created:

cd flask-social-example

Now, we need to add a remote that references our Dokku server and the application name we chose earlier:

git remote add name_for_remote dokku@your_domain.com:app_name

A few things to note here. The name_for_remote can be anything you want. It is simply how you're referencing your Dokku server. In many cases, it would be sensible to name it dokku.

Another thing to be sure to get right is the app_name. This must match the app_name you chose during the database creation. It is the only way that Dokku will know to tie these two pieces together.

Now, all you need to do to put your application on the Dokku server and automatically deploy it is push the repository:

git push name_for_remote master

Your application will be deployed to Dokku. When the deployment is complete, you will a message similar to this:

Once you get a handle on these few points, deploying applications to your own VPS server can happen rapidly and automatically. Dokku can help make your life easier by taking the headache out of deployment.