Docker ARG vs ENV

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.

Master Docker ARG and ENV in 5 Days!

Sign up to the free 5-day email course, and learn more about using environment and build-time variables with Docker. Get actionable advice, best practices and nifty tricks to use for your next project.

Privacy and your data: I use the US-provider ConvertKit as my email marketing automation platform. By clicking to submit this form, you acknowledge that the information you provide will be transferred to ConvertKit for processing in accordance with their Privacy Policy and Terms.

You can get more information about the usage of your data, logging of your registration, sending out mails with the US-provider ConvertKit, statistical analysis around emails and the possibility to unsubscribe in my Privacy Policy.

We won't send you spam. Unsubscribe at any time. Powered by ConvertKit