简体   繁体   English

Docker-如何检查Dockerfile中的curl命令是否具有响应代码200

[英]Docker - How to check if curl command inside Dockerfile had response code 200

Inside a Dockerfile I try to download an artifact using curl. 在Dockerfile内,我尝试使用curl下载工件。 I have checked that although the artifact doesn't exist (thus getting a 404) the docker build keeps running. 我检查了一下,尽管该工件不存在(因此得到了404),但docker build仍在运行。

RUN curl -H 'Cache-Control: no-cache' ${STANDALONE_LOCATION} -o $JBOSS_HOME/standalone/configuration/standalone.xml

Is there a way to check that the curl response code is 200 and throw an error otherwise? 有没有办法检查curl响应代码是否为200,否则抛出错误?

You can add -f (or --fail ) to the curl call, which causes curl to silently fail on server errors. 您可以在curl调用中添加-f (或--fail ),这将导致curl在服务器错误时默默地失败。 From the curl manpage : curl联机帮助页

-f/--fail -f /-失败

(HTTP) Fail silently (no output at all) on server errors. (HTTP)在服务器错误时静默失败(根本没有输出)。 This is mostly done to better enable scripts etc to better deal with failed attempts. 这样做主要是为了更好地使脚本等能够更好地处理失败的尝试。 In normal cases when a HTTP server fails to deliver a document, it returns an HTML document stating so (which often also describes why and more). 在正常情况下,当HTTP服务器无法交付文档时,它会返回HTML文档,说明其内容(通常还会说明原因以及更多内容)。 This flag will prevent curl from outputting that and return error 22. 该标志将阻止curl输出该错误并返回错误22。

This method is not fail-safe and there are occasions where non-successful response codes will slip through, especially when authentication is involved (response codes 401 and 407). 此方法不是故障安全的,并且有时会漏出不成功的响应代码,尤其是在涉及身份验证时(响应代码401和407)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM