【问题标题】:How to set environment variables in Phoenix with Distillery library?如何使用 Distillery 库在 Phoenix 中设置环境变量?
【发布时间】:2017-11-15 13:23:12
【问题描述】:

按照本指南使用 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 并在本地设置环境变量时:

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

似乎${DB_HOSTNAME} 不为 elixir/phoenix 所知。

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

【问题讨论】:

  • 原因是我现在使用zsh shell。应该使用export来设置环境变量。

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


【解决方案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"}

【讨论】:

  • 谢谢。我用这个指南做:blog.polyscribe.io/…。原因是我没有正确设置本地环境变量。
  • 优选/惯用的方式仍然是{:system, ...} 元组,即使"$..." 有效。
猜你喜欢
  • 2017-11-14
  • 2017-12-10
  • 2016-02-04
  • 2015-10-24
  • 2012-11-22
  • 2019-08-24
  • 2013-06-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多