2014-04-22 20 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