Deploying an Elixir Phoenix application with Docker and Makefile

Michele Rexha
3 min readJul 14, 2021

--

Photo by Colin Lloyd on Unsplash

The first part of the article shows how to dockerize your phoenix applications and add a makefile.
The rest of the article deals updating your database in production.

I recently fell in love with the elixir and most precisely the PETAL stack (Phoenix Elixir Tailwindcss Alpinejs and Liveview). At work, we needed to build an admin facing application to handle all sort of configurations for our customer facing applications.

I had some difficulties wrapping my head around production configurations, especially with regards to the .env files. Navigating elixir forum (my new best friend) I find out that a new merge request has been accepted which should solve the issues I was facing. It’s probably only a matter of time before the hex docs get updated and the problems I had become obsolete. Nonetheless, others people issues usually help to understand better a subject.

In what follows I will guide you to what I did step by step to make it work. I will finally show you the Makefile we currently use (feel free to comment any suggestion or any harsh criticism, it’s still a work in progress).

If you are anything like me, an elixir fanboy working full time in a java/javascript environment, desperate to know more but sometimes too busy to do, I hope this article will help shading some lights on your doubts.

This tutorial assumes some familiarity with:

The 5 steps

First, I remove the Mix.Config macro which is deprecated in favor of Config on your config files

Then, move the prod.secret.exs to runtime.exs file (or simply rename prod.secret.exs to runtime.exs) and just wrap it around an if env == prod condition

Add your .env variables

Copy your Dockerfile

Finally, add the Makefile

If you copy and paste the above files, you must replace your_otp_app_name , YourApp and YourAppWeb with the appropriate names, moreover, you must be careful not overwriting other configurations.

You might want to check which port you are exposing and whether or not you are using https. In that case, you might want to do some changes in your configs. In our case, we use a Nginx reverse proxy so we do not need ssl certificates on the app.

Run migrations in production

Do remember that Phoenix applications manage your database schema for you. Therefore, if your production database differs from your dev db (which I truly hope does), you will need to run migrations once you build your new image with Docker. The caveat is that you won’t have access to the mighty mix commands. As usual, the fabulous hex docs got us covered.

Start by adding `lib/your_otp_app_name/release.ex to your codebase

Build your image with the Makefile command build

make build VERSION=X.X.X

Run migrations

docker stop your_container
make shell
cd bin && ./your_otp_app_name eval "YourApp.Release.migrate"

Restart the container

make restart

Et voilà! I hope this was helpful as it would have been helpful to me! Keep it up and spread love for Elixir!

--

--