Docker ARG vs ENV Command Differences Explained in Detail

dockerfile, dockerfile arg vs env, arg vs env, docker command, docker arg, docker env, docker arg vs env, docker arg vs env examples, arg vs env differences

This post explains you the detailed differences between Docker ARG and Docker 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.

Docker ARG and Docker 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 explain this 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 and ENV command instructions.. Going forward, we will play more with docker tool. Keep practicing and have fun.

Also refer other articles,

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
How to Write Dockerfile To Build Your Own Docker Images
Docker ONBUILD Command Explained with Examples
Docker ARG vs ENV Command Differences Explained in Detail
Docker CMD & ENTRYPOINT Differences Explained in Detail

Support Us: Share with your friends and groups.

Stay connected with us on social networking sites, Thank you.
YouTube | Facebook | Twitter | Pinterest | Rss
Keywords : dockerfile, docker file, docker file arg, dockerfile env, arg vs env, docker command, docker arg, docker env, docker arg vs env, docker arg vs env examples, dockerfile arg and env differences, dockerfile arg vs env examples, difference arg vs env, writing dockerfiles, dockerfile examples, dockerfile arg examples, dockerfile env examples, dockerfile arg arguments, dockerfile env arguments, dockerfile arg explained, dockerfile env explained, docker arg command, docker arg command examples, docker env command, docker env command examples
June 28, 2018

Post a Comment


Contact Form


Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget