Blog Case studies Services
Michael Rode
Cook of tasty food, Coder of loved Elixir, Creator of products and dad of a wonderful family.

deployment docker elixir phoenix dokku

Deploying Elixir and Phoenix with Dokku and Docker - coming soon

Looking to deploy your Elixir and Phoenix app the smart way? Here’s a streamlined guide to get you up and running with **Dokku**, covering everything from setting up your VPS to automating deployments. Let’s dive in:

##Prerequisites

1. Deploying to a VPS

Start by spinning up a VPS (e.g., Hetzner). Install Dokku and set up your domain. https://dokku.com/docs~v0.35.13/getting-started/installation/

Create a new Dokku app

First, you need to create a new Dokku app on your Dokku instance. https://dokku.com/docs/deployment/application-deployment/ Login to your Dokku instance and run the following command:

dokku apps:create app-name
dokku domains:set app-name app-name.com

Setup Postgres

# on the Dokku host
# install the postgres plugin
# plugin installation requires root, hence the user change

sudo dokku plugin:install https://github.com/dokku/dokku-postgres.git

# create a postgres service with the name `app-name-postgres-db`

dokku postgres:create app-name-postgres-db

Linking backing services to applications

# on the Dokku host
# each official datastore offers a `link` method to link a service to any application
dokku postgres:link app-name-postgres-db app-name

2. Dockerizing Your Elixir App with releases

Build a Dockerfile tailored to your app. Use mix phx.gen.release --docker to generate releases. This ensures your app runs smoothly in a containerized environment. A release configuration with a Dockerfile is generatesd by running the following command:

  mix phx.gen.release --docker
  git add -A
  git commit -m "Generated release"

Configuration

Port Forwarding to Docker Container

dokku ports:add app-name http:80:80
dokku ports:add app-name https:443:80

Configuring environment variables

mix phx.gen.secret
dokku config:set app-name SECRET_KEY_BASE=your_secret_key_base
dokku config:set app-name PHX_HOST=app-name.com

Applications not using EXPOSE

Any application that does not use an EXPOSE directive will result in Dokku defaulting to port 5000. This behavior mimics the behavior of a Buildpack deploy. Phoenix supports the PORT environment variable, then you will need to set the PORT variable:

dokku config:set app-name PORT=4000

modify your application to support the PORT environment variable. switch to using an EXPOSE directive in your Dockerfile.

Deploying Your Dockerized App

git remote add dokku dokku@app-name.com:app-name
git push dokku main:main

3. Post-Deploy Scripts

Add custom scripts to automate tasks like database migrations post-deployment. Save time and avoid manual intervention.

4. Adding Let’s Encrypt for SSL

Secure your app with HTTPS using Dokku’s built-in Let’s Encrypt plugin. Free SSL certificates, automated renewals—done!

Setup SSL

To setup SSL, we will use Let’s Encrypt. Dokku has a Let’s Encrypt plugin for this. Install the plugin by running the following command on your Dokku machine if not already done:

Install Let’s Encrypt plugin

dokku plugin:install https://github.com/dokku/dokku-letsencrypt.git
dokku letsencrypt:set --global email your@email.tld

Setup SSL for domain

dokku letsencrypt:enable app-name
dokku letsencrypt:cron-job --add app-name

5. Backups to S3

Keep your data safe by configuring automatic backups to an S3-compatible storage. Dokku’s backup plugins make this simple.

6. Automated GitHub Deploys

Set up a GitHub Actions workflow for zero-click deployments. Push your code, and let your CI/CD pipeline handle the rest.

Other solutions

https://elixirforum.com/t/deploy-your-phoenix-app-on-digitalocean-in-30-minutes/65738