online
online

Reputation: 5527

How to set environment variables in Phoenix with Distillery library?

Follow this guide to use Distillery to release a elixir/phoenix project:

https://blog.polyscribe.io/a-complete-guide-to-deploying-elixir-phoenix-applications-on-kubernetes-part-1-setting-up-d88b35b64dcd

At the set config/prod.exs step, the author wrote:

config :myapp, Myapp.Repo,
  adapter: Ecto.Adapters.Postgres,
  hostname: "${DB_HOSTNAME}",
  username: "${DB_USERNAME}",
  password: "${DB_PASSWORD}",
  database: "${DB_NAME}",

to config database. Here using ${DB_HOSTNAME} type to get environment variables, but not System.get_env("DB_HOSTNAME").

However, when I run MIX_ENV=prod mix release --env=prod and set environment variables local:

REPLACE_OS_VARS=true PORT=4000 HOST=0.0.0.0 SECRET_KEY_BASE=highlysecretkey DB_USERNAME=postgres DB_PASSWORD=postgres DB_NAME=myapp_dev DB_HOSTNAME=localhost ./_build/prod/rel/myapp/bin/myapp foreground

It loops:

12:05:13.671 [error] Postgrex.Protocol (#PID<0.1348.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (${DB_HOSTNAME}:5432): non-existing domain - :nxdomain
12:05:13.671 [error] Postgrex.Protocol (#PID<0.1347.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (${DB_HOSTNAME}:5432): non-existing domain - :nxdomain
12:05:13.672 [error] Postgrex.Protocol (#PID<0.1344.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (${DB_HOSTNAME}:5432): non-existing domain - :nxdomain
12:05:13.672 [error] Postgrex.Protocol (#PID<0.1346.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (${DB_HOSTNAME}:5432): non-existing domain - :nxdomain
...

It seems ${DB_HOSTNAME} didn't been known by elixir/phoenix.

I am using Elixir 1.5.2 and Phoenix 1.3 now. The version problem?

Upvotes: 1

Views: 1138

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121010

I never saw this approach and I am unsure how it is supposed to work. The common way to getting the environment variables at runtime would be:

{:system, "VAR"}

For your case it’d be:

config :myapp, Myapp.Repo,
  adapter: Ecto.Adapters.Postgres,
  hostname: {:system, "DB_HOSTNAME"},
  username: {:system, "DB_USERNAME"},
  password: {:system, "DB_PASSWORD"},
  database: {:system, "DB_NAME"}

Upvotes: 1

Related Questions