I have a service that sends emails. I'd rather not to keep my Gmail password in any file. After googling I found that I can provide environment variables to the containers with --build-arg param while building them.
The Dockerfile file
FROM node:latest
ARG buildtime_variable=default_value
ENV env_var_name=$buildtime_variable
...
CMD [ "node", "dist/server.js" ]
The build command
$ docker build --build-arg buildtime_variable=new_value .
List environment variables in running container
$ docker exec app env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=38dd21dbeda3
NODE_VERSION=11.10.0
YARN_VERSION=1.13.0
env_var_name=default_value
HOME=/root
Why as a result the env_var_name still equal to default_value or how I can inject the new_value to the container?
Thanks a lot for each answer!
Just use the docker-compose instead of docker command:
docker-compose build --build-arg buildtime_variable=new_value <container name>
Enjoy!