darnahsan
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.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
engineeringdept
It sounds like you’re probably looking for the releases functionality that was added in Elixir 1.9. Here’s a good guide about setting up a Docker container that builds using
mix release, where the runtime config is set from environment variables.darnahsan
Thanks for the reply , my app is already a multistage docker build using docker-compose to run it. From the article it seems it relies on the vars to be passed while bringing up the container. Maybe thats the way to go by which I might have to make peace with but then again one could have many such variables for the config making it error prone to start up your application or the command complex enough to remember.
for my 2nd part of the question is mutating Application
envacceptable ? is it bad or just a choice ?engineeringdept
Sorry, I think I misunderstood the question.
I usually have a
config/rel/runtime.exsfile, 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
Applicationenvironment 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 runcommand 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.axelson
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.
darnahsan
not really for my current use case but considering it is called quite frequently I wouldn’t do it in ruby, new to elixir so trying to understand its impact for high volume apps. Does it have any additional overhead compared to
Application.fetch_env. the bot can get enough request to simulate a high traffic app and I haven’t seen any impact with reading multiple values fromSystemyetdarnahsan
so list down whats missing or happening while using docker-compose
I have the config sentrey set via
envvariableThe value is set via
env_fileoption in the docker-compose file.after doing a build the
docker-compose buildvalues are available inside the container when I run it
docker-compose updocker exec -it ultronex_bot shI can see the value for
$SENTRY_DSNand is successfully set by Application.put_all_env at runtime.but the
sys.configwhich is in the build failed to fetch the values while building a release which means its not available to the build process{sentry,[{dsn,nil},{included_environments,[prod,dev]},{environment_name,prod}]}Seems this could be due to use of
docker-composewher itsenv_fileonly sets values inside the container and not in build process and using thedockercommand with command line vars is the way to build with runtime dependencies. If that is the case then how to do a build withdocker-composewith runtime configs or maybe I got wrongdocker-composesettingswhat I understand the
envvariables set viaenv_filecan only be available after container runs, and to do a elixir release the build would requireARGSbeing set that then are set asENVin the build phase to be available as runtime variables for the release to usedarnahsan
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.
darnahsan
thanks for your insights helped me put things in better order as newbie to elixir