2013-10-15 44 views
5

泊塢窗API圖像創建/拉(/v1.6/images/create)顯然總是返回如何處理docker API/images/create?

HTTP/1.1 200 OK 
Content-Type: application/json 

無論過程是成功還是失敗。

此外,有效載荷是無效的json。

例如:/v1.6/images/create?fromImage=whatevertheflush

回報:

{"status":"Pulling repository whatevertheflush"}{"error":"Server error: 404 trying to fetch remote history for whatevertheflush","errorDetail":{"code":404,"message":"Server error: 404 trying to fetch remote history for whatevertheflush"}} 

由於不是有效的JSON,並且不被轉發的HTTP錯誤/使用使得很難處理客戶錯誤。

事實上,搬運工-PY剛吐的有效載荷(https://github.com/dotcloud/docker-py/blob/master/docker/client.py#L374)。並從OpenStack的DockerHTTPClient嘗試返回一個基於HTTP錯誤代碼,它始終是200 ...(https://github.com/openstack/nova/blob/master/nova/virt/docker/client.py#L191

現在,我明白拉可能需要很長時間,並且它有點有意義開始爲客戶提供一個答案,但我不禁認爲這裏有什麼不對。

所以,這是三折:

  • 我思念的東西完全在這裏嗎?如果不是:如果你正在實現一個客戶端應用程序(比如在Python中),你將如何處理這個問題(如果可能的話,優雅:))?嘗試檢測有效的json塊,加載它們,並在我們「認爲」出現問題時退出?
  • 如果不是的話:在將來的碼頭版本中,這是否會改變(更好)?

回答

0

這個特定端點實際上返回塊編碼。通過捲曲一個例子:

$ curl -v -X POST http://localhost:4243/images/create?fromImage=base 
* About to connect() to localhost port 4243 (#0) 
* Trying ::1... 
* Connection refused 
* Trying 127.0.0.1... 
* connected 
* Connected to localhost (127.0.0.1) port 4243 (#0) 
> POST /images/create?fromImage=base HTTP/1.1 
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8y zlib/1.2.5 
> Host: localhost:4243 
> Accept: */* 
> 
< HTTP/1.1 200 OK 
< Content-Type: application/json 
< Date: Fri, 07 Feb 2014 04:21:59 GMT 
< Transfer-Encoding: chunked 
< 
* Connection #0 to host localhost left intact 
{"status":"Pulling repository base"}{"status":"Pulling image (ubuntu-quantl) from  base","progressDetail":{},"id":"b750fe79269d"}{"status":"Pulling image (ubuntu-quantl) from base, endpoint: https://cdn-registry-1.docker.io/v1/","progressDetail":{},"id":"b750fe79269d"}{"status":"Pulling dependent layers","progressDetail":{},"id":"b750fe79269d"}{"status":"Download complete","progressDetail":{},"id":"27cf78414709"}{"status":"Download complete","progressDetail":{},"id":"b750fe79269d"}{"status":"Download complete","progressDetail":{},"id":"b750fe79269d"}* Closing connection #0 

現在,我不知道你如何在Python解析這一點,但在Ruby中,我可以使用Yajl像這樣:

parts = [] 
Yajl::Parser.parse(body) { |o| parts << o } 
puts parts 
{"status"=>"Pulling repository base"} 
{"status"=>"Pulling image (ubuntu-quantl) from base", "progressDetail"=>{}, "id"=>"b750fe79269d"} 
{"status"=>"Pulling image (ubuntu-quantl) from base, endpoint: https://cdn-registry-1.docker.io/v1/", "progressDetail"=>{}, "id"=>"b750fe79269d"} 
{"status"=>"Pulling dependent layers", "progressDetail"=>{}, "id"=>"b750fe79269d"} 
{"status"=>"Download complete", "progressDetail"=>{}, "id"=>"27cf78414709"} 
{"status"=>"Download complete", "progressDetail"=>{}, "id"=>"b750fe79269d"} 
{"status"=>"Download complete", "progressDetail"=>{}, "id"=>"b750fe79269d"} 
+0

那麼,它不僅分塊編碼,它也是JSON的大塊 - 事實上,我端到了Python寫一個解析器爲好。但感謝您的答案。 –

0

以下步驟將通過構建多克API的圖像。

樣品Dockerfile:

# cat Dockerfile 

FROM ubuntu:14.04 
RUN mkdir demo 
RUN apt-get update 
RUN apt-get -y install vim 

創建一個tar文件,其中包括您的Dockerfile。

# tar -cvf Dockerfile.tar.gz Dockerfile 

如下所示執行API並獲取更多選項,請參閱本文。

# curl -v -X POST -H "Content-Type:application/tar" --data-binary '@Dockerfile.tar.gz' http://127.0.0.1:5000/build?t=build_test 

# docker images 
REPOSITORY      TAG     IMAGE ID   CREATED    SIZE 
build_test      latest    b1736dd9b698  8 seconds ago 

參見本:

how to configure docker daemon port