如果任何RUN
命令返回非零代碼,則構建將失敗。
FROM fedora
RUN false
在上面的Dockerfile中,我只是通過運行false
來做一個快速測試。 false
是一個linux實用程序,它只是設置一個非零的返回碼,方便測試。正如你所看到的,當我構建Dockerfile時,它會抱怨並失敗。
$ docker build .
Uploading context 12.29 kB
Uploading context
Step 0 : FROM fedora
---> 58394af37342
Step 1 : RUN false
---> Running in a5b9a4b37e25
2014/04/22 09:41:19 The command [/bin/sh -c false] returned a non-zero code: 1
因此,它是讓你的文件和圖像中的校驗和(您似乎在通過wget
有),你可以測試它只是一個簡單的事情。下面是這個快速和骯髒的版本,其中我生成一個文件並在驗證它之前計算它的校驗和。在你的例子中,你顯然不會那麼做,我只是做它來告訴你它是如何工作的。
FROM fedora
# Create the text file
RUN echo ThisIsATest > echo.txt
# Calculate the checksum
RUN sha1sum echo.txt > sha1sums.txt
# Validate the checksum (this should pass)
RUN sha1sum -c sha1sums.txt
# Alter the text
RUN echo ThisShouldFail > echo.txt
# Validate the checksum (this should now fail)
RUN sha1sum -c sha1sums.txt
如果我們運行這個...
$ docker build -no-cache .
Warning: '-no-cache' is deprecated, it will be removed soon. See usage.
Uploading context 12.8 kB
Uploading context
Step 0 : FROM fedora
---> 58394af37342
Step 1 : RUN echo ThisIsATest > echo.txt
---> Running in cd158d4e6d91
---> 4088b1b4945f
Step 2 : RUN sha1sum echo.txt > sha1sums.txt
---> Running in 5d028d901d94
---> c97b1d31a720
Step 3 : RUN sha1sum -c sha1sums.txt
---> Running in 44d119897164
echo.txt: OK
---> ca01d590cadd
Step 4 : RUN echo ThisShouldFail > echo.txt
---> Running in 87b575ac4052
---> 36bb5d8cf6d1
Step 5 : RUN sha1sum -c sha1sums.txt
---> Running in e20b7ac0c924
echo.txt: FAILED
WARNING: 1 computed checksum did NOT match
2014/04/22 10:29:07 The command [/bin/sh -c sha1sum -c sha1sums.txt] returned a non-zero code: 1