maz
Load env vars with docker-compose env_file, secrets
Hi all, I have an elixir umbrella app that I deploy via docker-compose. I’d like to use env_file directive in my docker-compose.yml for things like guardian keys etc. I have my env vars in a my_app.env file which is referred to in the docker-compose.yml. The vars do not appear to be available at runtime because when I try to generate a JWT with guardian, the phoenix endpoint generates a Bad Request. If I return the hardcoded key, it works OK.
Any recommendations to getting secrets or env vars injected into a running docker elixir app? I also tried docker secrets via the docker-compose file and I can confirm that the umbrella app cannot find the /run/secrets/secret_file path/volume, even though docker builds the image without error. I did notice that /run/secrets doesn’t appear in docker volumes ls but that might not be either here or there.
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
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance










First 10 of 13 Posts
NobbZ
Docker secrets only work when you are using docker swarm.
And the environment variables should be available but perhaps you are reading them at the wrong time?
Can you tell us more about your build process and how you read from the environment?
cnck1387
I haven’t deployed my app yet, but in development environment variables are working with Docker Compose using the same patterns that worked with other applications.
I have a
.envfile in the same directory as mydocker-compose.ymlfile and then in the compose file I use:At this point all of the env variables are available in my Phoenix config files. For example you can use
System.get_env("SOME_ENV_VAR_FROM_THE_ENV_FILE").Are you doing something different, or maybe you forgot the
env_fileproperty on one of your services?maz
Is
env_file:a child ofservices:level or a child ofmy_app:level?I’m open to trying anything at this point.
cnck1387
It’s a child of
myapp. Each individual service can have its own list of environment files (they are loaded and merged from top to bottom since technically you can have more than 1 file).Basically, Docker isn’t going to automatically read in your environment variables. By setting
env_fileon a specific service, that instructs Docker to make them available.The only file that gets automatically used is a
.envfile, but that’s for setting things like theCOMPOSE_PROJECT_NAMEor using variable substitution in thedocker-compose.ymlfile itself. The vars in the.envfile won’t get loaded into your services unless you explicitly set it inenv_file.Edit: There’s also the
environmentproperty which you can use either in combination with or as an alternative toenv_file. Theenvironmentproperty lets you pass in key / value pairs of environment variables instead of having them loaded in from a file. Could be useful in some cases.NobbZ
@maz, let me ask you again, how exactly is your build process?
The Dockerfile won’t see environment variables that are only injected at container runtime via an env file. So depending on if you are doing compiletime or runtime configuration, the environment might already be backed in as “empty”.
maz
Sorry about the lack of response, I only had time this morning to make a quick response to @cnck1387. This is my docker compose with the env changes(they are not yet checked-in from my linux box at home which is where I can do quick turnaround development with docker)
I set
- port:tolocalhost:4000:4000, commented out thewebnetwork, and commented-out thetraefikdirectives in order to speed up iterations:for building I do:
So going from what you are saying, I’d need access to those environment variables at least by
docker-compose build --pull olivetreebut that doesn’t seem to be the case.[EDIT] added Dockerfile to gist.
maz
yeah I’m still not sure why my
env_file:\n - ./olivetree.envis not being honored. Or maybe it is but the timing of the SET is too late. It might be something I’m doing/not doing in my build process.cnck1387
You can’t access environment variables from
env_fileat build time. It’s for run time.If you want env variables to be set at build time, take a look at build arguments in the Docker Compose documentaton.
maz
Right, so I guess I would want, say,
System.get_env("GUARDIAN_SECRET_KEY")in config.exs to resolve at buildtime? Is there any situation where I’d prefer get_env() to resolve at runtime? Because if they can only be resolved at buildtime, I am barking up the wrong tree.cnck1387
I never messed around with releases, but in development using mix to launch Phoenix,
System.get_env("FOO")will resolve at run time and it will obtain that value thanks to Docker Compose making that environment variable available to Phoenix due toenv_file(orenvironment) in the docker-compose.yml file.You can double check what’s available in your container by running
docker-compose run olivetree env. It should return back all of your environment variables that you set.