Consider the example:
Dockerfile
FROM ubuntu
ENV var=hello
ENTRYPOINT ["printenv"]
This container has an environment variable with a default value, which I can override:
docker build -t exp .
docker run exp
...
var=hello
...
docker run -e var=lalala exp
...
var=lalala
...
I would like to have this optional overridability when I use the image with docker-compose. So far I tried the following three approaches:
Obligatory override - otherwise variable unset
docker-compose.yml
version: "3.1"
services:
hello-teller:
image: exp:latest
environment:
- var
result:
docker-compose up
varis not set, default value ignored.
env var=lala docker-compose up
varis overridden.
Cast in stone
docker-compose.yml
version: "3.1"
services:
hello-teller:
image: exp:latest
environment:
- var=lala
result:
docker-compose up
varis overridden tolala.
env var=huhu docker-compose up
varis still overridden tolala.
No effect
docker-compose.yml
version: "3.1"
services:
hello-teller:
image: exp:latest
environment:
- ${var:-lala}
result:
docker-compose up
varis not overridden.
env var=huhu docker-compose up
varis not overridden.