H12

H12

LiveView code reloading doesn't work when using Docker for local development

So I’m working on docker-izing a LiveView side-project of mine, and I’m having some struggles with code reloading.

Specifically, when I make changes to a LiveView project file, the app simply won’t recompile, throwing the following error:

** (RuntimeError) could not compile application: docker_example.

You must restart your server after changing the following config or lib files:

  * config/dev.exs
  * config/config.exs
  * mix.exs


    (phoenix 1.5.4) lib/phoenix/code_reloader/server.ex:211: Phoenix.CodeReloader.Server.mix_compile_unless_stale_config/1
    (phoenix 1.5.4) lib/phoenix/code_reloader/server.ex:196: Phoenix.CodeReloader.Server.mix_compile_project/3
    (phoenix 1.5.4) lib/phoenix/code_reloader/server.ex:75: anonymous fn/2 in Phoenix.CodeReloader.Server.handle_call/3
    (phoenix 1.5.4) lib/phoenix/code_reloader/server.ex:262: Phoenix.CodeReloader.Server.proxy_io/1
    (phoenix 1.5.4) lib/phoenix/code_reloader/server.ex:73: Phoenix.CodeReloader.Server.handle_call/3
    (stdlib 3.12.1) gen_server.erl:661: :gen_server.try_handle_call/4
    (stdlib 3.12.1) gen_server.erl:690: :gen_server.handle_msg/6
    (stdlib 3.12.1) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

I’ve been able to reproduce this in a fresh mix phx.new app_name --live --no-ecto project using the following files:

Dockerfile

FROM elixir:1.10

RUN apt-get update
RUN apt-get install --yes build-essential inotify-tools

# Set working directory
WORKDIR /app

# Install hex and rebar
RUN mix local.hex --force &&\
      mix local.rebar --force

# Install nodejs
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash - &&\
      apt-get install --yes nodejs

# Set the run command
CMD ["./run.sh"]

run.sh

#!/bin/sh

echo "Installing Elixir dependencies...\n"
mix deps.get
mix deps.compile

echo "Installing Node dependencies...\n"
cd assets && npm install
cd ..

echo "Launching Phoenix server...\n"
mix phx.server

docker-compose.yml

services:
  app:
    build: .
    ports:
      - "4000:4000"
    volumes:
      - /app/_build
      - /app/deps
      - /app/priv
      - /app/assets/node_modules
      - .:/app

With those files added to the project root, and after starting the project with docker-compose up --build, making a change to the generated lib/docker_example_web/live/page_live.ex file usually results in a compilation error. It sometimes reloads as it should, but I’m generally able to get it to fail compilation after saving a couple of times.

Additionally, when serving the app with mix phx.server, code reloading works flawlessly, so I’m fairly confident this issue is Docker-related.

Anyone have any idea what’s going on here? I’ve thrown a ton of different Dockerfile & docker-compose.yml setups at this problem to no avail.

Marked As Solved

slouchpie

slouchpie

Your given code does not even run for me. I get permission denied error when container tries to run run.sh.

Your general approach seems a bit wrong. There should be a COPY statement somewhere in your Dockerfile. Also you should mix deps.get and mix deps.compile as part of the build.

Try this setup:

Dockerfile:

FROM elixir:1.10

RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
RUN apt update
RUN apt install -y git nodejs inotify-tools
RUN apt install -y chromium-driver

RUN mkdir -p /app
WORKDIR /app

RUN mix local.hex --force && \
  mix local.rebar --force && \
  mix archive.install --force hex phx_new

COPY mix.exs .
COPY mix.lock .

# copy the deps in dev environment for faster builds
COPY deps ./deps
RUN ["mix", "deps.get"]
RUN ["mix", "deps.compile"]

COPY assets ./assets
WORKDIR /app/assets
RUN ["npm", "install"]

WORKDIR /app

COPY config ./config
COPY lib ./lib
COPY seeds ./seeds
COPY priv ./priv
COPY test ./test
COPY dev/support ./dev/support

RUN ["mix", "compile"]

# compile deps in test environment for faster test runs when built
RUN export MIX_ENV=test && mix deps.compile

COPY ./run.sh ./run.sh
CMD ["/bin/bash", "entrypoint.sh"]

with a run.sh like this:

iex --sname app -S mix phx.server;

and a docker-compose.yml like this:

version: "3.8"
services:
  app:
    image: app:local
    build:
      context: .
      dockerfile: ./Dockerfile
    ports:
      - "4000:4000"
    volumes:
      - /app/assets/node_modules
      - ./assets:/app/assets:ro
      - ./config:/app/config:ro
      - ./lib:/app/lib:ro
      - ./priv:/app/priv
      - ./test:/app/test:ro
      - ./seeds:/app/seeds:ro
      - ./mix.exs:/app/mix.exs:ro
      - ./run.sh:/app/run.sh:ro
    stdin_open: true
    tty: true

I don’t know why your setup had the problem with config or mix.exs being “altered” but I suspect it has something to do with how your never actually COPY the files during your build.

Disclaimer: there is no one correct way to use docker. It depends on your own needs. What I have provided is good for local :dev environment, local debugging.

Also Liked

Exadra37

Exadra37

Because you then need to rebuild the container each time you change a file, while mapping them in docker-compose.yml always reflects immediately in the docker container the changes done in host.

bryanhuntesl

bryanhuntesl

Lots of people start out the same way, I’ve seen it with 4 client teams I’ve worked with - ultimately they find the regular Elixir/Visual Studio install does the job without containers complicating everything. Another thing is that filesytem speed in Docker for Mac is dreadful - seen all sorts of hacks to work around that too, NFS mounting being one that particularly stood out. Still say, run your Postgres etc in Docker, but your development workspace, that’s overkill.

Exadra37

Exadra37

I don’t have to build every time I run the docker container, because I map the entire app I am working on to the container, thus I have the exact same dev flow I would have if I was not using docker.

Last Post!

H12

H12

Thanks! My specific issue is a social one, not a technical one (my work locks down homebrew usage on dev boxes due to security concerns).

But I’ll contribute to your new thread – I’ve definitely had a non-zero number of issues installing Elixir on my personal machine.

Where Next?

Popular in Questions Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement