Docker ARG vs ENV Command Differences Explained in Detail

This tutorial post will help you to understand about Docker ARG vs ENV command instructions available in dockerfile. Also refer the earlier post that explains the differences between Docker CMD and Docker ENTRYPOINT with examples.

To know more about other dockerfile instructions, refer this link Dockerfile Instructions Explained with Examples.

If you are interested in learning, Request you to go through the below recommended tutorial.

Docker ARG vs ENV Command Differences Explained in Detail

Docker ARG vs ENV command instructions are used to set environment variables. Both used for same purposes, but actually not same in functionality. So we must be aware of which instruction to be used for our requirement.

Docker ARG vs ENV Command Differences Explained in Detail

Example of ARG Instruction:

ARG JAVA_HOME=/opt/java
ARG app_version=2.0

Example of ENV Instruction:

ENV JAVA_HOME=/opt/java
ENV app_version=2.0

These variables will be set only during the image build process if it is specified with ARG instruction, But when you specify the same variables with ENV instruction, variables will be available during the image build also after running the docker container. It is completely based on our requirement which one to be used in dockerfile.

Let’s take an example to understand Docker ARG vs ENV Command instructions better.

Assume that, we want to set variables “JAVA_HOME” and “JAVA_VERSION” with the values “/opt/java” and “2.0” respectively.

Here is a dockerfile instructions, in which we have used ARG instruction for JAVA_HOME and ENV instruction for JAVA_VERSION for setting a variables.

MAINTAINER Devops Engineer
ARG JAVA_HOME=/opt/java

Image has already built using “docker build .” command and created a image tag “” using “docker tag” command.

Testing the Containers

Lets run a container with newly created image ( and check the variables set or not.

[[email protected] ~]# docker run -d -it –name test_container1
[[email protected] ~]# docker exec -it test_container1 /bin/bash
[[email protected] /]# env
[[email protected] /]#

Above output of the container clearly shows that only “JAVA_VERSION” is set to “2.0” which was specified with ENV instruction. But “JAVA_HOME” is not set as a variable within container which was specified with ARG instruction.

So Docker ARG can be used for temporary variable assignment and Docker ENV can be used for permanent variable assignment.

Hope you have got an idea about Docker ARG vs ENV command instructions.. Going forward, we will play more with docker tool.

Keep practicing and have fun. Leave your comments if any.

Support Us: Share with your friends and groups.

Stay connected with us on social networking sites, Thank you.