Skip to content

geometerio/elixir-phoenix-dockerfile-examples

Repository files navigation

Elixir/Phoenix Dockerfile Examples

The files in this repo are what you need to copy into your Elixir/Phoenix project (based on whether your Phoenix app has a database or not) in order to get it to run on a service such as Digital Ocean's App Platform. Note that the Dockerfile is identical in both cases, and simply requires that the appropriate environment variables be set (the list of variables and example values are given in .envrc.sample). One can also access Hex packages published by a private org by setting the appropriate environment variables (HEX_PRIVATE_ORG and HEX_PRIVATE_ORG_READ_ONLY_KEY).

This Dockerfile assumes you are deploying on Alpine Linux (it uses the apk package manager).

You can search for the tag CHANGE ME! to find the few items that require custom edits.

One can use whatever base Docker image one wants for the Elixir build image and the app runner image, but it is recommended that one picks images which have specific versions for Elixir, Erlang, and Linux; e.g.,

export APP_BUILDER_ELIXIR_DOCKER_IMAGE=hexpm/elixir:1.11.2-erlang-23.1.2-alpine-3.12.1
export APP_RUNNER_DOCKER_IMAGE=alpine:3.12.1

in lieu of

export APP_BUILDER_ELIXIR_DOCKER_IMAGE=hexpm/elixir:latest
export APP_RUNNER_DOCKER_IMAGE=alpine:latest

Sample Phoenix App without DB

See the full repo here. The parts which have been edited (and are not Phoenix auto-generated boilerplate) have been extracted out into the subdirectory sample_phoenix_app_without_db

Sample Phoenix App with Postgres DB

See the full repo here. The parts which have been edited (and are not Phoenix auto-generated boilerplate) have been extracted out into the subdirectory sample_phoenix_app_with_postgres_db

Creating the Docker image and running the container locally

  1. Copy the files from this repo into your Phoenix app (choose the database or non-database version)
  2. Edit the repo name, app name, and endpoint name in config/prod.exs and config/runtime.exs
  3. Set your environment variables using .envrc.sample as a template. Copy .envrc.sample to .envrc if you're using direnv, or to a .env file, or to however you're specifying environment variables.
  4. Get the Hex private key from hex.pm if you're going to be accessing hex packages published by a private org, and specify HEX_PRIVATE_ORG and HEX_PRIVATE_ORG_READ_ONLY_KEY (only one private org is currently supported).
  5. Use the scripts in bin/docker to create the Docker image locally, and also to run it (you'll need to have your environment variables set up properly first)
  6. DO NOT forget to set server: true in config/prod.exs for your endpoint!
  7. If you need to modify how to start the app (such as running migrations first before starting the actual app), create a file rel/overlays/bin/start_script. See here for an example.

If you have a Postgres database, you'll need to do these additional steps:

  1. Set DATABASE_SSL to false, and DATABASE_URL to postgres://postgres:postgres@host.docker.internal/sample_phoenix_app_with_postgres_db_dev?sslmode=disable
  2. Add and modify the file lib/<your_app_name>/release.ex for running migrations

Note that the Dockerfile should not require any edits.

Deploying on Digital Ocean's App Platform

Here are the steps required to deploy on Digital Ocean's App platform (after you have copied the required files into your project):

  1. If you want to customize your BEAM VM parameters, run mix release.init and customize the files in the rel directory
  2. Create an app on Digital Ocean
  3. Set the environment variables shown in .envrc.sample in Overview -> Components -> Environment Variables
  4. Create a CNAME in your DNS provider for the new Digital Ocean app domain name.
  5. Set the environment variable CANONICAL_HOST in Digital Ocean to be this new CNAME value.
  6. Under the Digital Ocean Apps -> Settings -> Domains & Certificates, add the new CNAME (this will cause a certificate to get auto-generated by Digital Ocean).
  7. If you are using a Digital Ocean Postgres database (and assuming you stuck with the default name of db for the database), then set the environment variable DATABASE_URL to ${db.DATABASE_URL}
  8. DATABASE_SSL can either be omitted, or should be set to false

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published