Passing environment variables to you Docker build
21 Jul 2020It can be useful to pass environment variable from your local environment to
your Docker build. This situation happened when I had to pass pypi keys to
install specific packages.
But since Docker is encapsulated, you need to take a couple
of steps to make it happen. Note that I’m assuming you’re building your Docker
image through a Dockerfile
- You need to pass the environment variables to your build command using the
flag
--build-arg
. For instance,docker build --build-arg DOCKER_ENV_VAR=$MY_LOCAL_ENV_VAR -f Dockerfile -t my_image:my_tag .
- You need to define these variables in your Dockerfile. Continuing on the
previous example, this would mean adding the following line in your
Dockerfile
:ARG DOCKER_ENV_VAR ... RUN apt-get install <something> --flag=$DOCKER_ENV_VAR
docker
]