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
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
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
- Copy the files from this repo into your Phoenix app (choose the database or non-database version)
- Edit the repo name, app name, and endpoint name in
config/prod.exs
andconfig/runtime.exs
- Set your environment variables using
.envrc.sample
as a template. Copy.envrc.sample
to.envrc
if you're usingdirenv
, or to a.env
file, or to however you're specifying environment variables. - 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
andHEX_PRIVATE_ORG_READ_ONLY_KEY
(only one private org is currently supported). - 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) - DO NOT forget to set
server: true
inconfig/prod.exs
for your endpoint! - 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:
- Set
DATABASE_SSL
tofalse
, andDATABASE_URL
topostgres://postgres:postgres@host.docker.internal/sample_phoenix_app_with_postgres_db_dev?sslmode=disable
- Add and modify the file
lib/<your_app_name>/release.ex
for running migrations
Note that the Dockerfile should not require any edits.
Here are the steps required to deploy on Digital Ocean's App platform (after you have copied the required files into your project):
- If you want to customize your BEAM VM parameters, run
mix release.init
and customize the files in therel
directory - Create an app on Digital Ocean
- Set the environment variables shown in
.envrc.sample
in Overview -> Components -> Environment Variables - Create a CNAME in your DNS provider for the new Digital Ocean app domain name.
- Set the environment variable
CANONICAL_HOST
in Digital Ocean to be this new CNAME value. - Under the Digital Ocean Apps -> Settings -> Domains & Certificates, add the new CNAME (this will cause a certificate to get auto-generated by Digital Ocean).
- 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 variableDATABASE_URL
to${db.DATABASE_URL}
DATABASE_SSL
can either be omitted, or should be set tofalse