Deploying a Python Flask App on Amazon Lightsail with Dokku

One of the welcome additions to Amazon’s AWS offerings is a simplified server provisioning service to compete directly with Digital Ocean called Lightsail.  Lightsail provides a nicer web UI for launching instances, many quick launch options for common apps like WordPress or GitLab and simplified billing (yay!).  With Lightsail you don’t need to pre-pay for Reserved Instances to get a good price on an EC2 server.

Dokku is mini heroku you can run on your own servers.  It uses the same buildpacks that Heroku does to enable git push deployments. By building on top of Docker a collection of available dokku plugins make it easy to start up databases, caching or tie in other services.  In this tutorial I add Postgresql and get an SSL cert using Let’s Encrypt

Together, Lightsail and Dokku create an easy way to manage your application deployment on an inexpensive server.

Get started on Lightsail by starting up a new virtual server:

And then selecting an Ubuntu Image:

There’s a spot here for ‘Add launch script’ where you can drop in these commands to automatically install dokku on first boot:

wget https://raw.githubusercontent.com/dokku/dokku/v0.7.2/bootstrap.sh
sudo DOKKU_TAG=v0.7.2 bash bootstrap.sh

Give it a name and press Create to start booting up the server. You should be able to SSH to the new server very quickly though you can connect before dokku and package updates have been applied (it’ll take a couple minutes for the dokku command to become available)

After a couple of minutes have passed and things are installed and running visit your server in a web browser:

For the public key you’ll want to grab the key on your computer.  if you have linux or macOS you can grab the contents of ~/.ssh/id_rsa.pub.  If you need to generate a key there’s a good How-To on Github about generating them.

Set the hostname you’ll use for the server if you have one and Finish Setup.

Next step is to SSH to the server and fiddle with it there using the private key you can download from Lightsail:

ssh -i LightsailDefaultPrivateKey.pem ubuntu@<YOUR PUBLIC IP ADDRESS>

And create the app you will be deploying:

dokku apps:create your-app

Add a postgres database (there are other great plugins available for dokku too)

sudo dokku plugin:install https://github.com/dokku/dokku-postgres.git
dokku postgres:create database-name
dokku postgres:link database-name your-app

Now, back to your local app add a git remote to this new server and deploy it:

git remote add dokku dokku@<PUBLIC IP OR HOSTNAME>:your-app
git push dokku master

If that is successful then the project should be visible online. Yay!

Then there are some next steps to help complete the app. Set any environment variables you need for the app:

dokku config:set your-app ENV=prod

You can install an SSL cert using Let’s Encrypt very easily:

sudo dokku plugin:install https://github.com/dokku/dokku-letsencrypt.git
dokku letsencrypt your-app

You can configure some pre and post deploy hooks inside the app.yaml file in your project repository to run checks or execute database migrations.

That’s about it! git push deploys to update your project whenever you want.

  • lscstu22

    $ git push dokku master
    Permission denied (publickey).
    fatal: Could not read from remote repository.

    Please make sure you have the correct access rights
    and the repository exists.

  • lscstu22