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