首先,我想說的是:網上的教程都是錯的!
網上很多文章都是相互轉載,其中用的示例都是一樣的,都統一得出結論:“這里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 specifiedPATH
orURL
. The build process can refer to any of the files in the context. For example, your build can use aCOPY
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文件名的大小寫不敏感。