Docker build args and environment variables are two ways of passing information to a Docker file during the build process. Both ways can be used to customize the behavior of a Docker container, such as changing configuration settings, passing credentials, or setting environment-specific parameters. However, they have different purposes and uses.
This write-up will describe the following:
What is the Usage of Docker Build Args?
Docker build args are utilized for setting values that are only required during the build process of a Docker image. They also permit users to customize the behavior of the Docker image without modifying the Docker file itself.
Build args can be set in the Docker file using the “ARG” instructions or can be passed as an argument to the “docker build” command using the “–build-arg” flag.
Example 1: Defining Build Args in Docker file
Docker Build Arg to specify the version of a specific package to install. In the below code, the “PKG_VERSION” build arg is used to specify the version of the “new-pkg” package that needs to be installed:
ARG PKG_VERSION
RUN apt install new-pkg=$PKG_VERSION
Example 2: Defining Build Args in the “docker build” Command
The default value of the “ARG” in the “docker build” command can be overridden via the “–build-arg” option. In the below-listed command, the “–build-arg” pass a build-time argument to the Docker file and the argument “version” is set to “0.5”:
The “–build-arg” option is used to pass a build-time argument to the Docker file. In this case, the argument “version” is set to “0.5.”
What is the Usage of Docker Environment Variables?
Docker environment variables are utilized to specify values that are available at runtime for a Docker container. They can store information about the system, application, or user. They allow users to customize the program’s behavior, pass arguments between processes, or access system settings.
Environment variables can be defined in a Docker file using the “ENV” instruction or can be passed as an argument in the “docker run” command using the “-e” or “–env” flag.
Example 1: Defining Environment Variable in Docker file
The environment variable can be used in a Docker file to specify the port on which the web server should listen. In the below code, the “PORT” environment variable is utilized to specify the “8080” port that the web server listens on in the “EXPOSE” instruction:
ENV PORT=8080
EXPOSE $PORT
Example 2: Defining Environment Variable in the “docker run” Command
Users can also specify the environment variable in the “docker run” command through the “–env” or “-e” flag.
This can also override the environment variable defined that is already defined in the Docker file. In the below example, the “PORT_NUMBER” environment variable is set to the value of “8080”:
We have efficiently explained the usage of Docker build args and environment variables.
Conclusion
Docker build args are utilized to pass values that are only needed during the build phase, such as a version number. They are defined with the “ARG” instruction in the Docker file, and they can be set with the “–build-arg” flag in the “docker build” command. Environment variables are utilized to pass values that are needed by the running container, such as a database URL or a port number. They are defined with the “ENV” instruction in the Docker file and can be overridden with the “-e” or “–env” flag in the “docker run” command.