ewintr.nl

Shared environment variables for make, bash and docker

It is possible to define a set of variables and share them in make, bash and the Docker containers that are orchestrated by docker-compose.

Docker-compose can use an .env file to substitute variables in a docker-compose.yml file in the same directory. In this docker-compose.yml they can be exported to the containers.

Incuding this .env file in your Makefile makes hem available there as well, but they are not automatically exported to the bash shells that are spawned by make to execute the targets. This can be changed by adding the .EXPORTALLVARIABLES: target to your Makefile.

An example that covers all areas is this .env file:

VAR1=this
VAR2=that
VAR3=those

With a Makefile:

include .env

.EXPORT_ALL_VARIABLES:

task:
  @echo "VAR1 is ${VAR1}"
  @some_command       # some_command can use $VAR1, $VAR2, $VAR3
  @docker-compose up

Together with this docker-compose.yml:

...
app:
  image: "registry/the_app:${VAR2}"
  environment:
    - VAR3=${VAR3}

Sources