Docker ARG vs ENV

Understanding functionality and availability of environment and build-time variables in Dockerfiles.

[ docker ]

If you’re new to Docker, working with Dockerfiles and building images, you will notice that there are two ways to set variables - through the ENV and ARG keywords. Although both seem similar, they are fundamentally different and serve different purposes.

The following image, displays the lifetimes of build arguments and environment variables:

An overview of ARG and ENV availability.

Build arguments can be set to a default value inside of a Dockerfile, but also changed by providing a –build-arg argument when actually building an image. Those values are not available after the image is built however. A running container won’t have access to an ARG variable value.

ENV values are available during the image build phase, but also after a container is launched from the image. You can override ENV values which were set in a Dockerfile by providing environment variables in the command line or your docker-compose.yml file. Default values which were overridden in this way, will not be available inside of a container.

You can use ARG values, to set default values of ENV variables - you can read more about it here.

To flatten your future learning curve, it’s a good idea to make sure that you have a firm grasp on basic concepts around Docker. To get a complete understanding of ways to set variables when using Docker and docker-compose.yml files, check out this in-depth article.

Get My Weekly Newsletter

Weekly-ish articles about Kubernetes, Docker, DevOps, automation and deployment.

Free goodies too: get a list of my top 12 tools for deployment & automation and more exclusive content I only share with subscribers.