下面來看一個遠程調用 Docker API的實現方法,希望文章可以幫助到各位深入的理解Docker API的使用方法.
Docker 的 API
API:Application Programe Interface, 應用程序訪問接口,通過發布 API,我們的程序就可以在別的語言,平臺中被調用,如果一個程序擁有 API,我們就可以為其開發 “客戶端”,而不依賴某一個具體語言,當然也能集成到自己的平臺,完成一些有趣的功能.
API 的主要類型:RESTful,基于 HTTP 協議,也能基于 RPC 協議.
所謂 RESTful,是指通過如HTTP協議封裝的各種請求,主要包括 GET,POST,UPDATE,DELETE,PUT 等,保存創建,修改等操作,一般 RESTful 服務器提供這些請求的接口(地址,路徑,參數,然后客戶端可以通過過類似 Linux 命令 curl,Python 標準庫 httplib、httplib2、urllib 等訪問,需要注意的是,多數參數(數據)都是以 JSON/XML 打包,并且多數按照 token 方式驗證請求權限和安全性,當然也支持其他類型的驗證.
可以在 Docker 官網看到 Docker API 的介紹:
Docker Remote API
Docker Remote API Client Libraries(簡稱 rcli)
有何區別?
原生態 RESTful 標準,官方維護,支持最新的功能,支持各種語言的 SDK、Client 庫,較 API 版本滯后.
和 OpenStack 的 python-*client 一樣,rcli 無非就是對 Docker Remote API 的一種封裝實現,由官方和第三方在維護,但并不保證兼容性.
下面是 官方 在介紹 rcli 的說明:
- These libraries have not been tested by the Docker Maintainers for compatibility.
- Please file issues with the library owners. If you find more library implementations,
- please list them in Docker doc bugs and we will add the libraries here.
需要注意的是,官方已經明確說明 Remote API 已經取代了 rcli:
https://docs.docker.com/reference/api/docker_remote_api
The Remote API is replacing rcli.
有精力的話完全可以自己封裝一些方法.
遠程調用 Docker API的實現方法
默認情況下 Docker 的守護進程啟動會生成一個 socket(/var/run/docker.sock)進程通信文件,而并沒有監聽端口,只能在本機操作 Docker,如果想在其它地方操作 Docker 主機,就需要讓 Docker 主機監聽一個端口號,這樣可以通過端口號就能實現遠程操作.
直接和 Docker 守護進程通信
用 Python 來調用 API,Docker 提供了 Python API Client.
項目地址:https://github.com/docker/docker-py
在 Docker 主機上安裝 docker-py:pip install docker-py
先用命令行看看操作輸出,沒有正在運行中的容器.
- # docker ps
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
有一個已經關閉的容器:
- # docker ps -a
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- 050f47812f2c ubuntu:14.04 "/bin/bash" 23 hours ago Exited (0) 22 hours ago hopeful_shockley //Vevb.com
用 API 方式重復上面命令行的操作,在 Python 交互里嘗試調用:
# ipython
導入模塊:In [1]: import docker
與 Docker 守護進程建立連接、通信:In [2]: c = docker.Client(base_url='unix:///var/run/docker.sock')
獲取當前運行的容器:
- In [3]: c.containers()
- Out[3]: []
加了一個參數:all=True,代表列出所有運行、關閉的容器.
- In [4]: c.containers(all=True)
- Out[4]:
- [{u'Command': u'/bin/bash',
- u'Created': 1410588586,
- u'Id': u'050f47812f2c1925439bbdefb479efb80bc1df007fc45ced1d74a9d483036e9b',
- u'Image': u'ubuntu:14.04',
- u'Names': [u'/hopeful_shockley'],
- u'Ports': [],
- u'Status': u'Exited (0) 22 hours ago'}]
docker 模塊可以實現命令行的所有操作,具體使用方法參考 help、dir 的輸出,或者查閱 docker-py 項目的文檔.
讓 Docker 監聽端口,修改 Docker 服務啟動參數,添加一個沒有被占用的端口號:
- # vim /etc/default/docker
- DOCKER_OPTS='-H docker01.thstack.com:6732'
重啟 Docker 服務生效:service docker restart,設置一個 DOCKER_HOST 環境變量,可以用主機名或 IP 地址,用主機名時候注意域名的解析,有防火墻的添加下面端口:
- # vim /etc/profile
- export DOCKER_HOST=tcp://docker01.thstack.com:6732
- # source /etc/profile
驗證:,可以在本機或其它主機用 Curl 來驗證,也可以使用 chrome 瀏覽器插件 postman 來驗證.
返回空:
- # curl -k -X 'GET' http://docker01.thstack.com:6732/containers/json -H 'Content-type: application/json' | python -mjson.tool
- 在 url 上加一個參數:all=1,獲取所有的 container
- # curl -k -X 'GET' http://docker01.thstack.com:6732/containers/json?all=1 -H 'Content-type: application/json' | python -mjson.tool
- % Total % Received % Xferd Average Speed Time Time Time Current
- Dload Upload Total Spent Left Speed
- 100 218 100 218 0 0 14273 0 --:--:-- --:--:-- --:--:-- 14533
- [
- {
- "Command": "/bin/bash",
- "Created": 1410588586,
- "Id": "050f47812f2c1925439bbdefb479efb80bc1df007fc45ced1d74a9d483036e9b",
- "Image": "ubuntu:14.04",
- "Names": [
- "/hopeful_shockley"
- ],
- "Ports": [],
- "Status": "Exited (0) 23 hours ago"
- }
- ]
具體 Docker Remote API 參數可以參考官方 API 文檔:
https://docs.docker.com/reference/api/docker_remote_api_v1.14/
用 docker-py 來驗證:
- # ipython
- In [1]: import docker
- In [2]: c = docker.Client(base_url='http://docker01.thstack.com:6732')
- In [3]: c.containers(all=True)
- Out[3]:
- [{u'Command': u'/bin/bash',
- u'Created': 1410588586,
- u'Id': u'050f47812f2c1925439bbdefb479efb80bc1df007fc45ced1d74a9d483036e9b', --Vevb.com
- u'Image': u'ubuntu:14.04',
- u'Names': [u'/hopeful_shockley'],
- u'Ports': [],
- u'Status': u'Exited (0) 23 hours ago'}]
- In [4]:
現在的 Docker 守護進程默認只監聽了端口,并沒有開放 socket 入口,可以讓兩者同時工作:
- # vim /etc/default/docker
- DOCKER_OPTS='-H unix:///var/run/docker.sock -H docker01.thstack.com:6732'
- # service docker restart
現在我們在任何地方來連接、操作自己的 Docker 主機了.
新聞熱點
疑難解答