kgpdeveloper
kgpdeveloper

Reputation: 2119

Running iex mix phoenix.server on a different port does not work

This looks like a bug but I am not sure. Can anyone tell if running iex on different port number works. It always uses port 4000.

PORT=4001 iex -S mix phoenix.server   

Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

[info] Running AppMessenger.Endpoint with Cowboy using http on port 4000
Interactive Elixir (1.2.5) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> 19 May 19:53:40 - info: compiled 5 files into 2 files, copied 3 in 714ms

Upvotes: 2

Views: 1394

Answers (2)

mlambie
mlambie

Reputation: 7487

On a modern Phoenix installation, you can control this behaviour with environment variables.

Within config/dev.exs change:

http: [
  ip: {127, 0, 0, 1}, 
  port: 4000
]

to

http: [
  ip: {127, 0, 0, 1}, 
  port: String.to_integer(System.get_env("PORT") || "4000")]
]

Use direnv with a .envrc file in the Phoenix project root directory to set the port:

export PORT=4001

Upvotes: 0

Dogbert
Dogbert

Reputation: 222318

By default, config/dev.exs has a hardcoded value 4000 for the port in Phoenix. If you want to use the value from PORT environment variable in dev, in config/dev.exs change:

http: [port: 4000]

to

http: [port: {:system, "PORT"}]

Upvotes: 7

Related Questions