Docker build command with multiple parameters
docker build
The command allows us to create a Docker image from a Dockerfile. It also allows us to build an image from a context that references a set of files located in a location specified by a PATH or URL.
A Dockerfile, on the other hand, is just a read-only text document that contains instructions that will be called when assembling our Docker image. A Docker image is the set of instructions, application code, dependencies, tools, and libraries that we use to build a Docker container.
This article demonstrates how to use docker build
the command with multiple parameters.
docker build
Commands
with multiple parameters
docker build
Commands are usually executed in a terminal or command line.
grammar:
docker build [options] <directory path or URL>
PATH or URL refers to a set of files in a context, which may include resources such as a pre-packaged tarball context, a plain text file, or even a git repository. If all files are stored in a local directory, that is, the same directory as the Dockerfile, we can choose to use .
as the path.
This path specifies the file used for the build context on the docker daemon.
docker build .
We can pass multiple parameters during build under options parameter. Using –build-arg
tag, we can set the values which can be set by the user at build time.
However, we first need to define a in our Dockerfile ARG
instruction, and then use --build-arg
the tag to build the image. We can access this build-time variable like regular environment variables; however, they do not persist after the image is built.
Although the ARG directive has limited scope, it can be declared before specifying the base image. We can also --build-arg
pass multiple build arguments by passing each argument separately using a separate tag.
grammar:
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 is as follows.
# 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
-build-arg
Therefore, we should use tags to pass values
when building images .
~/my-app$ docker build --build-arg language=Java .
Building the image using the above command will print messages alongside other files Hello Java Developer
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 multiple --build-arg
tags to docker build
use with multiple commands.
/my-app$ docker build \
> --build-arg language=Java \
> --build-arg name=Tonyloi \
> .
This will print the message below and next to other messages. Although this is very basic, this is how you 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
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Copy files from host to Docker container
Publish Date:2025/03/25 Views:126 Category:Docker
-
This article will discuss and demonstrate methods we can use to transfer files from the host to a running container in Docker. docker cp Copy the file from the host to the Docker container using docker cp The command is one of the simplest
Get the IP address of the Docker container
Publish Date:2025/03/25 Views:102 Category:Docker
-
This article demonstrates how to get the IP address of a Docker container. Connect to the Bridge network and get the IP address of the Docker container One of the big reasons why docker containers are so convenient is that we can easily con
Uninstalling Docker on macOS
Publish Date:2025/03/25 Views:95 Category:Docker
-
Recently, we have seen widespread adoption of Docker as the ultimate containerization platform. Because of this, setting up Docker on all platforms has been greatly simplified, including macOS and Windows. However, some users usually face p
Enter the Docker container's shell
Publish Date:2025/03/25 Views:98 Category:Docker
-
This article will demonstrate how to enter the Docker container shell using multiple methods. Use docker exec to enter the Docker container's shell We need to have a container up and running to use this command. We can check the status of t
Listing containers in Docker
Publish Date:2025/03/25 Views:122 Category:Docker
-
This article will discuss various commands for listing containers created in our system. This means we should create some containers beforehand for these commands to return values. List all running containers in Docker We will start by list
Mount the host directory into the Docker container
Publish Date:2025/03/25 Views:189 Category:Docker
-
Docker provides, among other features, tools to work with the host operating system and the container file system. One of these features is the ability to persist data in containers and share data between containers by mounting directories
Tagging images with Docker and Docker Compose
Publish Date:2025/03/25 Views:70 Category:Docker
-
When creating a Docker image, it is best practice to give it a descriptive and meaningful name. This process makes identifying and managing images more manageable, especially when dealing with many images. This article discusses tagging ima
Creating a database user with Docker Postgres
Publish Date:2025/03/24 Views:166 Category:Docker
-
When developing applications, we usually use database management systems such as PostgreSQL, MySQL, MongoDB, etc. to record application data. Docker helps us run an instance of these application database management systems. This helps save
Adding unsafe registry keys in Docker
Publish Date:2025/03/24 Views:130 Category:Docker
-
While it is highly recommended to secure your registry using a Transport Layer Security (TLS) certificate issued by a known Certificate Authority (CA), we have the option of using our insecure registry over an unencrypted Hypertext Transfer