我使用泊塢窗引擎API,我希望能夠模仿docker run
參數的容器。使用泊塢窗REST API來運行
我看到in this link一些關於這個音符,但對如何傳遞參數沒有具體的例子。例如,如何使用REST API我複製以下
docker run -d -p 27017:27017 -p 28017:28017 tutum/mongodb
?
我使用泊塢窗引擎API,我希望能夠模仿docker run
參數的容器。使用泊塢窗REST API來運行
我看到in this link一些關於這個音符,但對如何傳遞參數沒有具體的例子。例如,如何使用REST API我複製以下
docker run -d -p 27017:27017 -p 28017:28017 tutum/mongodb
?
發現了一個哈克方法是在命令本身運行strace的:
$ sudo strace -e write \
-o /tmp/docker.strace \
-s 10000 \
-f docker run -d -p 27017:27017 -p 28017:28017 tutum/mongodb
在文件/tmp/docker.strace
,您將看到:
10911 write(3, "POST /v1.25/containers/create HTTP/1.1\r\nHost: docker\r\nUser-Agent: Docker-Client/1.13.0 (linux)\r\nContent-Length: 1551\r\nContent-Type: application/json\r\n\r\n{\"Hostname\":\"\",\"Domainname\":\"\",\"User\":\"\",\"AttachStdin\":false,\"AttachStdout\":false,\"AttachStderr\":false,\"ExposedPorts\":{\"27017/tcp\":{},\"28017/tcp\":{}},\"Tty\":false,\"OpenStdin\":false,\"StdinOnce\":false,\"Env\":[],\"Cmd\":null,\"Image\":\"tutum/mongodb\",\"Volumes\":{},\"WorkingDir\":\"\",\"Entrypoint\":null,\"OnBuild\":null,\"Labels\":{},\"HostConfig\":{\"Binds\":null,\"ContainerIDFile\":\"\",\"LogConfig\":{\"Type\":\"\",\"Config\":{}},\"NetworkMode\":\"default\",\"PortBindings\":{\"27017/tcp\":[{\"HostIp\":\"\",\"HostPort\":\"27017\"}],\"28017/tcp\":[{\"HostIp\":\"\",\"HostPort\":\"28017\"}]},\"RestartPolicy\":{\"Name\":\"no\",\"MaximumRetryCount\":0},\"AutoRemove\":false,\"VolumeDriver\":\"\",\"VolumesFrom\":null,\"CapAdd\":null,\"CapDrop\":null,\"Dns\":[],\"DnsOptions\":[],\"DnsSearch\":[],\"ExtraHosts\":null,\"GroupAdd\":null,\"IpcMode\":\"\",\"Cgroup\":\"\",\"Links\":null,\"OomScoreAdj\":0,\"PidMode\":\"\",\"Privileged\":false,\"PublishAllPorts\":false,\"ReadonlyRootfs\":false,\"SecurityOpt\":null,\"UTSMode\":\"\",\"UsernsMode\":\"\",\"ShmSize\":0,\"ConsoleSize\":[0,0],\"Isolation\":\"\",\"CpuShares\":0,\"Memory\":0,\"NanoCpus\":0,\"CgroupParent\":\"\",\"BlkioWeight\":0,\"BlkioWeightDevice\":null,\"BlkioDeviceReadBps\":null,\"BlkioDeviceWriteBps\":null,\"BlkioDeviceReadIOps\":null,\"BlkioDeviceWriteIOps\":null,\"CpuPeriod\":0,\"CpuQuota\":0,\"CpuRealtimePeriod\":0,\"CpuRealtimeRuntime\":0,\"CpusetCpus\":\"\",\"CpusetMems\":\"\",\"Devices\":[],\"DiskQuota\":0,\"KernelMemory\":0,\"MemoryReservation\":0,\"MemorySwap\":0,\"MemorySwappiness\":-1,\"OomKillDisable\":false,\"PidsLimit\":0,\"Ulimits\":null,\"CpuCount\":0,\"CpuPercent\":0,\"IOMaximumIOps\":0,\"IOMaximumBandwidth\":0},\"NetworkingConfig\":{\"EndpointsConfig\":{}}}\n", 1703) = 1703
...
10914 write(5, "POST /v1.25/containers/42844726d8bd925d9903a0922f380c295dd398475b779d7ae2099ec2b4ab494e/start HTTP/1.1\r\nHost: docker\r\nUser-Agent: Docker-Client/1.13.0 (linux)\r\nContent-Length: 0\r\nContent-Type: text/plain\r\n\r\n", 207) = 207
後的第一個請求(容器創造),漂亮的形式是:
POST /v1.25/containers/create HTTP/1.1
Host: docker
User-Agent: Docker-Client/1.13.0 (linux)
Content-Length: 1551
Content-Type: application/json
{
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"27017/tcp": {},
"28017/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [],
"Cmd": null,
"Image": "tutum/mongodb",
"Volumes": {},
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {},
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"27017/tcp": [
{
"HostIp": "",
"HostPort": "27017"
}
],
"28017/tcp": [
{
"HostIp": "",
"HostPort": "28017"
}
]
},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 0,
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": null,
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": -1,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0
},
"NetworkingConfig": {
"EndpointsConfig": {}
}
}
現在,有很多默認值當下。讓我們來修剪出來,並創建一個最小集合,其中,在這種情況下,就是:
{
"ExposedPorts": {
"27017/tcp": {},
"28017/tcp": {}
},
"Image": "tutum/mongodb",
"HostConfig": {
"PortBindings": {
"27017/tcp": [
{
"HostIp": "",
"HostPort": "27017"
}
],
"28017/tcp": [
{
"HostIp": "",
"HostPort": "28017"
}
]
}
}
}
現在,讓我們嘗試創建容器和使用REST API我們開始吧。
$ sudo curl -v -H "Content-Type: application/json" -d '{"ExposedPorts":{"27017/tcp":{},"28017/tcp":{}},"Image":"tutum/mongodb","HostConfig":{"PortBindings":{"27017/tcp":[{"HostIp":"","HostPort":
"27017"}],"28017/tcp":[{"HostIp":"","HostPort":"28017"}]}}}' --unix-socket /var/run/docker.sock http://docker/containers/create
* Trying /var/run/docker.sock...
* Connected to docker (/var/run/docker.sock) port 80 (#0)
> POST /containers/create HTTP/1.1
> Host: docker
> User-Agent: curl/7.50.3
> Accept: */*
> Content-Type: application/json
> Content-Length: 198
>
* upload completely sent off: 198 out of 198 bytes
< HTTP/1.1 201 Created
< Api-Version: 1.25
< Content-Type: application/json
< Docker-Experimental: false
< Server: Docker/1.13.0 (linux)
< Date: Tue, 31 Jan 2017 05:03:34 GMT
< Content-Length: 90
<
{"Id":"be10c2bb8f07e36717a10cdf102a304aaf2ee072661df0265e322bdeb5fc1d78","Warnings":null}
* Curl_http_done: called premature == 0
* Connection #0 to host docker left intact
驗證容器已創建:
$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be10c2bb8f07 tutum/mongodb "/run.sh" About a minute ago Created inspiring_shannon
現在,讓我們開始的容器。從strace
輸出,我們現在知道,端點是/containers/UUID/start
,HTTP方法是POST
我們也知道,從以前的curl命令的輸出容器UUID
:
$ sudo curl -X POST -v --unix-socket /var/run/docker.sock http://docker/containers/be10c2bb8f07e36717a10cdf102a304aaf2ee072661df0265e322bdeb5fc1d78/start
* Trying /var/run/docker.sock...
* Connected to docker (/var/run/docker.sock) port 80 (#0)
> POST /containers/be10c2bb8f07e36717a10cdf102a304aaf2ee072661df0265e322bdeb5fc1d78/start HTTP/1.1
> Host: docker
> User-Agent: curl/7.50.3
> Accept: */*
>
< HTTP/1.1 204 No Content
< Api-Version: 1.25
< Docker-Experimental: false
< Server: Docker/1.13.0 (linux)
< Date: Tue, 31 Jan 2017 05:05:46 GMT
<
* Curl_http_done: called premature == 0
* Connection #0 to host docker left intact
讓我們驗證該容器確實運行。
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be10c2bb8f07 tutum/mongodb "/run.sh" 2 minutes ago Up 8 seconds 0.0.0.0:27017->27017/tcp, 0.0.0.0:28017->28017/tcp inspiring_shannon
僅供參考,API documentation是非常有用的,如果你不想讓你想了解的參數,每次運行strace的。也許他們也應該包括一些例子。 :-)
這是一個非常好的答案。非常感謝@Nehal! – nmpg
的可能的複製[如何使用泊塢窗遠程API來創建容器?](http://stackoverflow.com/questions/31381978/how-to-use-docker-remote-api-to-create-容器) – Matt