Docker Remote API

ubuntu16.04配置docker遠程訪問

查看配置

systemctl show --property=FragmentPath docker 

修改配置文件

sudo vim /lib/systemd/system/docker.servie
ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:5678

重新加載配置文件,重啓docker

sudo systemctl daemon-reload
sudo systemctl restart docker (or sudo service docker restart)

測試Docker Remote API

curl http://vb:5678/info # vb是Docker宿主機的主機名
curl http://localhost:5678/info
curl http://127.0.0.1:5678/info

通過API管理Docker鏡像

獲取所有宿主機中所有的鏡像

curl vb:5678/images/json | python3 -mjson.tool

使用python的json工具整理輸出格式,方便查看

查看某一個鏡像的信息

curl    localhost:5678/images/a081f7d44c38b87ba89aaa5d62a6caed3b4e7e029462e7c39010e4d04aca2bb8/json | python3 -mjson.tool

在Docker Hub上查找鏡像,並不是在本地查找

curl localhost:5678/images/search?term=busybox | python3 -mjson.tool

刪除鏡像

curl -v -X DELETE vb:5678/images/busybox

鏡像歷史

curl -v vb:5678/images/busybox/history

所以在build或者commi鏡像的時候如果鏡像命名是test/api_test這樣,在使用API的過程中會遇到問題。
構建鏡像

tar -zcf Dockerfile.tar.gz Dockerfile
curl -v -X POST -H "content-type: application/tar" --data-binary '@Dockerfile.tar.gz' localhost:5678/build?t=samplerepo

通過API管理Docker容器

查看正在運行的容器

curl -s localhost:5678/containers/json | python3 -mjson.tool

查看所有的容器,包括停止的容器

curl -s localhost:5678/containers/json?all=1 | python3 -mjson.tool

創建容器

curl -X POST -H "Content-Type: application/json" localhost://5678/containers/create?name=api_busybox -d '{"Image":"busybox", "Hostname":"busybox"}'

調用這個API會創建一個名稱爲api_busybox的容器,並返回容器的Id等信息。
啓動容器

curl -v -X POST -H "Content-Type: application/json' localhost:5678/containers/api_busybox/start

在Docker1.10之前或許還需要加-d配置一些選項,1.10之後加-d選項會出錯。
查看容器內的進程

curl localhost:5678/containers/api_busybox/top

導出容器

curl -o testExport.tar.gz localhost:5678/containers/api_export/export

重啓容器

curl -v -X POST localhost:5678/containers/api_busybox/restart

停止容器

curl -v -X POST localhost:5678/containers/api_busybox/stop

停止容器

curl -v -X POST localhost:5678/containers/api_busybox/kill
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章