Is it possible to start selected apps in umbrella by using Mix?

Suppose that I have an umbrella project. In this project, there’re three sub apps - a, b, c.

I want to deploy them as 2 releases:

  • web: includes a and b
  • remote_db: includes c

When building releases for them, it is easy:

releases: [
  web: [
    applications: [
      a: :permanent,
      b: :permanent
    ]
  ],

  remote_db: [
    applications: [c: :permanent]
  ]

But when doing development in local, it’s hard to start the applications in this way:

  • one BEAM instance to run a and b
  • one BEAM instance to run c

When I run iex -S mix, it always runs a, b, c together.

Does Mix support to run applications in this way? Or, is there any other way to do this?

You could do it via an ENV, e.g. START_APPS=a,b iex -S mix and then start whatever you need based on Mix.env() and the value of START_APPS

1 Like

This might be a better answer Mix task to start only certain applications? - #3 by shanesveller

This accepted solution is using mix cmd, which has a section in the official docs:

Beware that the Erlang VM does not terminate child processes when it shuts down. Therefore, if you use mix cmd to start long running processes and then shut down the VM, it is likely that those child processes won’t be terminated with the VM.

Because of that, I don’t think that’s a good solution.


I like @hauleth 's solution —— Don’t start any application by using --no-start, then start the required application manually.


Some contents from my notebook. 🠷

Start non-interactive shells:

$ mix run --no-start --no-halt -e "Enum.each([:a, :b], fn app -> {:ok, _} = Application.ensure_all_started(app) end)"
$ mix run --no-start --no-halt -e "Enum.each([:c], fn app -> {:ok, _} = Application.ensure_all_started(app) end)"

Start interactive shells:

$ iex -S mix run --no-start -e "Enum.each([:a, :b], fn app -> {:ok, _} = Application.ensure_all_started(app) end)"
$ iex -S mix run --no-start -e "Enum.each([:c], fn app -> {:ok, _} = Application.ensure_all_started(app) end)"

IEx will keep the BEAM instance running, so --no-halt is unnecessary in this case.

If cleaner code is required, try to move the eval code into standalone files, then above shell commands will be:

$ mix run --no-start --no-halt scripts/start-web.exs
$ mix run --no-start --no-halt scripts/start-db.exs

$ iex -S mix run --no-start scripts/start-web.exs
$ iex -S mix run --no-start scripts/start-db.exs

If you are using Phoenix, and want to start the Phoenix endpoint, remember to put following content into -e <code> or .exs file:

Application.put_env(:phoenix, :serve_endpoints, true, persistent: true)

From: phoenix/phx.server.ex at fd7421a9d37b126b32e29a7476256384a3ca4257 · phoenixframework/phoenix · GitHub

1 Like