Unable to compile default Elixir project from the getting started guide

NOTE: Found a solution to the problem I was having. See edit2 note below.

I’ve been struggling with this issue for a few hours now. I’m on macOS and the project runs just fine on my machine, but to deploy (to AWS), I need to build a docker image passing --platform=linux/amd64, e.g.:

ARG ELIXIR_VERSION=1.17.2
ARG OTP_VERSION=27.0.1
ARG NODE_VERSION=20.x
ARG DEBIAN_VERSION=bullseye-20240701-slim

ARG BUILDER_IMAGE="hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-debian-${DEBIAN_VERSION}"
ARG RUNNER_IMAGE="debian:${DEBIAN_VERSION}"

# ---- stage: builder
FROM --platform=linux/amd64 ${BUILDER_IMAGE} AS builder

The Elixir version I have there is quite new, but I had it set to 1.16 before and it presented the same issue. When I try to build, this is the error I get:

 => [builder  8/18] COPY mix.exs mix.lock ./                                                                                             0.0s
 => [builder  9/18] RUN mix deps.get --only prod                                                                                         3.9s
 => ERROR [builder 10/18] RUN mix deps.compile                                                                                           1.7s
------
 > [builder 10/18] RUN mix deps.compile:
1.165 ==> decimal
1.165 Compiling 4 files (.ex)
1.669 Generated decimal app
1.675 ==> my_app_name
1.676 Error while loading project :mime at /app/deps/mime
1.678 ** (ArgumentError) could not call Module.put_attribute/3 because the module MyAppName.MixProject is already compiled
1.678     (elixir 1.17.2) lib/module.ex:2365: Module.assert_not_readonly!/2
1.678     (elixir 1.17.2) lib/module.ex:2046: Module.__put_attribute__/5
1.678     /app/deps/mime/mix.exs:2: (module)
------
Dockerfile:59
--------------------
  57 |     COPY mix.exs mix.lock ./
  58 |     RUN mix deps.get --only $MIX_ENV
  59 | >>> RUN mix deps.compile
  60 |
  61 |     COPY assets assets
--------------------
ERROR: failed to solve: process "/bin/sh -c mix deps.compile" did not complete successfully: exit code: 1

The image builds just fine if I don’t pass the --platform, so if I have:

FROM ${BUILDER_IMAGE} AS builder

Then the image builds and runs fine on my machine, but then I can’t use in in AWS - at least with what I have access (all other services are built for amd64 to work on our infra.

Any help at this point would be much appreciated!

edit: Just found this other related question here: Cross-platform specific error when installing latest hex (unfortunately, without a solution - I’ll keep looking).

edit2: Finally found a solution to the problem in this other question: Elixir docker image won't build for linux/arm64/v8 using Github Actions - #13 by yuriploc

It suggests adding ERL_FLAGS="+JPperf true" to the environment and so I did in my Dockerfile, so now I have:

FROM --platform=linux/amd64 ${BUILDER_IMAGE} AS builder
...
# set build ENV
ENV MIX_ENV="prod"
ENV ERL_FLAGS="+JPperf true"

# install mix dependencies
COPY mix.exs mix.lock ./
RUN mix deps.get --only $MIX_ENV
RUN mkdir config

# copy compile-time config files before we compile dependencies
# to ensure any relevant config change will trigger the dependencies
# to be re-compiled.
COPY config/config.exs config/${MIX_ENV}.exs config/
RUN mix deps.compile
3 Likes