Make Your Dockerfile Dynamic, ARG vs ENV

Tokerto
Rexven
Published in
3 min readDec 26, 2023

Docker has been a great tool for containerizing and isolating your projects. It enables you to work on your project without considering about the project environment, operating system and other stuff. It presents a solution to the most popular developer problem “It works on my machine!”.

In the development process, using variables plays a crucial role in order to maintain a dynamic and scalable project. Because of its isolated nature, Docker images and containers have different handling mechanisms of using environment variables. This article will explain ARGuments and ENVironment variables shortly.

ARGument (ARG)

Arguments are the variables that can be used in build time which means that only the Dockerfile can use it when the image is built. After running a container based on the image, these arguments cannot be used in the project code or elsewhere. Moreover, in Dockerfile, every command is considered as a layer and at the last layer, I mean the CMD command, these arguments cannot be used. Because, CMD command is the command that will be used when the container is run and does not work in the build time.

By setting the ARG in the Dockerfile, you can set the default value of the argument. But while building the image, it is possible to change the value of the argument by adding “ — build-arg NODE_VERSION=newValue”.

docker build -t example --build-arg NODE_VERSION=14 .

The important point is that ARG declaration before the FROM statement can be done but ONLY FROM statement can use it. An ARG declared before a FROM is outside of a build stage, so it can't be used in any instruction after a FROM. So make sure that you are using ARG after the FROM statement.

ENVIRONMENT VARIABLES (ENV)

Environment variables are the variables which can be used in both build and run time. It is possible to set the variables in the Dockerfile before running the container and use them in your project without any problem. Unlike ARG, ENVs can be used in the CMD command and in the project code.

Here, it is possible to use this PORT variable in your node project and start your project on port 8000. Env variables can be overridden when starting a container. You can use the following command to achieve this.

docker run -p 8001:8001 --rm -d --name example-container -e PORT=8001 example-image

Besides all these, if there is an .env file, use that file via this command.

docker run -p 8001:8001 --rm -d --name example-container --env-file=./.env example-image

As it is explained, there are two ways of declaring and using variables in Dockerfile. While ARG can only be used inside Dockerfile and only in build time, ENV can be used both in the build time and run time which makes it more useful than ARG. One should be aware of the fact that secrets must not be used in Dockerfile ENV because it will cause security issues about your credentials. Thanks for reading so far, claps will be appreciated.

If you find it helpful, you can buy me coffee.

--

--