LukasKnuth

LukasKnuth

Bake release version at compiletime

I’m using the standard Dockerfile generated by mix phx.gen.release --docker with a small change:

# Compile the release
ARG APP_VERSION
RUN mix compile

The added ARG expression accepts a Docker build-arg that I supply when building the image:

docker build . -t my_app --build-arg APP_VERSION=0.1.1

In my mix.exs I load the data from the Environment variable:

defmodule MyApp.MixProject do
  use Mix.Project

  def project do
    [ #Other config
      version: version()
    ]
  end

  def version do
    System.get_env("APP_VERSION", "0.0.0-local")
  end
end

I’m expecting that MyApp.MixProject.version/0 is invoked at compile time and that the ENV variable is read then and “baked” into the final release.
This seems to be true partially:

  • during image build it logs: assembling my_app-0.1.1 on MIX_ENV=prod
  • running bin/my_app version returns the expected 0.1.1
  • the file releases/start_erl.data contains 15.2.1 0.1.1 (the later is the version)
  • the folder in releases/0.1.1 exists

All this seems to indicate that it worked fine, but at runtime, i get the fallback value:

iex> Application.spec(:my_app, :vsn)
~c"0.0.0-local"

I can’t figure out why it seems to work everywhere except for the Application spec.

I have also tried setting the APP_VERSION env variable to some other string to see if it might be evaluated at runtime - it still returns "0.0.0-local". What am I overlooking?

First 4 of 4 Posts Switch mode

arcanemachine

arcanemachine

Off the top of my head, I would try this:

ARG APP_VERSION
ENV APP_VERSION=$APP_VERSION

build arg != environment variable

EDIT: On second thought, maybe just skip the build arg part altogether and pass it as an environment variable:

docker build . -t my_app -e APP_VERSION=0.1.1

Then, I don’t think you would need to put anything in the Dockerfile.

LostKobrakai

LostKobrakai

Not sure if that’s helpful, but all the places where things worked are the result of mix release, while Application.spec uses the .app file of the compiled application as created by mix compile. The .app file would be in lib/my_app-{version}/ebin/my_app.app of the release.

LukasKnuth

LukasKnuth OP

Bingo! Even the folder is called lib/my_app-0.0.0-local/. I have moved the ARG APP_VERSION instruction higher in the Dockerfile, just below ENV MIX_ENV="prod" and now everything works as expected!

I wonder what step really creates the .app file, because I had the instruction right above mix compile before (where it didn’t work). After moving it, the following commands now also receive it:

  • mix deps.get
  • mix deps.compile
  • mix assets.deploy

None of which seems like an obvious candidate to me.

rhcarvalho

rhcarvalho

One of those might be doing it as a side effect of their transitive dependencies. You don’t have to call “mix compile” exclusively to trigger compilation (and in that case your RUN mix compile line was likely a no-op.)

For example, it happens all the time when you run mix test – code is first compiled, then the test suite is run.

— All posts loaded —

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement