Elixir release inside Docker container without Rabbit MQ Connection

I have tried to create a project such as yours.

mix new broker_client

In the mix.exs application function is configured to run the start function of my module.

def application do
  [
    extra_applications: [:logger],
    mod: {BrokerClient, []}
  ]
end

Additionally, in config/runtime.exs, I am configuring amqp with the connection and one or more channels as documented here.

import Config

config :amqp,
  connections: [
    myconn: [url: System.get_env("BROKER_URL")]
  ],
  channels: [
    mychan: [connection: :myconn]
  ]

In lib/broker_client.ex I have the start function implemented which creates a simple Task as showing in this answer.

defmodule BrokerClient do
  def sample() do
    {:ok, chan} = AMQP.Application.get_channel(:mychan)
    :ok = AMQP.Basic.publish(chan, "", "", "Hello")
    Process.sleep(1000 * 10)
  end

  def start(_type, _args) do
    IO.puts("starting...")
    Task.start(fn -> sample() end)
  end
end

I can build this fine without having rabbitmq running locally or setting the variable broker.

FROM elixir as builder
WORKDIR /app
RUN mix local.hex --force && mix local.rebar --force
COPY mix.exs mix.lock ./
RUN mix deps.get --only prod
COPY  ./ .
RUN MIX_ENV=prod mix release

FROM debian:stable-slim
ENV LANG="C.UTF-8" LC_AL="C.UTF-8" PATH="/app/bin:$PATH"
COPY --from=builder /app/_build/prod/rel/broker_client /app
CMD [ "broker_client", "start"]

Now I can run this with docker-compose as example.

version: '3.9'

services:
  client:
    build: ./
    environment:
      BROKER_URL: 'amqp://guest:guest@rabbitmq'
    # sleep 10 seconds to give the broker time to start
    command: [ "sh", "-c", "sleep 10 && broker_client start" ]
    depends_on:
      - rabbitmq
  rabbitmq:
    image: rabbitmq

Its probably also useful to look at the offical docs for Application.


You need to use the runtime configuration, otherwise the configuration must be passed at compile-time, to do this all you have to do is add runtime.exs and to get the environment variable there:

config :rabbit, url: System.get_env("RABBIT_CONNECTION")