迹忆客 EN >

当前位置:主页 > 学无止境 > 操作系统 > Docker >

带有多个参数的 Docker 构建命令

作者:迹忆客 最近更新:2023/03/22 浏览次数:

docker build 命令允许我们从 Dockerfile 创建 Docker 映像。它还允许我们从引用一组文件的上下文构建镜像,这些文件位于 PATH 或 URL 指定的位置。

另一方面,Dockerfile 只是一个只读文本文档,其中包含在组装我们的 Docker 映像时将调用的指令。Docker 映像是我们用来构建 Docker 容器的指令集、应用程序代码、依赖项、工具和库。

本文演示了如何使用 docker build 命令和多个参数。

带有多个参数的 docker build 命令

docker build 命令通常在终端或命令行中执行。

语法:

docker build [options] <directory path or URL>

PATH 或 URL 是指上下文中的一组文件,其中可能包括资源,例如预打包的 tarball 上下文、纯文本文件,甚至是 git 存储库。如果所有文件都存储在本地目录中,即与 Dockerfile 相同的目录,我们可以选择使用 . 作为路径。

此路径指定用于 docker 守护程序上的构建上下文的文件。

docker build .

我们可以在构建期间在 options 参数下传递多个参数。使用 –build-arg 标签,我们可以设置用户可以在构建时设置的值。

然而,我们首先需要在我们的 Dockerfile 中定义一个 ARG 指令,然后再使用 --build-arg 标签来构建镜像。我们可以像访问常规环境变量一样访问这个构建时变量;但是,它们在构建映像后不会持续存在。

虽然 ARG 指令的范围有限,但可以在指定基本映像之前声明它。我们还可以通过使用单独的 --build-arg 标签分别传递每个参数来传递多个构建参数。

语法:

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

我们有一个简单的 Dockerfile,它在以下示例中定义了一些 ARG 值。在这种情况下,我们没有设置它们的默认值。

Docker 文件如下所示。

#  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 标签传递值。

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

使用上面的命令构建镜像将在其他文件旁边打印消息 Hello Java Developer,如下所示。

                                                                                                           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

我们还可以使用多个 --build-arg 标签将 docker build 与多个命令一起使用。

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

这将在下方和其他消息旁边打印消息。虽然这是非常基本的,但这是在从 Dockerfile 构建 Docker 映像时传递多个命令的方式。

输出:

 => [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

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Solution to incorrect access log time when deploying Nginx in Docker

发布时间:2025/03/26 浏览次数:165 分类: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

发布时间:2025/03/26 浏览次数:131 分类: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

发布时间:2025/03/26 浏览次数:107 分类: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

发布时间:2025/03/26 浏览次数:97 分类: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

发布时间:2025/03/26 浏览次数:124 分类: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

发布时间:2025/03/26 浏览次数:202 分类: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

发布时间:2025/03/26 浏览次数:87 分类: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

发布时间:2025/03/26 浏览次数:140 分类: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

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便