I’ve recently run into the issue where I want to run two different Phoenix apps
at the same time on my local machine. I wanted to run one app on
port 4000 and
another app on
I thought that I was going to be able to do something like:
That didn’t work, so I dove into the docs
to see what I could do with
mix phoenix.server. Unfortunately, there are no command-line
arguments for passing in the port.
Doing more Internet research, I saw a suggestion by Chris McCord about how to override the default port of 4000 on a Phoenix App here.
There are two solutions to override the default port of 4000. The solution you pick depends on what you need.
Solution 1: Replace the hardcoded port in config/dev.exs with a new port number
Looking at a sample app, port is hardcoded as 4000.
You can change this to
4001 and running
mix phoenix.server will start your app on
This solution works if you know you want to run a specific app on a specific port every single time and keeps your starting command neat.
Solution 2: Dynamically override the port in config/dev.exs with an environment variable
To take this one step further, you can use an environment variable to set the port. To do this, you can replace 4000 here like this:
And then, when running your Phoenix server, you can do the following:
Phoenix will now use the environment variable port number you pass in to run your app and will default to 4000 if you do not pass an environment variable in. This makes solution 2 more flexible than solution 1.