Deploy a Flask Application with Dokku
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Dokku is a self-hosted Platform-as-a-Service (PaaS) that makes deploying applications simple using Git. Although Dokku’s implementation is similar to Heroku, it lacks certain key features such as auto-scaling. Dokku is an extremely powerful tool that automatically runs your application inside Docker and requires minimal configuration of web servers.
This guide demonstrates how to:
- Create a Flask application that returns ‘Hello World!’ on the index page
- Install Dokku on a Linode
- Deploy a Flask application with a WSGI server inside a Docker container
- Add an SSL certificate through Dokku with the Let’s Encrypt plugin
A public key is assumed to be available. Typically this is located in
Install Git if needed:
sudo apt install git
The Dokku install script creates a
dokku user on the system, installs Docker, and pulls the relevant image.
Download the install script from Dokku then run the script:
wget https://raw.githubusercontent.com/dokku/dokku/v0.12.5/bootstrap.sh sudo DOKKU_TAG=v0.12.5 bash bootstrap.sh
Preparing to install v0.11.6 from https://github.com/dokku/dokku.git... For dokku to build containers, it is strongly suggested that you have 1024 megabytes or more of free memory If necessary, please consult this document to setup swap: http://dokku.viewdocs.io/dokku/advanced-installation/#vms-with-less-than-1gb-of-memory --> Ensuring we have the proper dependencies --> Initial apt-get update --> Installing docker --> NOTE: Using Linode? Docker may complain about missing AUFS support. You can safely ignore this warning. Installation will continue in 10 seconds. ...
Navigate to the public IP address of your Linode in a browser and enter the public key:Add the public key immediately after running the installation script to avoid someone else adding a public key to Dokku. For an unattended installation, refer to the advanced installation instructions.
To add additional SSH keys, pipe the output over SSH to the
example.comwith the IP address of your Linode.
cat ~/.ssh/id_rsa.pub | ssh firstname.lastname@example.org ssh-keys:add new-key
On your local computer, create a new project directory:
mkdir flask-example && cd flask-example
Create a new file called
hello_world.pythat serves ‘Hello World!’ on the index page.
- File: hello_world.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14
import os from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return 'Hello World!' if __name__ == '__main__': # Bind to PORT if defined, otherwise default to 5000. port = int(os.environ.get('PORT', 5000)) app.run(host='127.0.0.1', port=port)
requirements.txtfile to track versions of any dependencies of the Flask application. Gunicorn is the WSGI server used to allow Flask to interface properly with NGINX.
- File: requirements.txt
For more complex projects with many dependencies using a virtual environment, redirect output of
pip freeze > requirements.txt
Optionally, add a
.gitignore file to have Git omit caching and virtual environment files from version control.
- File: .gitignore
1 2 3 4
__pycache__/ *.pyc venv/
The Procfile tells the Gunicorn server what command to use when launching the app:
- File: Procfile
web: gunicorn hello_world:app --workers=4
Initialize a Git repository:
git init git add . git commit -m "Deploy Flask with Dokku"
Add a remote named
dokkuwith the username
example.comwith the public IP address of your Linode:
git remote add dokku email@example.com:flask-example
Verify the remote is added:
git remote -v
This will list the remotes.
dokku dokku@example-ip:flask-example (fetch) dokku dokku@example-ip:flask-example (push)
In summary, the project layout looks like:
flask-example ├── .gitignore ├── Procfile ├── hello_world.py └── requirements.txt
SSH into your Linode and create the application:
dokku apps:create flask-example
Make sure VHOST is enabled.
dokku domains:enable flask-example
On your local computer, deploy the Flask application by pushing the branch to the
dokkuremote. This will take care of NGINX behind the scenes and expose port
git push dokku master
Other local branches can also be deployed but, all branches must be pushed to the master branch of the
git push dokku branch-name:master
curlthe IP address of your Linode to test that the app was deployed successfully:
The remaining steps in this guide should be performed from your Linode.
Install the Let’s Encrypt plugin for Dokku:
sudo dokku plugin:install https://github.com/dokku/dokku-letsencrypt.git
DOKKU_LETSENCRYPT_EMAILenvironment variable to the email for Let’s Encrypt:
dokku config:set flask-example DOKKU_LETSENCRYPT_EMAILfirstname.lastname@example.org
Add the application and domain:
dokku domains:add flask-example example.com
Create the SSL certificate. NGINX will automatically start serving the application over HTTPS on port 443:
dokku letsencrypt flask-example
Run this as a cron job so the certificate will renew automatically:.
dokku letsencrypt:cron-job --addThis requires Dokku version 0.5 or higher. Check by running
List all running Dokku applications:
Restart an application:
dokku ps:restart flask-example
Stop an application:
dokku ps:stop flask-example
Restore all applications after a reboot:
View the application logs through Dokku or the Docker container.
To see logs through Dokku:
dokku logs flask-example
List all running Docker containers:
sudo docker ps -a
Find the container ID then run:
sudo docker logs container_id
Dokku does not scale applications automatically, and by default will only run a single
web process. To increase the number of containers running your application, you can use the
Check how many workers your application currently has:
dokku ps:scale flask-example
-----> Scaling for flask-example -----> proctype qty -----> -------- --- -----> web 1
Scale up to 4
dokku ps:scale flask-example web=4
Confirm that the new processes are running:
-----> Scaling for flask-example -----> proctype qty -----> -------- --- -----> web 4
Dokku is an open source alternative to Heroku for small applications. Deploying applications is as simple as pushing to a remote with Git. Elements such as Docker and NGINX are abstracted away to minimize time to deployment. There are additional features such as pre-deploy hooks and linking databases which are not shown in this guide.
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on