Docker Build Arguments and Environment Variables

Docker provides different ways to inject the dynamic values during the build phase and the run phase. we can use Docker build arguments to dynamically injecting the values at the build time and environment variable to inject values at run time and can combine both to maximize their benefit.

Docker build arguments

It is used to dynamically injecting the values during the build time. It gives us the flexibility to create an image in different modes. Build arguments are used when we need to create multiple images with a specific value difference. So we can pass that value as a build argument and can use the same docker image.

Defining build arguments in Dockerfile

we can add an argument step in the Dockerfile like

ARG VERSION=0.1
COPY $VERSION .ORCOPY ${VERSION} .
ARG VERSION

Defining build argument in the docker build command

We can override the default value of the ARG in the build command using the --build-arg command.

docker build -t hello-world:latest --build-arg version= 0.2 .

Defining multiple docker arguments

In Dockerfile, we can define multiple build arguments with multiple ARG commands like

ARG VERSION
ARG PORT
docker build -t hello-world:latest --build-arg VERSION=0.2 --build-arg PORT=80 .

Using Build Arguments in the docker-compose file.

We can declare the build arguments in the Dockerfile and can set the value in the docker-compose file.

ARG VERSION=0.1
ARG PORT=8080
build:
context: .
args:
VERSION: 1
PORT: 9090
build:
context: .
args:
VERSION
PORT

Docker environment variables

Docker environment variables are used to make the docker run command flexible, it gives the user flexibility to change certain values at the run time. The environment variables can be accessed in the application code as well. It provides a way to set application configuration at the time of running the application, a configuration like database details, the port number to expose, etc.

process.env.PORT
System.getenv("PORT")

Defining environment variables in Dockerfile

we can directly define the environment variables in the Dockerfile

ENV PORT_NUMBER=8080
EXPOSE $PORT_NUMBEROR EXPOSE ${PORT_NUMBER}
ARG PORTENV PORT_NUMBER=$PORT

Defining environment variable in the docker run command

The other way to define the environment variable is to pass it in the docker run command using --env tag or -e tag.

 docker run --env PORT_NUMBER=8080 <image-name>OR docker run -e PORT_NUMBER=8080 <image-name>

Defining multiple environment variables

We can define multiple environment variables in the Dockerfile like

ENV PORT_NUMBER=8080
ENV HOST_NAME=localhost
ENV DB_NAME=MySQL
docker run -e PORT_NUMBER=8080 -e HOST_NAME=localhost -e DB_NAME= MySQL <image-name>

Environment variable file

It is used to separate the Dockerfile and environment variables, it gives us the flexibility to use different environment variables for running different containers using the same Dockerfile.

PORT_NUMBER=8080
HOST_NAME=localhost
DB_NAME=MySQL
docker run --env-file ./.env <IMAGE-NAME>

Environment variables in the docker-compose file

Environment variables can be used in the docker-compose file in several ways. The most common way is to add the environment variables in the file (.env), docker-compose file directly reads for it. The file must be present at the same level as the docker-compose file.

TAG: 7.3
version: '3'
services:
web:
image: 'webapp:${TAG}'
docker-compose --env-file ./config/.env.dev up docker-compose --env-file ./config/.env.prod up docker-compose --env-file ./config/.env.test up
version:3
services:
node:
environment:
- PORT: 9090
version:3
services:
node:
env_file:
- /config/.env.test
version:3
services:
node:
env_file:
- /config/.env.test1
- /config/.env.test2
version:3
services:
node:
environment:
- PORT
docker-compose run -e PORT=9090 node
docker-compose run -e PORT=9090 node java

Sample Dockerfile using both Build arguments and Environment variable

FROM node:alpine
ARG APP_DIR
WORKDIR $APP_DIR
COPY package.json .
RUN npm install
COPY . .ENV PORT_NUMBER=80
EXPOSE PORT $PORT_NUMBER
CMD ["npm", "install"]

Difference between Build Arguments and Environment variables

  1. Build arguments and the environment variable both provides a level of flexibility but at different phases. We cannot replace one with another, in fact, we can use them together to get higher flexibility.
  2. Build arguments works on the BUILD phase, it has no impact on the RUN phase of docker whereas the ENV can be used at both phases. Environment variables declared in Dockerfile are available at both the phases but environment variables declared in the environment file or passed in the RUN command are available at only the RUN phase.
  3. Build arguments cannot be used in the application code to be containerized, but we can use environment variables in the application code.

Full stack developer