Difference Between ARG and ENV in Docker Container
Last Updated :
23 Jul, 2025
Docker is a software application that allows developers to create, build and test software applications. These software applications are kept in small package units known as containers. Containers allow applications to run on any platform. Docker Engine hosts the containers. The engine has two parts: Docker Daemon and Docker CLI. These two parts use Rest APIs to communicate with one another.
What is Dockerfile?
A Dockerfile is considered the blueprint of the containers. It is a document that comprises all the necessary instructions that can be used to assemble the docker image. It includes the definition of packages or operating systems that can run the containerized applications. It also includes the port details to which the application would be exposed. The structure of the Dockerfile is as follows
FROM base image:tag
# Define build-time variable
ARG key=val
# Define runtime environment variables
ENV key=value
# Set the working directory to /name
WORKDIR /name
#Expose port number
EXPOSE port_number
# Copy the current directory contents into the container at /app
COPY . /app
# statements
RUN statement
# Step 8: Specify the default command to run when a container starts
CMD ["executable", "param1", "param2"]
ARG
ARG is a command that is used inside the Dockerfile. ARG or Argument are basically definitions of variables that can be defined during the build time of the variables. The values are not included in the final image and can be changed using the Docker build command.
Example of ARG
Let us illustrate the use of ARG. Here we have defined a Python file named main.py. We have defined the Dockerfile or the base image that has all the necessary details like version, package, file name, etc. The structure of DockerFile is as follows:
# Use Python image
FROM python:3.9-slim
# Define build-time variable
ARG APP_VERSION=1.0
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Print the build-time argument
RUN echo "Building application version: $APP_VERSION"
# Run main.py when the container launches
CMD ["python", "main.py"]
Now use the below command to build the application. The command is
docker build --build-arg APP_VERSION=2.0 -t myapp:2.0 .
In the above command we can change the version as it is of type ARG. We have also used tag to our image.
Now use the run command to run the Docker container.
docker run --rm myapp:2.0
The output is as follows
Use cases of ARG
There are different use cases of ARG. Some of them are as follows:
- ARG can be used for the installation of extra tools that might be required during the development and the production of applications.
- We can define Proxy settings using ARG field.
- To store extra information or metadata like date and version, ARG can be used for storing such necessary details.
- Since we can override the values of ARG during building of Docker image, we can customize as per our requirements.
Importance of ARG
The benefits of using ARG is as follows:
- ARG is used to build the environments without opening the Docker files. This can be done during the building of images.
- ARG is used to change the version numbers via Command line
- ARG can be used to reduce the size of Dockerfile images by excluding certain built in parameters.
ENV
ENV is an acronym for Environment variables. ENV is used inside Docker file that are available during the runtime of the containers. The task of the ENV is used in the configuration of the container's environment. They are included in the final image and have a major impact on running containers.
Example to illustrate the use of ENV
To run Docker locally, Docker Desktop needs to be installed. Here we have developed a BMI calculator using Python.
Now in the Dockerfile we have made use of ARG and ENV variables along with other packages which are to be used during the creation of the container.
The Python code is as follows:
# bmi_calculator.py
import os
def calculate_bmi(weight, height):
return weight / (height ** 2)
if __name__ == "__main__":
weight = float(os.getenv('WEIGHT', '70')) # default weight is 70 kg
height = float(os.getenv('HEIGHT', '1.75')) # default height is 1.75 meters
bmi = calculate_bmi(weight, height)
print(f"Weight: {weight} kg")
print(f"Height: {height} m")
print(f"BMI: {bmi:.2f}")
# Check if the image file exists
image_path = 'sample_image.png'
if os.path.isfile(image_path):
print(f"Image file '{image_path}' found.")
else:
print(f"Image file '{image_path}' not found.")
The Dockerfile is as follows:
# Use Python image
FROM python:3.9-slim
# Define build-time variable
ARG VERSION=1.0
# Define runtime environment variables
ENV WEIGHT=70
ENV HEIGHT=1.75
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Print the build-time argument
RUN echo "Building version: $VERSION"
# Print the environment variables
RUN echo "Default weight: $WEIGHT kg"
RUN echo "Default height: $HEIGHT m"
# Run bmi_calculator.py when the container launches
CMD ["python", "bmi_calculator.py"]
Let us go through line by line
- In the first line we are importing the Python base image. We are using the version of Python that has limited packages. Then we are specifying the version as 1 using ARG variable. The version can be overridden during the building of image of container.
- Now we are setting the value of weight and height variables as 70 and 1.75 using ENV. ENV ensures that the variables are available during the runtime of the container.
- In the third line we are setting the working directory as /app. Any changes made will be affected in that working directory.
- Using COPY means copying all the codes and necessary packages onto Docker Engine.
- Using RUN command we are printing the values of build time and environment variables.
- CMD is used when the container gets executed.
Now to build Docker image we are using the command
docker build --build-arg VERSION=2.0 -t bmi-calculator.
So from the above we can see that we are able to change the VERSION which is an ARG variable during the building of image. However we cannot change the ENV using Docker build. Now use docker run to create and run a container.
docker run bmi-calculator
Use Cases of ENV
Some Use Cases of ENV variables are as follows:
- To set options that can be accessed during the runtime, ENV variables can be used.
- We can provide additional directories using the ENV variables.
- To handle texts, dates and other values we can use ENV Lang for the same.
Importance of ENV
The benefits of using ENV is as follows:
- The variables which are defined using ENV can be accessed across multiple Dockerfile instructions.
- We can override ENV variables using -e during the running of containers.
- ENV can be used to store sensitive information such as passwords thereby providing advanced security measures.
ARG vs ENV
Build Time vs Run Time
Build Time means installation of packages and dependencies, compilation of Code etc. So basically Docker reads the instructions specified in the Dockerfile and produces image that can be later used to run containers. So ARG is used to change specifications if required during the build time
Run Time means Docker container is executing the application that has been defined inside. ENV is used to set the environment variables. These variables influence the behaviour of applications.
How and where they are used in Dockerfile
ARG is used for the definition of build time variables. The values can however be changed during the build time of images. They do not appear in the final images. It is usually defined at the starting of Docker file
On the other hand, ENV can be used to define environment variables that influence the behavior of applications during the runtime. They persist in the final images.
Examples demonstrating their distinct behaviors
The Example of using ARG in Dockerfile is as follows
FROM base image:tag
# Define build-time variable
ARG key=val
The use of ENV
ENV key=value
# Set the working directory to /name
WORKDIR /name
#Expose port number
EXPOSE port_number
# Copy the current directory contents into the container at /app
COPY . /app
Characteristics | ARG | ENV |
---|
Scope | Used during the build time | Used during the run time as well as build time |
---|
Command | Use the command docker build --build-arg or in DockerFile | Use the command docker run -e or define it in Dockerfile |
---|
Use | The values are passed during the build of the image | The values are available during the runtime of the containers. |
---|
Example | ARG Version=1 | ENV Version=1 |
---|
Similar Reads
DevOps Tutorial DevOps is a combination of two words: "Development" and "Operations." Itâs a modern approach where software developers and software operations teams work together throughout the entire software life cycle.The goals of DevOps are:Faster and continuous software releases.Reduces manual errors through a
7 min read
Introduction
What is DevOps ?DevOps is a modern way of working in software development in which the development team (who writes the code and builds the software) and the operations team (which sets up, runs, and manages the software) work together as a single team.Before DevOps, the development and operations teams worked sepa
10 min read
DevOps LifecycleThe DevOps lifecycle is a structured approach that integrates development (Dev) and operations (Ops) teams to streamline software delivery. It focuses on collaboration, automation, and continuous feedback across key phases planning, coding, building, testing, releasing, deploying, operating, and mon
10 min read
The Evolution of DevOps - 3 Major Trends for FutureDevOps is a software engineering culture and practice that aims to unify software development and operations. It is an approach to software development that emphasizes collaboration, communication, and integration between software developers and IT operations. DevOps has come a long way since its in
7 min read
Version Control
Continuous Integration (CI) & Continuous Deployment (CD)
Containerization
Orchestration
Infrastructure as Code (IaC)
Monitoring and Logging
Microsoft Teams vs Slack Both Microsoft Teams and Slack are the communication channels used by organizations to communicate with their employees. Microsoft Teams was developed in 2017 whereas Slack was created in 2013. Microsoft Teams is mainly used in large organizations and is integrated with Office 365 enhancing the feat
4 min read
Security in DevOps