Fl4m3Ph03n1x

Fl4m3Ph03n1x

Why is mix escript not good enough for deploys?

Background

I am doing a personal project and as a deliverable I create a script using MIX_ENV=prod mix escript.build.
My program is in this small script so when I want to use it I can just run it via ./my_script --args.

It works great for the most part, but I had some questions so I started digging - which is when i found this:

(https://hexdocs.pm/mix/master/Mix.Tasks.Escript.Build.html)
Escripts should be used as a mechanism to share scripts between developers and not as a deployment mechanism. For running live systems, consider using mix run or building releases.

Why?

My application is a terminal application, so using escript looked like an obvious choice. Why should I use mxi release instead of having a script file?

Both need erlang on the machine they are going to run on, and mix release actually needs to compile in a machine with the same architecture and OS system of the machine that is going to execute the file (a luxury I cannot afford for now).

Could someone explain me what are the main drawbacks of using escript?

First 9 of 9 Posts! Switch mode

josevalim

josevalim

Creator of Elixir

escripts does not support accessing content in priv directories. That rules out serving static files, running migrations, etc, right out of the box. They also don’t provide the same flexibility as Mix tasks, which provide different entry points and command line options. And at the end of the day, both Mix and Escripts are inferior when it comes to system configuration compared to releases.

So can you use escripts? Sure, you can, but they are inferior in almost every way compared to other options, so I don’t see the point of using them besides one-off scripts.

amnu3387

amnu3387

A release doesn’t require neither erlang nor elixir to be installed on the target machine as long as you either pack ERTS (the default) or the machine has ERTS accessible, it does require that the release be assembled on the same target architecture indeed and to assemble the release you do need erlang and elixir, but you can do that with docker easily mimicking wtv targets you wish.

Fl4m3Ph03n1x

Fl4m3Ph03n1x

That requires me to have docker and to do mix release in a docker container. This inevitably adds complexity I would pretty much like to avoid and I am not sure how it even fits with the rest of my pipeline.

While I do agree this is a solution worth exploring, I would personally like a release process like with other technologies where you can define the OS and architecture when building the release.

Imagine not having to use docker nor any additional tools and just run mix realease --os=linux --arch=amd64. (:wink: :wink: since @josevalim is now reading this :stuck_out_tongue: )

This however, is outside the scope of this post (nor do I know if this is even possible to begin with) and you have provided a viable solution.

Thank you for the answer it is clear now !

psylone

psylone

Maybe it worth to mention about it in the mix escript.build docs? I had the same question and reached this post after reading the docs.

tristan

tristan

Rebar3 Core Team

Technically you can. The escript api takes an archive of files where you can include priv directories. This is how rebar3 works and rebar3 escriptize supports this as an option, escript_incl_extra.

Also, one thing not mention that releases provide over escripts is the ability to bundle ERTS. You can not create a self-contained escript but must have Erlang installed on the system you are deploying to.

tristan

tristan

Rebar3 Core Team

This would be simple to implement if restricted to only fetching OTP for that architecture. The issue is what is done for NIFs, ports, etc that are in the project? That is outside the scope of a release task and would require all the different compiler tasks, like zigler, rustler, and general makefiles and rebar3’s pc plugin have a common interface for assigning the architecture and each to support cross compiling.

But I’d also point out that for many people it doesn’t require docker/vm – except for those who have a hard requirement on deploying local code directly to a target and don’t run on the same env as the target – because usually CI is involved before deploying were the release can be built and either directly deployed or pushed to some storage to be deployed later.

tristan

tristan

Rebar3 Core Team

Oh, one last thing. If you don’t have to compile the code for your escript on the same arch as where you are running it then you don’t for a release either.

Releases do not have to contain ERTS, so a user who is fine with having the Erlang runtime installed on the target host only has to compile any other potential native code for that host. There must be no native code in the project so no need to build the release on a particular architecture and set of system libraries, simply leave out ERTS.

All that to say, nothing about a release is unique in requiring compilation on the target host that doesn’t also apply to running the project any other way because you do not have to bundle ERTS into the release.

josevalim

josevalim

Creator of Elixir

A mix release that doesn’t include the Erlang VM will have the exact same properties as an escript. You only need the target compilation if you are bundling the VM and if you need to bundle NIFs (not supported by escripts IIRC).

I mean, we also include them, but IIRC it is not easy to read them. You would need to use an special API for so?

tristan

tristan

Rebar3 Core Team

Don’t think so. Can just read them like regular files with regular paths (path just has to include the name of the escript).

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
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
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

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