docker build: COPY/ADD報錯:not a directory
阿新 • • 發佈:2019-01-04
注:本文基於CentOS 6.5 3.10核心,docker版本1.7.1
背景
製作docker映象時,要把一個檔案拷貝至映象中,可以使用ADD或者COPY命令,但是使用的時候一直報錯。
[[email protected] test]# docker build . Sending build context to Docker daemon 3.072 kB Sending build context to Docker daemon Step 0 : FROM gpmidi/centos-6.5 ---> d7943348aefb Step 1 : ADD a /root stat /var/lib/docker/devicemapper/mnt/45f35175c0848980b14929ead6ba6bc3b85b4836c5f0861cd25c9fad7c27e4eb/rootfs/root/a: not a directory
最簡單的操作,把當前目錄的a檔案拷貝至映象環境中的/root目錄,然而就是不成功。
換成COPY也一樣報錯。
[[email protected] test]# docker build . Sending build context to Docker daemon 3.072 kB Sending build context to Docker daemon Step 0 : FROM gpmidi/centos-6.5 ---> d7943348aefb Step 1 : COPY a /root stat /var/lib/docker/devicemapper/mnt/31d0d0527b091d952147cfb63b8700ef6865b88ae768862e4aa16518f5541101/rootfs/root/a: not a directory
解決方案
在網上找了很久,一度還以為是bug,然而並不是,而是沒搞清楚ADD/COPY的用法。在拷貝檔案的時候docker是用“/”來區分目錄和檔案的,也就是“/root”是被當做根目錄檔案root,“/root/”才被識別為目錄。知道這個點後,修改就簡單了,在目錄最後以斜杆“/”結尾即可。
[[email protected] test]# docker build . Sending build context to Docker daemon 3.072 kB Sending build context to Docker daemon Step 0 : FROM gpmidi/centos-6.5 ---> d7943348aefb Step 1 : COPY a /root/ ---> f6477c17485c Removing intermediate container 0d5ca33a3bda Successfully built f6477c17485c
注意事項
在查閱資料時還看到一些其他注意事項,一併記錄。
1、拷貝或者新增檔案時不能超出當前目錄範圍,即不能使用以下方式:
ADD ../data/file /home/
COPY /home/a.zip /home/
因為在docker build最開始就將當前上下文環境傳給 docker deamon,因此如果使用其他目錄,docker deamon無法找到這些檔案,這也就是我們使用docker build . ,這個“.”的用意。
2、COPY命令拷貝是原樣拷貝,而ADD命令會將壓縮包解壓。