1

按照本指南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}type 来获取环境变量,但不是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 1.5.2 和 Phoenix 1.3。版本问题?

4

1 回答 1

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"}
于 2017-11-15T05:23:12.407 回答