简体   繁体   中英

Error with running simple Elixir app in Docker container

I have a very simple Elixir app

defmodule Example do
  require Logger
  def greet(word) do
    Logger.info "Hello #{word}"
  end
end

Example.greet("hheheh")

Which is called on mix run

My Dockerfile looks like this

FROM elixir

COPY . /app/

WORKDIR /app

RUN chmod a+rwx -R /app

RUN mix deps get

CMD ["mix", "run"]

But when I launch the container I get the following error:

Compiling 1 file (.ex)
warning: redefining module Example (current version loaded from _build/dev/lib/example/ebin/Elixir.Example.beam)
  lib/example.ex:1


02:13:22.656 [info]  Hello hheheh
** (EXIT from #PID<0.73.0>) an exception was raised:
    ** (File.Error) could not write to file "/app/_build/dev/lib/example/ebin/Elixir.Example.beam": read-only file system
        (elixir) lib/file.ex:719: File.write!/3
        (mix) lib/mix/compilers/elixir.ex:388: anonymous fn/4 in Mix.Compilers.Elixir.write_manifest/5
        (elixir) lib/enum.ex:1755: Enum."-reduce/3-lists^foldl/2-0-"/3
        (mix) lib/mix/compilers/elixir.ex:384: Mix.Compilers.Elixir.write_manifest/5
        (mix) lib/mix/compilers/elixir.ex:163: anonymous fn/4 in Mix.Compilers.Elixir.compile_manifest/7
        (elixir) lib/agent/server.ex:31: Agent.Server.handle_cast/2
        (stdlib) gen_server.erl:601: :gen_server.try_dispatch/4
        (stdlib) gen_server.erl:667: :gen_server.handle_msg/5

02:13:22.704 [error] GenServer #PID<0.89.0> terminating
** (File.Error) could not write to file "/app/_build/dev/lib/example/ebin/Elixir.Example.beam": read-only file system
    (elixir) lib/file.ex:719: File.write!/3
    (mix) lib/mix/compilers/elixir.ex:388: anonymous fn/4 in Mix.Compilers.Elixir.write_manifest/5
    (elixir) lib/enum.ex:1755: Enum."-reduce/3-lists^foldl/2-0-"/3
    (mix) lib/mix/compilers/elixir.ex:384: Mix.Compilers.Elixir.write_manifest/5
    (mix) lib/mix/compilers/elixir.ex:163: anonymous fn/4 in Mix.Compilers.Elixir.compile_manifest/7
    (elixir) lib/agent/server.ex:31: Agent.Server.handle_cast/2
    (stdlib) gen_server.erl:601: :gen_server.try_dispatch/4
    (stdlib) gen_server.erl:667: :gen_server.handle_msg/5
Last message: {:"$gen_cast", {:cast, #Function<8.32884957/1 in Mix.Compilers.Elixir.compile_manifest/7>}}
State: {[{:module, Example, :module, "lib/example.ex", nil, <<70, 79, 82, 49, 0, 0, 6, 200, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 149, 131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115, 95, 118, 49, 108, 0, 0, 0, ...>>}], [{:source, "lib/example.ex", [Logger, Kernel], [:erlang, String.Chars], [{Example, %{{:greet, 1} => '\b'}}, {Kernel, %{{:def, 2} => [3], {:defmodule, 2} => [1], {:to_string, 1} => [4]}}, {Logger, %{{:info, 1} => [4]}}], [{Logger, %{{:bare_log, 3} => [4]}}, {String.Chars, %{{:to_string, 1} => [4]}}, {:erlang, %{{:++, 2} => [4]}}], []}]}

So, the program runs, but I'm not sure i really understand the rest of the error.

There is some kind of file mod time and permission problem going on:

Compiling 1 file (.ex) warning: redefining module Example (current version loaded from _build/dev/lib/example/ebin/Elixir.Example.beam)
lib/example.ex:1

This means that mix run is attempting to recompile your code after loading it.

My guess is that your docker image is built with MIX_ENV value that is different that the default value for mix run . There are 3 different values for MIX_ENV, prod, test, dev... and different mix commands use different values for where to look for pre-built BEAM files.

I would try changing your mix run command to :

env MIX_ENV=prod mix run

The problem might be in your Dockerfile where it says:

RUN mix deps get

This should be:

RUN mix deps.get

(note the period)

Without that then "mix run" will try to rebuild your dependencies. In your container you are running on a read-only file system so you can't build new things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM