Minimal Phoenix and Elixir Dockerfile Example

Recently, I was setting up a Dockerfile for a Phoenix web app. There are official Elixir Docker images, but finding one specifically set up to handle the Phoenix Framework with asset compilation is messier territory. There's no single set of "official" configurations.

Everybody has an opinion about how you should do this. A lot of these opinions involve adding a lot of bells and whistles. For one, their preferred solution might include Docker Compose. For another, Distillery releases. Since I had not successfully deployed my app with Docker yet at all, I wanted fewer variables to debug.

Here's what I came up with:

1FROM elixir:1.8.0-alpine
2
3#### If needed for native dependencies
4RUN apk add --no-cache make gcc libc-dev
5
6ENV CC=gcc
7ENV MAKE=cmake
8#####
9
10RUN mix local.hex --force \\
11 && mix local.rebar --force
12
13WORKDIR /root/app
14
15ADD ./ /root/app/
16
17EXPOSE 4000
18
19ENV MIX_ENV=prod
20ENV PORT=4000
21
22RUN mix deps.get
23RUN mix deps.compile
24RUN mix compile
25RUN mix phx.digest
26
27CMD ["mix", "phx.server"]

This starts with the elixir alpine image. Alpine is a skinny Linux that works well in containers. I've found that it is a suitable base for Elixir apps. In my case, I needed a C toolchain to compile some libraries. You might not need that part. Then it sets up hex and rebar for fetching and building dependencies. Then it adds the application directory. It sets the default port and environment. It fetches the dependencies, compiles them, compiles the app, and digests the assets. Then, it starts the server. That's it.

This approach follows the minimal instructions for a production Phoenix deployment on Docker, with no extras. From there, you can add ~complexity~ more features if you would like.

Bonus: .dockerignore for Phoenix and Elixir Projects

Here's the .dockerignore file I use:

1 .git
2 Dockerfile
3
4 # Build artifacts
5 _build
6 deps
7 *.ez
8
9 # Crash dumps from Erlang VM
10 erl_crash.dump
11
12 # NPM dependencies added by asset pipeline
13 node_modules