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.

In the previous posts, already we have explained the below topics. Refer those links to understand this topic from basics.

What is Docker - Get Started from Basics - Docker Tutorial
What is Container, What is Docker on Container - Get Started
How to Install Docker on CentOS 7 / RHEL 7
Docker Images Explained with Examples - Docker Tutorial
Dockerfile Instructions Explained with Examples
Docker ONBUILD Command Explained with Examples
Docker ARG vs ENV Command Differences Explained in Detail
Docker CMD & ENTRYPOINT Differences Explained in Detail

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.

[root@docker-host ~]# docker run -d -it --name test_container1
[root@docker-host ~]# docker exec -it test_container1 /bin/bash
[root@8f298a841fb9 /]# env
[root@8f298a841fb9 /]#

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.
Support Us: Share with your friends and groups.

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

Post a Comment

Previous Post Next Post