Draft: Env file loading (MR will be replaced)
Resolves #336
How it works:
- When
./cdli.pyor./cdlidev.pyruns, it now generates an environment variable file to pass to Docker Compose's--env-fileparameter. - The generated environment variable file compiles values from two files:
.env, and.env.[<env>](e.g..env.geaor.env.dev).- The existence of either of the files is optional.
- If there happen to be duplicate keys across one or more of the source files, the last entry wins out.
- Docker Compose will load the file into the environment for use in the containers.
- Pre-existing OS environment variables will retain precedence over variables set in the file.
- The environment variables can be passed into containers using the
environmentattribute in thedocker-compose.ymlfiles (see https://docs.docker.com/reference/compose-file/services/#environment).- Values set in the Compose file can be constant, or they can reference environment variables set by the OS or the generated file through interpolation (see https://docs.docker.com/reference/compose-file/interpolation/).
- The
.env,.env.*, and.env-generatedfiles are ignored from source-control. - There is an
.env-examplesfile that should detail any required or important variables that users may need to put in their own.envfiles for a given environment.
Quick and dirty example:
Take for instance this docker-compose.yml
# Image--infrastructure:postfix
postfix:
image: juanluisbaptiste/postfix:latest
environment:
SERVER_HOSTNAME: ${CDLI_POSTFIX_SERVER_HOSTNAME:-internal.cdli.network}
SMTP_SERVER: smtp.cdli.network
SMTP_USERNAME: no-reply@cdli.network
SMTP_PASSWORD: ${CDLI_POSTFIX_SMTP_PASSWORD?error}
ALWAYS_ADD_MISSING_HEADERS: yes
DOMAIN: ${CDLI_POSTFIX_DOMAIN}
networks:
- nodelocal-private
- nodelocal
And this .env file:
CDLI_POSTFIX_SMTP_PASSWORD=a_secret_SMTP_password
- Running
./cdli.py upwill compile the.envfile into its generated environment. - The
SERVER_HOSTNAMEvariable in the container will default tointernal.cdli.network, sinceCDLI_POSTFIX_SERVER_HOSTNAMEis not set in the environment or.envfile. - The
SMTP_PASSWORDvariable in the container will take the value ofCDLI_POSTFIX_SMTP_PASSWORD.- If the value was not set in the
.envfile, the?errorinterpolation would cause Docker Compose to fatally error on startup.
- If the value was not set in the
- Since
CDLI_POSTFIX_DOMAINisn't set in the environment or.envfile, Docker Compose will warn that it is going to pass an empty string where it is referenced, but otherwise will run without error (the container may not like this).
Edited by Tim Bellefleur