Elixir on Heroku with Docker Containers
It is possible to deploy your Elixir application on Heroku using the Elixir Buildpack . However, you may want to deploy using Docker on Heroku instead. Perhaps your application has complex setup or configuration needs. Or perhaps you already have a Docker-ified application and you don’t want to have to do extra work to convert it to buildpacks and Procfiles.
Heroku supports Docker via the Container Registry and Runtime. Here’s the process of getting it set up:
-
Create a Heroku application with
heroku create
. -
Set the Heroku stack to “container”
heroku set:stack container
. This enables the container functionality and ensures that Heroku doesn’t become confused if your project also contains other manifests (such as package.json or Gemfile). -
Set up your Docker environment to talk to Heroku’s Docker registry instead of the default Docker registry:
heroku container:login
. -
Build the image and push it to the Heroku registry:
heroku container:push web
. -
Deploy it to your dynos:
heroku container:release web
. - Check your site and make sure everything works.
-
If your application does not boot and logs the error message
Shall I install Hex? (if running non-interactively, use "mix local.hex --force") [Yn] ** (Mix) Could not find an SCM for dependency :phoenix from
, you may have to make a small change to your Dockerfile..Mixfile
- Add ENV MIX_HOME=/opt/mix before you install hex & rebar in your Dockerfile. The reason this is needed from what I can gather is that Heroku’s Docker execution environment is different from your local development environment.
For reference, here’s my full Dockerfile for my Phoenix application:
1
FROM elixir:1.8.0-alpine
2
3
RUN apk add make gcc libc-dev
4
5
ENV CC=gcc
6
ENV MIX_HOME=/opt/mix
7
8
RUN mix local.hex --force \\
9
&& mix local.rebar --force
10
11
WORKDIR /root/app
12
13
ADD ./ /root/app/
14
15
EXPOSE 4000
16
17
ARG MIX_ENV=prod
18
RUN echo ${MIX_ENV}
19
ENV MIX_ENV=$MIX_ENV
20
ENV PORT=4000
21
22
RUN mix deps.get
23
RUN mix deps.compile
24
RUN MAKE=cmake mix compile
25
RUN mix phx.digest
26
27
CMD ["mix", "phx.server"]