关于docker build 命令中 path 参数的介绍

 

首先,我想说的是:网上的教程都是错的!

网上很多文章都是相互转载,其中用的示例都是一样的,都统一得出结论:“这里path参数是指镜像构建过程中的上下文环境的目录”。然后文章都给出官网文档

The docker build command builds Docker images from a Dockerfile and a “context”. A build’s context is the set of files located in the specified PATH or URL. The build process can refer to any of the files in the context. For example, your build can use a COPY instruction to reference a file in the context.

来做佐证。但是这些文章都没有列出 docker-build 命令的man手册,而在 docker-build 命令的man手册中,却能找到下面的说明

This will read the Dockerfile from the directory specified in PATH.  It also sends any other files and directories found in the current directory to the Docker daemon. The contents of this directory would be used by ADD commands found within the Dockerfile.

这里的第一句话已经说明了PATH参数将被用来查找Dockerfile文件,所以PATH参数很明显不像网上各种blog说的那样,只是用来负责构建上下文环境目录的。

实际上官网文档和man手册都不是很正确,各自只说明了一半,PATH参数既用于查找Dockerfile,又用于指定构建上下文。如果在PATH参数中没有找到Dockerfile(注意大小写),就必须使用 -f 选项来具体给出Dockerfile文件。

可以采用如下方法进行测试验证:

在任意路径使用 docker build -t xxx path 进行构建,但执行 docker build 命令的路径下没有dockerfile文件,path参数中同样没有Dockerfile文件,也即 docker build 命令执行路径、path路径、Dockerfile文件所在路径是三个不同的路径,此时linux下进行构建会提示 “unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /home/ubuntu/Dockerfile: no such file or directory” 类似的错误, mac os则提示 “failed to solve with frontend dockerfile.v0: failed to read dockerfile: open /var/lib/docker/tmp/buildkit-mount258672054/Dockerfile: no such file or directory”。

如果这时将Dockerfile文件放入path参数指定的路径中,则构建成功。

随后再在 docker build 命令执行路径下放入一个不能成功构建的Dockerfile文件,此时依然能够构建成功,进一步确定Dockerfile文件是由path参数来指定的。

 

额外说明:

在Linux Ubuntu发行版下测试时,发现Dockerfile文件名的大小写不敏感。

posted @ 2021-02-19 20:20  impluse  阅读(1907)  评论(0编辑  收藏  举报