DEV Community

Cover image for Personal note: Dokerize Elixir App
Burhanuddin Ahmed
Burhanuddin Ahmed

Posted on

Personal note: Dokerize Elixir App

After we have completed creating our Elixir CRUD app, now we want to wrap it into a container. We will use Docker which is a popular tool to container the app.

To container it, we need to create a dockerfile in root app.

We can add .dockerignore. It's optional but ok if we have it.

.dockerignore

# Elixir Artifacts
/_build/
/deps/
/doc/
/cover/
/.fetch
*.ez
APPNAME-*.tar
erl_crash.dump


# Node Artifacts
npm-debug.log
/assets/node_modules/
/priv/static/


# File uploads
/uploads
/test/uploads


# Docker only
/test/
/.iex.exs
Enter fullscreen mode Exit fullscreen mode

OK, after we add it now we can add dockerfile.

dockerfile

FROM elixir:1.15.1 as builder
WORKDIR /app
COPY . .
ENV MIX_ENV=prod
COPY lib ./lib
COPY mix.exs .
COPY mix.lock .
COPY config/prod.env.exs config/

RUN mix local.rebar --force \
    && mix local.hex --force \
    && mix deps.get \
    && mix release

EXPOSE 8081

CMD ["_build/prod/rel/todo_app/bin/todo_app", "start"]
Enter fullscreen mode Exit fullscreen mode

If we want more advanced dockerfile, we can use multi-stage build dockerfile. By using multi-stage build we can have lighter image because it will only contain Elixir distribution file.

But in this post, We will only provide single-stage build for more simplicity.

Happy Coding!!! 🍻

Top comments (0)