Docker Swarm with image versions externalized to .env file
The answer is quite simple: it's not a bug, nor a feature. .env
is not currently supported by docker stack
.
You must source manually the .env
running export $(cat .env)
before running docker stack ...
There is an issue discussing this needs in the Docker Github. https://github.com/docker/docker.github.io/issues/3654 and another one discussing the problem and solution: https://github.com/moby/moby/issues/29133#issuecomment-285980447
The yaml parser in docker stack deploy
doesn't have all the same features of that in docker-compose
. However, you can use docker-compose config
to output a yaml file after it's done all the variable substitutions, extending other files, and merging multiple files together. This effectively turns docker-compose
into a preprocessor.
you can create a deploy.sh
export $(cat .env) > /dev/null 2>&1; docker stack deploy ${1:-STACK_NAME}
- The
.env
parses without regular expressions or unstable tricks. - Errors on
stderr
produced by#comments
inside the.env
will be redirected tostdin
(2>&1) - Undesired prints of all
export
anderror
now onstdin
too, are redirected to/dev/null
. This prevents console flood. - Those errors do not prevent
.env
being parsed correctly.
We can define STACK_NAME
in our .env
but we can also pass our customized stack_name
. deploy.sh <stack_name> (stack_name opcional)
This workaround gave me headaches for 3 nights