Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With Elixir/Phoenix (0.10.0), how do I set the port based on the PORT environment variable in a release?

In config/test.exs, I have the following lines:

config :youli, Youli.Endpoint,
  http: [port: System.get_env("PORT") || 4001

When I run mix release, untar the release, and run the app with PORT=4242, it runs on port 4001 instead. Grepping a bit, I found that it's hard-coded that way in releases/0.0.3/sys.config.

How can I get my release to run with the port set from the environment?

like image 500
ijt Avatar asked Dec 25 '22 21:12

ijt


1 Answers

Instead of System.get_env("PORT"), use {:system, "PORT"}:

$ git diff
diff --git a/phoenix/config/test.exs b/phoenix/config/test.exs
index 10cea91..617f34c 100644
--- a/phoenix/config/test.exs
+++ b/phoenix/config/test.exs
@@ -1,7 +1,7 @@
 use Mix.Config

  config :youli, Youli.Endpoint,
  -  http: [port: System.get_env("PORT") || 4001]
  +  http: [port: {:system, "PORT"}]

The documentation for this is in lib/phoenix/endpoint.ex in the Phoenix source.

like image 54
ijt Avatar answered Jan 19 '23 00:01

ijt