Multiple ways to create docker variables with examples

This tutorial explains Setting environment variables in Docker

There are two types of setting variables

  • Docker ARG directive ARG directions are used to create Variables and assign them with default values.

These are also called build variables. This will be available during the Docker build, Not available at Runtime and container.

  • Docker Env directive These are environment variables and are available during docker container execution also called runtime variables

How to set environment variables in Docker build

You can create a variable using ARG command

Here is a syntax to declare a variable


VARIABLE_VALUE is an optional value,

Here is an example that added to DockerFile

FROM ubuntu
ARG environment=development
RUN echo $environment

This variable can be set using --build-arg during the docker image build.

docker build -t testcontainer --build-arg environment=production .

Docker builds the image using the variable passed from the command line.

If you pass a variable value from the command line, It overrides the default value. If the variable is passed from the command line, the Default value assigned consider.

What is the default scope of variables declared with Docker ARG? Variables declared with ARG have scopes. Variables are accessed from the line where it is declared to the end of the file. It is not used in other Dockerfile.

You can pass multiple variables with this command as given below

docker build -t testcontainer --build-arg environment=production -build-arg test=false .

Docker Environment variables

Environment variables are runtime variables that are used during Docker image container execution. these are also runtime variables

Declare these variables using the ENV command directive in Dockerfile


You can pass these variables using the command line with the -e option for docker run

docker run -e MYSQL_USERNAME=root -e MYSQL_PASSWORD=root

This approach has the following disadvantages

  1. complete the command shown to the user during the ps command execution
  2. Variables are not secure and are shown to the user with these commands

The first approach can be avoided using variables set using the below command


The second approach to secure environment files, move all the environment variables to secure the env file Created a config.env file that contains all variables


you can pass this file using the --env-file option to the docker run command

docker run --env-file config.env

What is an ARG command in Docker?

ARG Directive is used to define the variables with default values, These are available during the docker build.

You can change or override its value using the docker build command with the argument --build-arg

docker build -t testcontainer --build-arg environment=production .

What is an ENV command in Docker?

ENV command is used to pass environment variables to Dockerfile. These are available during runtime with the docker run.

You can change or override its value using the docker run command with argument -e

docker build -t testcontainer -e environment=production .

Difference between ARG and ENV

Both are used to store the variable values during the docker build and execution process.

ARG commandENV command
store the variables for Docker Buildavailable to pass during docker image execution
Override default value with --build-arg using command lineOverride default value with -e using command line
It works docker build commandworks with docker run command