如何使用Docker api引擎执行容器中的命令。我想使用api docker通过http调用在容器中执行cmds
答案 0 :(得分:0)
如果要在容器中执行cmd,则需要输入ID或容器名称,此处的ID与执行docker ps -a所见的ID不同,这里有一个EndPoint在正在运行的容器的json中获取详细信息,即
结果响应如下所示
{
"Id": "ba7b20038dfe49d326788258eca42acb752405db835e09ffdf2705fdc16b7d17",
"Names": [
"/muruga"
],
"Image": "ubuntu",
"ImageID": "sha256:93fd78260bd1495afb484371928661f63e64be306b7ac48e2d13ce9422dfee26",
"Command": "/bin/bash",
"Created": 1544778203,
"Ports": [],
"Labels": {},
"State": "running",
"Status": "Up 3 seconds",
"HostConfig": {
"NetworkMode": "default"
},
"NetworkSettings": {
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "6204f5fc4d3689aebe589bd1eab4a94f73a249d69aa88772a800d94f1edc1ea6",
"EndpointID": "7509759b07f6463b4a0a88baa00a5f6834cf69615ac88bb8bc1dbd8557be7db3",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
},
"Mounts": []
}
您需要使用运行容器的ID或名称来创建“ exec”实例,即 url ===>发布/容器/ {{id / name}} / exec
`发布数据
{
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"DetachKeys": "ctrl-p,ctrl-q",
"Tty": true,
"Cmd": [
"bin/bash","-c","touch appa.py"
],
"Env": [
"FOO=bar",
"BAZ=quux"
],
"Privileged":true,
"User":"root"
}`
它返回带有json id的响应201,此id在每次exec实例发布完成时都是唯一的,它将返回唯一的id,您需要复制此id并将其传递给另一个url或endpoin
url ===> post / exec / {{id(我在响应中提到的唯一ID)}} / start 发布以下数据
{
"Detach": true,
"Tty": false
}
返回200 ok,您可以进入容器并检查命令的执行情况