Published on

Set Docker Environment Variables during Build Time


When using that ARG option in your Dockerfile, you can specify the --build-args option to define the value for the key that you specify in your Dockerfile to use for a environment variable as an example.

Today we will use the arg and env to set environment variables at build time.

The Dockerfile:

Our Dockerfile

FROM alpine:edge
CMD ["sh", "-c", "echo env var: ${OWNER}"]

Building our Image, we will pass the value to our NAME argument:

$ docker build --build-arg NAME=james -t ruan:test .

Now when we run our container, we will notice that the build time argument has passed through to our environment variable from the running container:

$ docker run -it ruan:test 
env var: james

When we build the image without specifying build arguments, and running the container:

$ docker build -t ruan:test .
$ docker run -it ruan:test 
env var: NOT_DEFINED

Thank You

Thanks for reading, feel free to check out my website, and subscribe to my newsletter or follow me at @ruanbekker on Twitter.

Please feel free to show support by, sharing this post, making a donation, subscribing or reach out to me if you want me to demo and write up on any specific tech topic.

Buy Me A Coffee