Docker Build Command With Multiple Arguments

Docker Build Command With Multiple Arguments

The docker build command allows us to create Docker images from a Dockerfile. It also allows us to build an image from a context referring to a set of files located in a location specified by a PATH or URL.

On the other hand, a Dockerfile is simply a read-only text document with instructions that will be called when assembling our Docker image. A Docker image is an instruction set, application code, dependencies, tools, and libraries that we use to build a Docker container.

This tutorial demonstrates how to use the docker build command and multiple arguments.

the docker build Command With Multiple Arguments

The docker build command is usually executed in the terminal or command line.

Syntax:

docker build [options] <directory path or URL>

The PATH or URL refers to a set of files in a context that may include resources such as pre-packaged tarball context, plain text files, or even a git repository. If all files are stored in the local directory, i.e., the same directory as the Dockerfile, we can opt to use . as the path.

This path specifies the files used for the build context on the docker daemon.

docker build .

We can pass multiple arguments during build time under the options parameter. Using the –build-arg tag, we can set values that users can set at build-time.

However, we first need to define an ARG instruction in our Dockerfile before using the --build-arg tag to build the image. We can access this build-time variable like regular environmental variables; however, they do not persist after building the image.

Although the scope of the ARG instructions is limited, it can be declared before specifying the base image. We can also pass multiple build arguments by separately passing each argument with a separate --build-arg tag.

Syntax:

docker build -t <image-name>:<tag> --build-arg <key1>=<value1> --build-arg <key2>=<value2> .

We have a simple Dockerfile that defines some ARG values in the following example. In this case, we have not set their default values.

The Docker file looks as shown below.

#  base image
FROM python

ARG language
ARG name
# Set your working directory
WORKDIR /var/www/

# Copy the necessary files
COPY ./app.py /var/www/app.py

# Install the necessary packages
RUN pip install -r /var/www/requirements.txt

RUN echo "Hello $language Developer"
MAINTAINER Isaac Tonyloi

Therefore, we’ll be expected to pass the values using the -build-arg tags when building the image.

~/my-app$ docker build --build-arg language=Java .

Building an image using the command above will print the message Hello Java Developer alongside other files, as shown below.

                                                                                                           0.0s
 => [5/6] RUN pip install -r /var/www/requirements.txt                                                                                                                      100.4s
 => [6/6] RUN echo "Hello Java Developer"                                                                                                                                     4.4s
 => exporting to image                                                                                                                                                        3.8s
 => => exporting layers                                                                                                                                                       2.8s
 => => writing image sha256:22fa358b711d2ea3a1d72e59f062f6c7c38b414bdb253fb8d0def20222cadd93

We can also use docker build with multiple commands using multiple --build-arg tags.

/my-app$ docker build \
> --build-arg language=Java \
> --build-arg name=Tonyloi \
> .

This prints the message below and alongside other messages. Although this is very basic, this is how you can pass multiple commands when building a Docker image from a Dockerfile.

Output:

 => [2/2] RUN echo "Hello Java Developer and my name is Tonyloi"                                                                                                              3.3s
 => exporting to image                                                                                                                                                        3.0s
 => => exporting layers                                                                                                                                                       2.0s
 => => writing image sha256:d4d7c3b18aa9422be2990f5381a5423a18867dda8090dd4a4f166efc4e7c4ba2
Author: Isaac Tony
Isaac Tony avatar Isaac Tony avatar

Isaac Tony is a professional software developer and technical writer fascinated by Tech and productivity. He helps large technical organizations communicate their message clearly through writing.

LinkedIn

Related Article - Docker Command

  • The --rm Flag in Docker
  • Difference Between Copy and Add Commands in Dockerfile
  • Difference Between CMD and ENTRYPOINT in Docker