Using environmental variables inside docker

Hi there!

I am building a dockerized phoenix application to create an api, nevertheless, each time I want to use an environmental variable looks like elixir still using the data (which is nil) that was injected in my prod.secret once I build the image. The fun part comes when I try asking for the variable directly using iex -S mix and typing Application.get_env(:application, SomeModule)[:config_key] it returns the value of my environmental variable.

I have faced the same issue before when we were using aws elastic beanstalk, and I have always resolved the issue re-deploying the same app version. Nowadays we are using other tool called and I am facing the same issue, do you guys have any ideas?

My configuration file basically looks like this:

config :application, SomeModule,
    config_key: System.get_env("VARIABLE_NAME")

and I use it in my code like that:

   defmodule SomeModule do
       #This is nil when I run the code in production
       @variable Application.get_env(:application, SomeModule)[:config_key]

Most of the kontena commands I have already used are listed here just in case you’re curious about them.

Thank you in advance.

Try packaging using bitwalker/distillery. Then in your config say prod.secret, reference the env car as $MY_VAR (the usual Unix env var declaration). In your docker file, set “ENV REPLACE_OS_VAR=true” and any other env you require.

Inject the needed in docker run using " -e MY_VAR=myvarvalue" or via a environment syntax in docker-compose.yml or via .env file with content MY_VAR=myvarvalue line and reference .env in your docker-compose.yml

1 Like