Setting up Phoenix + PostgreSQL with docker-compose for local development

Ricardo Trindade
Aug 1 · 2 min read

I’ve been learning Elixir recently and enjoying it so far, coming from a Rails background it’s really similar to Ruby. As I’m a Docker fan I had to configure everything up to be able to develop with it.

Sharing my current configuration for local development with Elixir 1.9.1, and Postgres 11.4 all tied up with docker-compose.
I’ve adapted the configuration from previous posts on Medium, namely this one.

I’ll be creating a new Phoenix project in API only mode without any of the UI parts in order to reduce the complexity of the Dockerfile.

So the first step would be to create a new Phoenix project.
mix --no-html --no-webpack

This simplifies the process as I don’t need to install Node.

After that we can move on to the Dockerfile. I’ll be using the latest version from Elixir slim but I’m planning on moving to alpine to make it lighter.

And as for docker-compose:

Notice that we’re executing a shell script which is as follows:

A possible alteration that you’ll have to make is to change the database host on dev.exs and test.exs from localhost to db. I was facing some connectivity issues between the postgres container and the application one until I made this change.

And that’s it, go ahead and run docker-compose up -d and then you can log into the container with docker exec -it web bash , and then iex -S mix.Any changes you made to the elixir files are propagated in the container and you’ll just have to hit recompile and you’re good to go.

Ricardo Trindade

Written by

Backend engineer at Drover. Keen interest in machine learning, Ruby and Kotlin

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade