Deploying a Phoenix application to Dokku
Introduction
In this tutorial I’m going to show you how easy it is to deploy a Phoenix web application on a server running Dokku.
As an extra bonus, we will also configure our application to use a valid SSL certificate with just a few extra commands.
Prerequisites
- Server running Dokku with SSH configured to connect remotely (I highly recommend using Digital Ocean’s 1-click solution)
- A domain name pointing at your Digital Ocean VPS with a wildcard A record set
- A recent version of Git installed locally
1. Clone the sample application
First, we will need a sample application to deploy to our server. So we will be using Chris McCord’s Phoenix Chat Example.
You can either fork and clone or simply clone this project locally like so:
Once we have the project on our machine, we are going to add a new remote URL to the repository.
This will set a second remote URL to the repository and allow us to push our updates to our Dokku server. We can see all remotes by running:
2. Configure the application for production
Next, we will have to make a small change to our sample application so that it will work correctly in our production environment.
Since this application does not use a database the only change we require is setting the host
and secret_key_base
to use environment variables we will configure shortly. To fix this, in config/prod.exs
replace:
With:
We also need to replace the hardcoded secret key with an environment variable. Open up config/prod.secret.exs
and replace:
With:
In the same file if we had a database to configure, we would also change:
To:
This DATABASE_URL
environment variable is given to us by Dokku when we link a Postgres database to our application.
3. Buildpacks
In order for Dokku to know how to install Elixir and Javascript dependencies and how to run our application, we need to tell it to use an Elixir buildpack.
Fortunately all the hard work of creating these has been done by productive members of the Elixir community.
We need a combination of 2 buildpacks in order to deploy our application. One for Elixir/Phoenix and another for our static assets. To use both of these buildpacks we first create a file in the root of our project named .buildpacks
and add the following lines:
We then configure our phoenix-static
buildpack to use a later version of Node required by Phoenix 1.1 and above.
To do this we create a file phoenix_static_buildpack.config
and add the line below:
4. Create the Dokku App
We are now ready to create the application in Dokku. We can do this via the dokku-cli
gem, but for now we’ll just SSH into our server to configure the application.
We will also add the environment variables we setup before:
4b. Create a database (optional)
Creating a databse on Dokku is very straightforward. First we install the Postgres Dokku plugin if we don’t have it already:
Then we create the database itself:
We can then link this container to our application with:
The format is postgres:link <name> <app>
where name
is the name of the database and app
is the name of the application.
We can also see that our DATABASE_URL
environment variable has been set and is available to our application.
5. Push the application
Now on our local machine, we can now push the application to Dokku which will configure and deploy it:
If we navigate to http://phoenix.mydomain.com we will see the sample application running.
Pretty sweet.
6. Bonus - SSL Encryption
Previously, adding SSL to an application was a sometimes tedious and pricey endeavour. But thanks to the folks at Let’s Encrypt the process has been simplified enormously.
Let’s add SSL to our sample application.
To do this we need to install the Let’s Encrypt plugin for Dokku. So on your server run:
We then need to set an environment variable for our email, which is required to issue an SSL certificate:
Now to add a certificate to your application we simply run:
Now we can visit our site securely at https://phoenix.mydomain.com and see the valid SSL certificate in action: