Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose .env vs direnv .envrc

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?

like image 715
Patrick Avatar asked Dec 19 '17 03:12

Patrick


People also ask

Does docker use .env file?

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.

What is Direnv?

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.

What is the use of .env file?

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.

How do I pass an environment variable in docker run?

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.


2 Answers

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

like image 119
lfender6445 Avatar answered Sep 20 '22 19:09

lfender6445


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.

like image 23
wab Avatar answered Sep 17 '22 19:09

wab