如何使用Distillery库在Phoenix中设置环境变量?

时间:2017-11-15 03:15:45

标签: environment-variables elixir phoenix-framework release distillery

按照本指南使用Distillery发布elixir / phoenix项目:

  

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

在设定的config/prod.exs步骤中,作者写道:

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

配置数据库。这里使用${DB_HOSTNAME}类型来获取环境变量,但不是System.get_env("DB_HOSTNAME")

但是,当我运行MIX_ENV=prod mix release --env=prod并将环境变量设置为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

循环:

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
...

elixir / phoenix似乎并不知道${DB_HOSTNAME}

我现在正在使用Elixir 1.5.2和Phoenix 1.3。版本问题?

1 个答案:

答案 0 :(得分:1)

我从未见过这种方法,我不确定它应该如何工作。在运行时获取环境变量的常用方法是:

{:system, "VAR"}

对于你的情况,它是:

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