This repository demonstrates seven (7) different ways that env vars can be injected into docker, either at buildtime or runtime, each in surprising and not at all irritating ways.
ENV
instruction in Dockerfile (cannot be directly overriden at build time)ARG
instruction in Dockerfile (can overrideENV
at build time but only if assigned to do so)environment
incompose.yml
(only evaluated at runtime)args
incompose.yml
(can override Dockerfile instructions).env
file- can override
args
during build if explicitly assigned from environment incompose.yml
, e.g.args: NODE_ENV: ${NODE_ENV:-buildtime} # note posix fallback also works here
- can override
--build-arg
CLI option used with docker compose CLI forbuild
command, e.g.this will overridedocker compose build --build-arg NODE_ENV=peekaboo
.env
file-e
CLI option used with docker compose CLI forrun
command, e.g.this will override any settings for an env var at runtime onlydocker compose run -e NODE_ENV=gotcha vartest