I’d like to describe here how to set up Docker for Elixir projects.

Here is the roadmap of this configuration:

Dockerfile definition Auxiliary bash script mix.exs description your_application.ex main module

Dockerfile

Firstly, see how the Dockerfile looks like. If something is not clear enough (despite inline comments), I’ll explain some details below.

WORKDIR — according to the Docker documentation it creates and sets the current working directory

— according to the Docker documentation it and sets the current working directory --force — it installs hex and rebar in a non-interactive mode which means it won’t ask you for the confirmation

— it installs and in a non-interactive mode which means COPY — to leverage Docker caching we copy just dependencies definition firstly and, once we have them installed, we copy the rest of the files

— to leverage Docker caching we copy just dependencies definition firstly and, once we have them installed, we copy the rest of the files --only prod — we do want to compile the dependencies that are only marked as a production ones. If you want to see how to distinguish them, you may follow this stackoverflow thread.

— we do want to compile the dependencies that are only marked as a production ones. If you want to see how to distinguish them, you may follow this stackoverflow thread. docker.sh — the file is explained in the next section

Docker bash script

Let’s see how you can prepare some helper bash script to not clutter your Dockerfile with something like:

CMD one_command \

--some-flag \

argument && \

yet-another-command \

--with-flag and-argument

In this particular example you have Ecto migrations running and the most important step — building escript release.

An escript is an executable that can be invoked from the command line. An escript can run on any machine that has Erlang installed and by default does not require Elixir to be installed, as Elixir is embedded as part of the escript . This task guarantees the project and its dependencies are compiled and packages them inside an escript .

After we have the binary prepared, we can execute it as the final step.

mix.exs

We almost don’t have to change anything in the mix.exs file. There’s only one small entry to make everything running:

Before invoking mix escript.build , it is only necessary to define an :escript key with a :main_module option in your mix.exs file:

escript: [main_module: MyApp.CLI]

your_application.ex

Because a task needs to implement run function which receives a list of command line args , you need to add it like that:

Subscribe to get the latest content immediately

https://tinyletter.com/KamilLelonek

Summary