Variable substitution in docker-compose .env env-files
I was recently given an existing set of files to create a collection of Docker containers via the docker-compose tool. Unfortunately this rapidly led to some annoyances with variable substitutions within a docker-compose (.env) env-file. This is the kind of thing I mean:
DOMAIN=example.org
EMAIL=admin@${DOMAIN}
The problem was that they don't seem to be supported in docker-compose 1.25.5 (as shipped in the 19.03.11 docker snap). This resulted in the variable containing the raw string from the .env file - eg admin@${DOMAIN}
which caused all sorts of confusion.
This question appears to suggest that a later Docker/docker-compose might support internal env-file substitution, however the latest snap:
sudo snap refresh --edge docker
(Version 19.03.13)
...Still seems to contain docker-compose 1.25.5 and does not perform env-file substitution.
Grabbing the latest docker-compose release and manually using that version (currently 1.27.4) does appear to support internal env-file variable substitution.
Here's a quick download/install process borrowed from DigitalOcean:
sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
(Although personally I put it in a sub-directory of my homedir and executed it from there.)
Why did it change?
I think I've tracked down the change in behaviour to docker-compose 1.26.0, the release notes say Added python-dotenv to delegate .env file processing.
and python-dotenv can "interpolate variables using POSIX variable expansion".