An elegant way to develop an Elixir application is to use Docker. This lets your have a development environment without the need to use asdf
or installing the entire Elixir toolchain on your machine.
Getting a Container up and running is as simple as using this command:
docker run -it -v $(pwd):/app -p 4000:4000 --rm elixir bash
However, if you point your browser to localhost:4000
it doesn’t open the Phoenix application.
To get this to work, you’ll have to configure your Phoenix application so that you can see the application running in Docker in your local machine.
You do this by going to config/dev.exs
in your project.
Then you’ll want to change this line:
http: [ip: {127, 0, 0, 1}, port: 4000],
To this:
http: [ip: {0, 0, 0, 0}, port: 4000],
We do this because, 127.0.0.1
is what’s called a loopback address. What this means is only local connections can be made.
Meanwhile, 0.0.0.0
is what’s called a wildcard address. This tells the server to listen for incoming connections from other machines.
In our case, our browser on our desktop is a different machine making a connection to the application in “another machine” in Docker.