We've been using direnv for quite some time now to automatically load environment variables in a specific folder. And since version 3, docker-compose seems to support .env files.
The .envrc
files used by direnv use export
:
export NODE_ENV=development
Using the same file with docker-compose doesn't seem to work, only without export
I get the value for the variable.
NODE_ENV=development
Any ideas on how to unify this into a single .env
or .envrc
file or an alternative to direnv?
Here's a list of easy takeaways: The . env file, is only used during a pre-processing step when working with docker-compose. yml files.
direnv is an extension for your shell. It augments existing shells with a new feature that can load and unload environment variables depending on the current directory.
env. example file documents the application's necessary variables and can be committed to version control. This serves as a helpful reference and speeds up the onboarding process for new team members by reducing the amount of time spent digging through the coding to figure out what needs to be set up.
When we launch our Docker container, we can pass environment variables as key-value pairs directly into the command line using the parameter –env (or its short form -e). As can be seen, the Docker container correctly interprets the variable VARIABLE1.
Here is an alternative solution based on the comment chain for this answer
direnv ships with a stdlib that can be used to support classical 'dotenv' settings
# myproject/.envrc
# dotenv <optionalPathToDotEnvFile>
dotenv
# myproject/.env
FOO=BAR
this is especially helpful when using container systems like docker that support the dotenv style
I use the following setup to have variables available during dev from .envrc
but using a docker-compose
file for deployment:
In ./secrets
define your variables as docker-compose needs them (without export):
foo=bar
secret_var=secret
...
In ./envrc
export them to your shell:
#!bash
set -a
. ./secrets
set +a
set -a
makes everything export by default, set +a
turns this off afterwards.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With