使用 - < Dockerfile 构建 docker 图像时 COPY 指令出现问题

Problem with COPY intruction while building docker image with - < Dockerfile

我有 1 个文件夹,其中包含多个 docker 文件,我想指定要构建的文件。 在这种情况下,我应该使用

docker build - < Dockefilename

但它返回错误

docker copy failed stat /var/lib/docker/tmp/docker-builder/src/main.py: no such file or directory

但是。如果在文件夹中只留下一个 docker 文件,名为“Dockerfile”,并构建与

完全相同的 docker 文件
docker build .

它没有问题。 该错误是由 COPY 指令引起的。在构建时 -

编辑 复制的文件是否与 dockerfile 在同一目录中并不重要。无论如何都会出错。

这是预期的行为。

引用official doc的说法。

$ docker build - < Dockerfile

This will read a Dockerfile from STDIN without context. Due to the lack of a context, no contents of any local directory will be sent to the Docker daemon. Since there is no context, a Dockerfile ADD only works if it refers to a remote URL.

使用 -,docker 构建在构建过程中添加或复制文件时在没有上下文的情况下从 STDIN 读取导致问题。在 docker build . 命令的情况下,使用上下文,因此 copying/adding 文件有效。

此外,如果您想在构建期间指定特定的 docker 文件,请使用 -f option.