I have worked with docker for over 7 years now and I still get confused about the difference between environment variables and arguments. In this post I try to simplify the distinction and explain when to use what.
Whats the difference between ENV and ARG variables and why should I care?
ARG and ENV in Dockerfile
- ARG and ENV are ways to define variables in a Dockerfile.
ARG variables
- ARG variables are only available during the build process.
- ARG variables are used to pass values to the Dockerfile.
ENV variables
- ENV variables are available to running containers.
- ENV variables are used to set environment variables for running containers.
Using ARG and ENV together
- You can use ARG values to set ENV values. This is a common way to set default values for environment variables.
- For example, you could use an ARG variable to specify the name of a package to install, and then use an ENV variable to set the path to the installed package.
The following image from VSUPALOV illustrated this beautifully.
.
How do I use ARGs and ENV Vars?
Using ARGs
As stated above these need to be passed and defined before the build process. You would define an ARG in your Dockerfile
like this:
What this does is it lets Docker know that this image we are builidng should expect an argument called my_mighty_arg. A value for which we will pass when we are building our image like this:
so when we run this we should see something like this:
Thats it the ARG has served its purpose and will not be available beyond the docker build scope.
Using ENV VARS
On the other hand ENV VARS are available to the running container. Imagine passing information to your continaer via env var that populates itself through a build time ARG. You can define an ENV VAR in your Dockerfile
like this:
Another way is to simple pass the ENV VAR in the command line:
Conclustion
In conclusion, ARG and ENV variables are both useful tools for defining variables in a Dockerfile. ARG variables are only available during the build process and are used to pass values to the Dockerfile, while ENV variables are available to running containers and are used to set environment variables for running containers. You can use ARG values to set ENV values, which is a common way to set default values for environment variables. By understanding the difference between ARG and ENV variables, you can better manage your Docker images and containers and ensure that your applications run smoothly.
Photo by Philippe Oursel on Unsplash