Releases
A release is the program packaged for a machine that has no Elixir and no Mix. mix release writes a folder with the Erlang runtime, the compiled application and a start script; the target machine needs an operating system and the release, nothing else. A release is how an OTP application leaves the developer machine.
mix release
The command builds in the prod environment by default and writes under _build/prod/rel. The output names the release, the version and the few files it skips on a system without Windows:
mix new rel_demo
* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating lib
* creating lib/rel_demo.ex
* creating test
* creating test/test_helper.exs
* creating test/rel_demo_test.exs
Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:
cd rel_demo
mix test
Run "mix help" for more commands.
cd rel_demo
MIX_ENV=prod mix release
Compiling 1 file (.ex)
Generated rel_demo app
* assembling rel_demo-0.1.0 on MIX_ENV=prod
* skipping runtime configuration (config/runtime.exs not found)
* skipping elixir.bat for windows (bin/elixir.bat not found in the Elixir installation)
* skipping iex.bat for windows (bin/iex.bat not found in the Elixir installation)
Release created at _build/prod/rel/rel_demo
The rest of the output lists the commands of the start script. The block keeps the build and the line that says where the release is, because the command list is the same for every release.
The folder it writes
_build/prod/rel/rel_demo holds erts-* (the runtime), lib (the application and its dependencies), releases (the boot scripts and the version) and bin (the start script). Copying the folder to another machine of the same architecture is the deploy, and the machine does not need Elixir.
A command in the release
The start script has commands of its own. eval runs an expression in a fresh VM with the application loaded, which is the quick check that the release really works:
_build/prod/rel/rel_demo/bin/rel_demo eval 'IO.puts("from the release")'
from the release
_build/prod/rel/rel_demo/bin/rel_demo eval 'IO.inspect(Application.spec(:rel_demo, :vsn))'
~c"0.1.0"
The version is the charlist of the application specification, the same value the application lesson read in Mix. The start command keeps the release running, stop ends it and remote opens a shell in the running node.
The environment at boot
A release reads config/runtime.exs when it starts, not when it is built, so the port and the database of the target machine come from its environment. The configuration lesson shows the call; the release is the reason the file exists. RELEASE_COOKIE names the cookie of the node, and RELEASE_NODE the node itself, both read at boot.
The commands on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), on Linux x86_64 in a Docker container.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
mix releaseproduce?Answer
A folder with the runtime, the application and a start script
A release is self-contained: ERTS, the app and the bin scripts.
-
Which environment does
mix releasebuild by default?Answer
prod
The release task uses MIX_ENV=prod unless told otherwise.
-
What does the target machine need to run a release?
Answer
The same operating system and architecture, and the release folder
The runtime is inside the release; the machine supplies the OS and the architecture.
-
Which command runs an expression in a release without starting it as a service?
Answer
bin/app eval
eval runs the expression in a fresh VM with the release loaded.
-
What does the release command
bin/rel_demo eval 'IO.puts("from the release")'print?Answer
from the release
eval runs the expression; IO.puts writes the line.
-
What does
Application.spec(:rel_demo, :vsn)return in a release?Answer
The charlist ~c"0.1.0"
The version in the application spec is a charlist.
-
When is
config/runtime.exsread?Answer
When the release starts
The runtime file is evaluated at boot, so the machine decides the values.
-
Why does a release not need Elixir on the target machine?
Answer
Because it carries ERTS and the compiled beams
The Erlang runtime and the application are copied into the release folder.
-
Which command connects a shell to a running release?
Answer
bin/app remote
The remote command opens a shell in the running node.
-
What is
RELEASE_COOKIEfor?Answer
The cookie that authenticates a connection to the node
The cookie is the shared secret of the distributed node.
Автор статьи: Arthur Isaev