2014-04-22 54 views
4

我正在創建一個Dockerfile,用於從源代碼下載,構建和安裝node.js。我想構建它之前checksum下載,停止或退出Dockerfile如果校驗失敗:在Dockerfile中校驗和下載的典型方法?

# officially supported ubuntu 
FROM ubuntu:12.04 

# SETUP 
RUN cd /tmp 
RUN apt-get update -y 
RUN apt-get install wget build-essential automake -y 
RUN wget http://nodejs.org/dist/latest/node-v0.10.26.tar.gz 
RUN wget http://nodejs.org/dist/latest/SHASUMS256.txt 

# RUN checksum: exit on fail, continue on success 
??? how ??? 

# INSTALL 
RUN tar -xvf node-v0.10.26.tar.gz && cd node-v0.10.26 
RUN ./configure && make && make install 

# CLEANUP 
apt-get autoremove --purge wget build-essential automake -y 

已泊塢窗社會上這樣做的「最佳實踐」的方式解決?

回答

4

如果任何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