Docker: “build” Requires 1 Argument Error – Docker: “build”需要1个参数错误

最后修改: 2022年 4月 4日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Docker is an open-source container platform. It allows us to package applications into containers and standardize the executable components that combine application source code with the operating system. It is a software development toolkit for building, sharing, and running individual containers.

Docker是一个开源的容器平台。它允许我们将应用程序打包成容器,并对将应用程序源代码与操作系统相结合的可执行组件进行标准化。它是一个用于构建、共享和运行单个容器的软件开发工具包。

Dockerfile is a text file that contains a list of commands which can be used to build an image. It is the simplest way to automate image creation.

Dockerfile是一个文本文件,其中包含一个可用于建立镜像的命令列表。它是自动创建镜像的最简单方法。

One of the benefits of Dockerfile is that we just need to write commands equivalent to Linux shell commands, so we don’t need to learn any new syntax for it.

Dockerfile的一个好处是,我们只需要编写相当于Linux shell命令的命令,所以我们不需要为此学习任何新的语法。。

We may face different issues while building an image using the Dockerfile. Here in this tutorial, we will learn to solve a very common Docker build issue.

在使用Docker文件构建镜像时,我们可能会面临不同的问题。在本教程中,我们将学习如何解决一个非常常见的Docker构建问题。

Let’s first understand the error “Docker build Requires 1 Argument”.

我们先来了解一下 “Docker build Requires 1 Argument “这个错误。

2. Understanding the Problem

2.了解问题

In this section, first, we‘ll create a sample Dockerfile to reproduce the error and then use different approaches to resolve it. Let‘s create a new Dockerfile named “Dockerfile“ with the following content:

在这一部分,首先。我们‘会创建一个样本Docker文件来重现这个错误,然后使用不同的approaches to 解决 it。让‘s创建一个新的Docker文件,名为“Dockerfile“,内容如下。

FROM        centos:7
RUN         yum -y install wget \
            && yum -y install unzip \
            && yum install -y nc \
            && yum -y install httpd && \
            && yum clean all
EXPOSE      6379
ENTRYPOINT  ["ping"]
CMD  ["google.com"]

Here, we have written a sample Dockerfile that uses a “centos:7” as a base image. Hence, it supports all the commands of centos.

在这里,我们写了一个Docker文件样本,使用 “centos:7” 作为基础镜像。因此,它支持centos的所有命令。

We have also installed some other utility commands of the Docker into the Dockerfile.

我们还在Docker文件中安装了Docker的一些其他实用命令。

The command to reproduce the issue “Docker build Requires 1 Argument” is:

重现 “Docker build Requires 1 Argument “问题的命令是。

$ docker build
"docker build" requires exactly 1 argument.
See 'docker build --help'.
Usage:  docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile

This issue will also occur if we use the Docker build command with different options.

如果我们使用不同选项的Docker构建命令,也会出现这个问题。

Let’s explore one of the options:

让我们来探讨其中一个选项。

$ docker build -t test_image/centos
"docker build" requires exactly 1 argument.
See 'docker build --help'.
Usage:  docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile

Here, in both cases, we faced the issue of “Docker build Requires 1 Argument”.

在这里,在两种情况下,我们都面临 “Docker构建需要1个参数 “的问题。

3. Different Ways to Build a Docker Image

3.构建Docker镜像的不同方法

Before, we move forward to solve the error “Docker build requires 1 Argument”, let’s first understand the Docker build command using different options.

在解决 “Docker build需要1个参数 “的错误之前,我们首先了解一下使用不同选项的Docker build命令。

The Docker build is run by the Docker daemon. Firstly Docker build sends the entire context to the daemon. The ideal situation is to start with an empty directory and add only the files needed to build the Docker image using the Dockerfile:

Docker build是由Docker daemon运行的。首先,Docker build会将整个上下文发送给守护进程。最理想的情况是以一个空目录开始,只添加使用Dockerfile构建Docker镜像所需的文件:。

$ docker build .
Sending build context to Docker daemon  2.048kB
Step 1/4 : FROM        centos:7
14.04: Pulling from library/centos
2e6e20c8e2e6: Extracting [============>                                      ]  17.83MB/70.69MB
0551a797c01d: Download complete 
512123a864da: Download complete 

We can also use -f flag to build an image by pointing a Dockerfile from a particular location:

我们还可以使用-f标志,通过指向特定位置的Dockerfile来构建镜像。

$ docker build -f /root/dockerImage/Dockerfile .

In order to tag an image, we can use -t option with the Docker build command:

为了标记一个图像,我们可以在Docker构建命令中使用-t选项。

$ docker build -t test_image/centos .

We can also pass build arguments with Docker build command:

我们也可以用Docker build命令传递构建参数。

$ docker build -t test_image/centos --build-arg JAVA_ENV=1.8  .

To clean up the build of an image, we can use –no-cache option in the command:

为了清理图像的构建,我们可以在命令中使用-no-cache选项。

$ docker build -t test_image/centos --build-arg JAVA_ENV=1.8  --no-cache .

In the older version of the Docker, we needed to pass –no-cache=true, but it is not in the case of newer versions. We can also create a Dockerfile without having the file name as Dockerfile:

在旧版本的Docker中,我们需要传递-no-cache=true,但在新版本中则不需要。我们也可以创建一个Dockerfile,而不把文件名设为Dockerfile:。

$ docker build -f /root/dockerImage/DockerFile_JAVA .

Here we created a Docker image using DockerFile_JAVA file name.

这里我们使用DockerFile_JAVA文件名创建了一个Docker镜像。

4. Due to Insufficient Arguments

4.由于论据不充分

The most common reason for “Docker build Requires 1 Argument” error is when we try to build the image without providing sufficient arguments. Here, in the arguments, we need to provide the directory with the command.

“Docker build Requires 1 Argument “错误最常见的原因是,我们试图在没有提供足够参数的情况下构建镜像。这里,在参数中,我们需要提供命令的目录。

By default we provided dot(.) in the command which specifies the Docker daemon to use the shell’s current working directory as the build context:

默认情况下,我们在命令中提供了dot(.),指定Docker守护进程使用shell的当前工作目录作为构建环境。

$ docker build .

The dot(.) basically tells the Docker that Dockerfile has to be used from the current directory. We can also change the Docker build context using the following command:

点(.)基本上告诉Docker,Dockerfile必须从当前目录中使用。我们还可以使用以下命令来改变Docker的构建环境。

$ docker build /root/test

Another issue related to Docker build that we may face:

另一个与Docker构建有关的问题,我们可能面临。

$ docker build -f /root/test/Dockerfile2 .
unable to prepare context: unable to evaluate symlinks in Dockerfile path:
  lstat /root/test/Dockerfile2: no such file or directory

Here in the above command, we are trying to build a Docker image using the file name “Dockerfile2”, if this file is not present in the current directory, we will get the following error:

在上述命令中,我们试图使用文件名 “Dockerfile2 “来构建一个Docker镜像,如果这个文件不在当前目录中,我们将得到以下错误。

unable to prepare context: unable to evaluate symlinks in Dockerfile path:
  lstat /root/test/Dockerfile2: no such file or directory

In order to solve this issue, we need to provide the correct file name with -f option.

为了解决这个问题,我们需要用-f选项提供正确的文件名。

5. Conclusion

5.总结

In this tutorial, we learned about the issues related to the Docker build command.

在本教程中,我们了解了与Docker构建命令有关的问题。

Initially, we explored different ways of building a Docker image, then we addressed the issue with the Docker build arguments, and finally, we learned a few more issues related to the Docker build command.

最初,我们探索了构建Docker镜像的不同方法,然后我们解决了Docker构建参数的问题,最后,我们又了解了一些与Docker构建命令有关的问题。