Docker 使用自定义路径和自定义文件名构建无效

Docker build with custom path and custom filename not working

我在 Windows 10 中使用 Docker 版本 19.03.2。

我有一个包含文件 DockerfileDockerfile_test 的目录 C:\docker_test。当我执行

PS C:\> docker build 'C:\docker_test'

在 PowerShell 中,图像是从 Dockerfile 构建的,正如预期的那样。但是,当我想像这样 Dockerfile_test 构建图像时

PS C:\> docker build -f Dockerfile_test 'C:\docker_test'

我明白了

unable to prepare context: unable to evaluate symlinks in Dockerfile path:
CreateFile C:\Dockerfile_test: The system cannot find the file specified.

虽然我指定了构建路径,但我不明白为什么 Docker 正在寻找 C:\Dockerfile_test

您应该像这样说明您的 Dockerfile 的路径(上下文)

PS C:\> docker build -f 'C:\docker_test\Dockerfile_test' 'C:\docker_test'

问题已经有了答案,但这里要详细一点

来自docs

The docker build command builds an image from a Dockerfile and a context. The build’s context is the set of files at a specified location PATH or URL

您使用 C:\docker_test 指定了上下文。

来自相同的文档

Traditionally, the Dockerfile is called Dockerfile and located in the root of the context. You use the -f flag with docker build to point to a Dockerfile anywhere in your file system.

因此,如果您指定 -f 标志,docker 将搜索给定的文件。在您的情况下,您有一个没有路径的文件,因此 docker 将在当前目录中搜索。

要使其正常工作,请使用@Arik 建议的命令