规范的方式来校验码下载在Dockerfile?

我正在创build一个Dockerfile,用于从源代码下载,构build和安装node.js。 我想在构build它之前校验下载,如果校验和失败,则停止或退出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 

Docker社区是否以“最佳实践”的方式解决了这个问题?

如果任何一个RUN命令返回一个非零代码,构build将失败。

 FROM fedora RUN false 

在上面的Dockerfile中,我只是通过运行false来做一个快速testing。 false是一个linux实用程序,它只是设置一个非零的返回码,方便testing。 正如你所看到的,当我构buildDockerfile时,它会抱怨并失败。

 $ 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显示该文件和校验和),并且可以对其进行testing。 这是下面这个快速和肮脏的版本,其中我生成一个文件,并在validation它之前计算它的校验和。 在你的例子中,你显然不会这样做,我只是做这个来告诉你它是如何工作的。

 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