Configure your Phoenix application to develop in Docker

Written on October 19, 2023

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, is what’s called a loopback address. What this means is only local connections can be made.

Meanwhile, 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.

Stay in touch

Thanks for reading this article. I'd love to stay in touch and share more tips on programming and side projects with you. Sign up and I'll send you my articles straight to your email, you'll also get a free copy of the light themed version of my Git cheat sheet.
Git cheat sheet preview image