darnahsan

darnahsan

Mutating Application env with put_env in containers, good or bad?

I have this slack bot app with an api to check for stats. As its containerised with docker. I was using env file via docker-compose. The app was using System.get_env to fetch values every time which obviously doesn’t sounds good to make a system level call for every incoming request. So I decided to put them in config file and as module level constants but for both the values are not available at runtime as they are set via docker-compose and are only available once the container is running.
One approach would be to embed those values in the Dockerfile or pass them along the build phase but that is same as embedding it in, which I don’t prefer. This missing values also causes issues with libraries like sentry where it depends on environment variables.

One way I solved it is upon application starting I call put_all_env and set the values from System.get_env and then use Application,.fetch_env!

so what I would like to know is if that is ok to do or are there any other practices for containers to have compile time and runtime values available for releases.

Marked As Solved

darnahsan

darnahsan

I think I have for runtime variables figured out for docker-compose. need to define the env variables on the build machine and add values via args to docker-compose and then refer to them in the Dockerfile as ARG which are then available to the build process.

Also Liked

axelson

axelson

Scenic Core Team

Are you actually running into concrete performance problems with this? I wouldn’t really expect you to unless you’re calling that in a very tight loop.

engineeringdept

engineeringdept

Sorry, I think I misunderstood the question.

I usually have a config/rel/runtime.exs file, which sets the application config from the system’s environment variables. e.g.:

config :app, :component, api_key: System.fetch_env!("API_KEY")

Then I make as many calls to Application.fetch_env! as I need to from the runtime code - they’re cheap enough.

I wouldn’t mutate the Application environment further at runtime. I think of it as layers: System Environment -> Application Environment -> Runtime code, with each layer reading the layer outside it, but never writing to it.

That means accepting that your docker run command might reference a lot of environment variables. I’d prefer to keep everything explicit, so I’d tend to solve that through other means, perhaps a bash script, depending on your environment.

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement