Deploying a Django application can be challenging, especially when choosing the right infrastructure.
Hetzner, combined with Dokku, provides a strong and flexible solution that makes the deployment process easier.
Dokku, a platform-as-a-service (PaaS) built on Docker, lets you deploy, manage, and scale applications with ease.
This guide will show you how to deploy a Django application to Hetzner using Dokku.
What is Hetzner and Dokku?
Hetzner is a German company that offers various web hosting services, such as dedicated servers, cloud hosting, virtual private servers (VPS), and colocation services.
They are known for providing high-performance infrastructure at affordable prices, making them popular among developers, businesses, and tech enthusiasts.
Hetzner's data centers are mainly located in Germany and Finland (but also the USA and now Singapore), offering strong security, reliability, and scalable solutions.
They are particularly favored for their cost-effective cloud hosting options, making them a strong competitor in the European hosting market.
Dokku
Dokku is an open-source tool that makes it easy to deploy and manage web applications.
It uses Docker and lets you deploy apps with Git, similar to Heroku. Dokku works with many programming languages and frameworks, and it can handle databases, SSL certificates, and more.
It's lightweight and simple to set up on a server, making it a popular choice for developers who want a self-hosted solution like Heroku but with more control and flexibility.
Get "Python's Magic Methods - Beyond __init__ and __str__"
Magic methods are not just syntactic sugar, they're powerful tools that can significantly improve the functionality and performance of your code. With this book, you'll learn how to use these tools correctly and unlock the full potential of Python.
Pre-Requisites
Before we start the deployment process, make sure you have the following:
- A Django Application: Your Django app should be ready for deployment. It should include a
requirements.txt
file and aProcfile
. - A Hetzner Cloud Account: If you don't have one, you can sign up at Hetzner.
- A GitHub Account: To be able to deploy your application.
- Basic Command Line Knowledge: You should be comfortable using the terminal.
Step 1 - Create a VPS in Hetzner
Start by creating a VPS (Virtual Private Server) in the Hetzner Cloud. If you don't have an account, you can sign up here.
Once on the cloud console:
- Create a new project, let's call it
Hetzner Dokku
. - Enter the project and Add Server.
- For Location, let's choose Helsinki (eu-central).
- For Image, let's choose Ubuntu 22.04 (Dokku doesn't support 24.04 at the time of writing)
- For Type, we choose Shared CPU, x86 and CX22 (2vCPUS, 4GB RAM, 40GB SSD).
- For Networking, we leave both IPv4 and IPv6 on.
- For SSH Keys, you should add your SSH key. If you don't have one, you can generate it with
ssh-keygen
. - The other options you can leave off for now. Later on you can active a firewall if needed.
- Finally, give it a name, in this case, let's called
dokku-server
.
Then click on Create & Buy now. After a couple of seconds, your VPS should be up and running and accessible with SSH in the indicated IP address:
ssh root@<ip_address>
Step 2- Install Dokku
Now that the VPS is configured and running, you can install Dokku. Before that, it is a good policy to update the VPS, which you can do with:
apt update && apt upgrade -y
If necessary, restart the VPS.
Installing Dokku
You can now install Dokku:
wget -NP . https://dokku.com/install/v0.34.8/bootstrap.sh
sudo DOKKU_TAG=v0.34.8 bash bootstrap.sh
At the time of writing the latest version was v0.34.8. Check the Dokku website for the latest version.
This might take a couple of minutes since it also needs to pull a couple of Docker images that support the deployment process.
You should restart the server after installing it to make sure all packages are active and running.
Setup SSH key and Virtualhost Settings
For accessing applications, it is advisable to have a domain name. But you can also use the IP address with sub-domain support with https://sslip.io/.
First, you need to copy your SSH key to the Dokku SSH Admin key:
cat ~/.ssh/authorized_keys | dokku ssh-keys:add admin
Then, you can set your domain or IP address:
This article is for paid members only
To continue reading this article, upgrade your account to get full access.
Subscribe NowAlready have an account? Sign In