为什么在docker build命令中使用-f

时间:2019-03-20 11:29:17

标签: docker docker-build

我遵循this K8s tutorial,并且在文件中间,有以下说明:

12. Now let’s build an image, giving it a special name that points to our local cluster registry.

$docker build -t 127.0.0.1:30400/hello-kenzan:latest -f applications/hello-kenzan/Dockerfile applications/hello-kenzan

我不明白您为什么需要使用-f applications/hello-kenzan/Dockerfile指向dockerfile。

在docker build的人中:

-f, --file=PATH/Dockerfile
          Path to the Dockerfile to use. If the path is a relative path and you are
          building from a local directory, then the path must be relative to that
          directory. If you are building from a remote URL pointing to either a
          tarball or a Git repository, then the path must be relative to the root of
          the remote context. In all cases, the file must be within the build context.
          The default is Dockerfile.

所以-f指向dockerfile,但是我们已经在build命令-docker build ...applications/hello-kenzan的末尾给出了dockerfile的路径,那么为什么还要编写两次?我错过了什么吗?

2 个答案:

答案 0 :(得分:2)

这样做的原因是因为他可能有多个名为Dockerfile的文件,并且使用-f告诉docker不要在当前目录(如果有)中使用Dockerfile,而是在{{1 }}。

尽管在此特殊示例中没有必要执行此操作,但我感谢教程创建者向您展示了使用PATH的选项,并将-f指向特定位置以向您(想要学习的人)展示它是可以使用不在PATH中的Dockerfile(例如,当您有多个要使用其创建构建的dockerfile时,或者当文件的名称不是applications/hello-kenzan而是Dockerfile时)

答案 1 :(得分:1)

你是对的。在这种情况下,您不必使用-f选项。 official docs说:

-f:Dockerfile的名称(默认为“ PATH / Dockerfile”),并且由于给定的PATH为applications/hello-kenzan,因此将隐式找到Dockerfile。