Skip to content

Draft: Env file loading (MR will be replaced)

Resolves #336

How it works:

  • When ./cdli.py or ./cdlidev.py runs, it now generates an environment variable file to pass to Docker Compose's --env-file parameter.
  • The generated environment variable file compiles values from two files: .env, and .env.[<env>] (e.g. .env.gea or .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 environment attribute in the docker-compose.yml files (see https://docs.docker.com/reference/compose-file/services/#environment).
  • The .env, .env.*, and .env-generated files are ignored from source-control.
  • There is an .env-examples file that should detail any required or important variables that users may need to put in their own .env files 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 up will compile the .env file into its generated environment.
  • The SERVER_HOSTNAME variable in the container will default to internal.cdli.network, since CDLI_POSTFIX_SERVER_HOSTNAME is not set in the environment or .env file.
  • The SMTP_PASSWORD variable in the container will take the value of CDLI_POSTFIX_SMTP_PASSWORD.
    • If the value was not set in the .env file, the ?error interpolation would cause Docker Compose to fatally error on startup.
  • Since CDLI_POSTFIX_DOMAIN isn't set in the environment or .env file, 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

Merge request reports

Loading