dockerfile編寫的過程中,不可避免會遇到運行構建新鏡像錯誤的問題,那麼我們應該怎樣調試dockerfile呢。其實,當我們遇到某個指令失敗時,我們也能夠得到前一個指令構建的鏡像。因此,我們可以進入到前一個臨時鏡像,調試下一個指令。
在/root/debug-dockerfile目錄下創建一個Dockerfile構建一個包含有錯誤命令的新鏡像
root@localhost debug-dockerfile]# pwd
/root/debug-dockerfile
Dockerfile
FROM centos:7.4.1708
RUN touch tmpfile
RUN cp tmpfile tmpdir/
很明顯,該dockerfile的第三個命令是會運行失敗的,因爲當前目錄沒有tmpdir目錄。
運行Dockerfile後,報錯信息如下,在step3,即 RUN cp tmpfile tmpdir/ 時出現了錯誤。
[root@localhost debug-dockerfile]# docker build -t debug-dockerfile .
Sending build context to Docker daemon 2.56kB
Step 1/3 : FROM centos:7.4.1708
---> 295a0b2bd8ea
Step 2/3 : RUN touch tmpfile
---> Running in 7530981ccd45
Removing intermediate container 7530981ccd45
---> 8408a48380c2
Step 3/3 : RUN cp tmpfile tmpdir/
---> Running in a50d0a45ce94
cp: cannot create regular file 'tmpdir/': Not a directory
The command '/bin/sh -c cp tmpfile tmpdir/' returned a non-zero code: 1
這時,我們可以進入到前面一個指令中獲取到的臨時鏡像8408a48380c2,調試下一個指令。
[root@localhost debug-dockerfile]# docker run -it 8408a48380c2
通過ll命令,我們可以看到上一個命令創建的文件tmpfile