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
Get the IP address of the Docker container from the host using docker inspect
Publish Date:2025/03/26 Views:103 Category:Docker
-
Docker containers are not just for isolation—they are often used to manage processes that still need to communicate directly with each other. However, to communicate, you usually need to know the IP address of each container, which you ca
Solution to incorrect access log time when deploying Nginx in Docker
Publish Date:2025/03/26 Views:165 Category:Docker
-
In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t
Docker deploys nginx php application
Publish Date:2025/03/26 Views:131 Category:Docker
-
I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam
How to use Docker to image a Node.js web application
Publish Date:2025/03/26 Views:107 Category:Docker
-
Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at
Start a Bash terminal in a new Docker container
Publish Date:2025/03/26 Views:97 Category:Docker
-
Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers
Passing environment variables to containers in Docker
Publish Date:2025/03/26 Views:125 Category:Docker
-
This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t
Install Docker using Homebrew
Publish Date:2025/03/26 Views:202 Category:Docker
-
There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide
Enforce clean build of images in Docker
Publish Date:2025/03/26 Views:88 Category:Docker
-
This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho
Running a Docker instance from a Dockerfile
Publish Date:2025/03/26 Views:140 Category:Docker
-
Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application