使用docker API启动容器

时间:2017-06-13 07:36:42

标签: docker docker-api

我正在尝试使用Docker REST API部署容器。我能够创建一个本地图像并创建一个容器。但是在启动容器时,我收到以下错误:

     {"message":"starting container with non-empty request body was deprecated since v1.10 and removed in v1.12"}

start请求的dockerd日志是:

     err-code: 400
     time="2017-06-13T07:33:12.679308012Z" level=debug msg="Calling POST /containers/67cf42e1cb92c754ec00a7ec6e02d22ef5ab2267406d7749a85e983481fc0c03/start" 
     time="2017-06-13T07:33:12.679451206Z" level=debug msg="form data: {\"Binds\":[],\"PublishAllPorts\":true}" 

发送空请求正文后,我从dockerd获取以下错误日志:

    err-code: 404 (no such container - https://docs.docker.com/engine/api/v1.19/#2-endpoints)
    time="2017-06-13T07:54:16.483161021Z" level=debug msg="Calling POST /containers/dbaabc5f437f/start" 
    time="2017-06-13T07:42:34.237522360Z" level=error msg="Create container failed with error: oci runtime error: container_linux.go:247: starting container process caused \"exec: \\\"None\\\": executable file not found in $PATH\"\n" 

服务器上docker ps的输出是:

    $ docker -H=<host-name>:2376 ps -a
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
    dbaabc5f437f        tomcat:8.0          "None"              7 seconds ago       Created                                 my_tomcat_container_rnixfw

4 个答案:

答案 0 :(得分:0)

我没有意识到在创建容器时,我应该传递要执行的命令。 Dockerfile具有CMD行,但显然如果必须使用REST API启动容器,则必须明确传递要执行的命令。

不确定这是否是所需行为或docker引擎中的错误。

time="2017-06-13T08:15:22.920664416Z" level=debug msg="Calling POST /containers/create?name=my_tomcat_container_ooztsn" 
time="2017-06-13T08:15:22.920815655Z" level=debug msg="form data: {\"AttachStderr\":true,\"AttachStdin\":true,\"AttachStdout\":true,**\"Cmd\":[\"catalina.sh\",\"run\"]**,\"CpuShares\":null,\"Dns\":null,\"Domainname\":null,\"Entrypoint\":null,\"Env\":null,\"ExposedPorts\":null,\"Hostname\":null,\"Image\":\"tomcat:latest\",\"Memory\":0,\"MemorySwap\":0,\"NetworkDisabled\":false,\"OpenStdin\":true,\"PortBindings\":{},\"PublishAllPorts\":true,\"StdinOnce\":false,\"Tty\":true,\"User\":\"\",\"Volumes\":null,\"VolumesFrom\":null,\"WorkingDir\":\"\"}" 

答案 1 :(得分:0)

有关配置docker守护程序端口的更多详细信息,请refer configure-docker-daemon-port

配置Docker端口后,您可以访问远程主机中的Docker API。

JSON输入文件:

用于创建容器的API:

curl -X POST http://192.168.56.101:6000/containers/create -d @container_create.json --header "Content-Type: application/json" | jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   602  100    90  100   512   1737   9883 --:--:-- --:--:-- --:--:-- 10039
{
  "Warnings": null,
  "Id": "f5d3273e48350d606bd8b9d2a5bd876dc5c2d1a73183f876a1dd56473cad8940"
}

生成的ID是容器ID,状态不会处于活动/运行状态。

用于启动已创建容器的API。

# curl -X POST http://192.168.56.101:6000/containers/f5d3273e48350/start | jq .  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

用于检查状态/检查容器的API:

# curl -X GET http://192.168.56.101:6000/containers/f5d3273e48350/json | jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  4076    0  4076    0     0   278k      0 --:--:-- --:--:-- --:--:--  306k
{
  "NetworkSettings": {
    "Networks": {
      "bridge": {
        "MacAddress": "02:42:ac:11:00:03",
        "GlobalIPv6PrefixLen": 0,
        "GlobalIPv6Address": "",
        "IPv6Gateway": "",
        "IPAMConfig": null,
        "Links": null,
        "Aliases": null,
        "NetworkID": "689d6b65ce1b06c93b2c70f41760a3e7fb2b50697d71cd9c1f39c64c865e5fa6",
        "EndpointID": "76bf1f8638d1ff0387e6c3fe89e8ccab1670c709ad550f9acc6f46e559654bee",
        "Gateway": "172.17.0.1",
        "IPAddress": "172.17.0.3",
        "IPPrefixLen": 16
      }
    },
    "MacAddress": "02:42:ac:11:00:03",
    "SecondaryIPAddresses": null,
    "SandboxKey": "/var/run/docker/netns/24a031d9dfda",
    "Ports": {
      "0/tcp": null
    },
    "LinkLocalIPv6PrefixLen": 0,
    "LinkLocalIPv6Address": "",
    "HairpinMode": false,
    "SandboxID": "24a031d9dfda70026a875f4841269c5e790b12ccafcc11869111faa240020b99",
    "Bridge": "",
    "SecondaryIPv6Addresses": null,
    "EndpointID": "76bf1f8638d1ff0387e6c3fe89e8ccab1670c709ad550f9acc6f46e559654bee",
    "Gateway": "172.17.0.1",
    "GlobalIPv6Address": "",
    "GlobalIPv6PrefixLen": 0,
    "IPAddress": "172.17.0.3",
    "IPPrefixLen": 16,
    "IPv6Gateway": ""
  },

    },
    "AttachStderr": true,
    "AttachStdout": true,
    "AttachStdin": true,
    "User": "",
    "Domainname": "",
    "Hostname": "f5d3273e4835",
    "OpenStdin": true,
    "StdinOnce": true,
    "Env": [
      "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
    ],
    "Cmd": [
      "/bin/bash"
    ],
    "ArgsEscaped": true,
    "Image": "ubuntu:14.04",

<*************REMOVING THE OUTPUT CONTENT********>

  "ExecIDs": null,
  "HostnamePath": "/var/lib/docker/containers/f5d3273e48350d606bd8b9d2a5bd876dc5c2d1a73183f876a1dd56473cad8940/hostname",
  "ResolvConfPath": "/var/lib/docker/containers/f5d3273e48350d606bd8b9d2a5bd876dc5c2d1a73183f876a1dd56473cad8940/resolv.conf",
  "Image": "sha256:132b7427a3b40f958aaeae8716e0cbb2177658d2410554ed142e583ef522309f",
  "State": {
    "FinishedAt": "0001-01-01T00:00:00Z",
    "StartedAt": "2017-06-09T06:53:45.120357144Z",
    "Error": "",
    "Status": "running",
    "Running": true,
    "Paused": false,
    "Restarting": false,

  "Path": "/bin/bash",
  "Created": "2017-06-09T06:52:51.820429355Z",
  "Id": "f5d3273e48350d606bd8b9d2a5bd876dc5c2d1a73183f876a1dd56473cad8940",
  "HostsPath": "/var/lib/docker/containers/f5d3273e48350d606bd8b9d2a5bd876dc5c2d1a73183f876a1dd56473cad8940/hosts",
  "LogPath": "/var/lib/docker/containers/f5d3273e48350d606bd8b9d2a5bd876dc5c2d1a73183f876a1dd56473cad8940/f5d3273e48350d606bd8b9d2a5bd876dc5c2d1a73183f876a1dd56473cad8940-json.log",
  "Name": "/objective_bartik",
  "RestartCount": 0,
  "Driver": "aufs",
  "MountLabel": "",
  "ProcessLabel": "",
  "AppArmorProfile": "docker-default"
}

有关详细信息,请参阅此处:

How to build an Image using Docker API?

How to commit Docker Container using API

希望这些信息对他有帮助。

答案 2 :(得分:0)

我可以通过运行以下命令重现您已经解决的问题:

curl -v -d "{\"Binds\":[],\"PublishAllPorts\":true}" --unix-socket /var/run/docker.sock http://localhost/v1.27/containers/0df187bd421a/start
*   Trying /var/run/docker.sock...
* Connected to localhost (/var/run/docker.sock) port 80 (#0)
> POST /v1.27/containers/0df187bd421a/start HTTP/1.1
> Host: localhost
> User-Agent: curl/7.53.0
> Accept: */*
> Content-Length: 35
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 35 out of 35 bytes
< HTTP/1.1 400 Bad Request
< Api-Version: 1.29
< Content-Type: application/json
< Docker-Experimental: false
< Ostype: linux
< Server: Docker/17.05.0-ce (linux)
< Date: Tue, 13 Jun 2017 15:57:12 GMT
< Content-Length: 109
<
{"message":"starting container with non-empty request body was deprecated since v1.10 and removed in v1.12"}
* Connection #0 to host localhost left intact

此错误消息相对简单。在尝试启动容器时,我不应该包括任何空物体。我可以调整命令发送一个空体,我可以启动我的容器:

curl -v -d '' --unix-socket /var/run/docker.sock http://localhost/v1.27/containers/0df187bd421a/start
*   Trying /var/run/docker.sock...
* Connected to localhost (/var/run/docker.sock) port 80 (#0)
> POST /v1.27/containers/0df187bd421a/start HTTP/1.1
> Host: localhost
> User-Agent: curl/7.53.0
> Accept: */*
> Content-Length: 0
> Content-Type: application/x-www-form-urlencoded
>
< HTTP/1.1 204 No Content
< Api-Version: 1.29
< Docker-Experimental: false
< Ostype: linux
< Server: Docker/17.05.0-ce (linux)
< Date: Tue, 13 Jun 2017 15:59:20 GMT
<
* Connection #0 to host localhost left intact

此问题也在此github问题中讨论:https://github.com/moby/moby/issues/25667

简短的回答是确保在容器创建时传递适当的配置,然后在调用POST /v1.27/containers/<container>/start api端点时传递空体。

答案 3 :(得分:0)

the rapid dashboard中,您可以应用几个步骤来启动和记录容器。

启动快速ui后

$ docker run -d --name rapid  \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -p 8080:8080 \
  ozlerhakan/rapid

您可以键入以下步骤:

POST images/create?fromImage=ubuntu&tag=latest

POST containers/create?name=mycontainer
{
  "Cmd": [
    "date"
  ],
  "Entrypoint": "",
  "Image": "ubuntu"
}

POST containers/mycontainer/start

GET containers/mycontainer/logs?stdout=true&tail=all

以上所有命令也可以转换为有效的cURL命令:

curl --unix-socket /var/run/docker.sock -XPOST "http:/v1.30/images/create?fromImage=ubuntu&tag=latest"

curl --unix-socket /var/run/docker.sock -XPOST "http:/v1.30/containers/create?name=mycontainer" -H "Content-Type: application/json" -d'
{
  "Cmd": [
    "date"
  ],
  "Entrypoint": "",
  "Image": "ubuntu"
}'

curl --unix-socket /var/run/docker.sock -XPOST "http:/v1.30/containers/mycontainer/start"

GET containers/mycontainer/logs?stdout=true&tail=all

curl --unix-socket /var/run/docker.sock -XGET "http:/v1.30/containers/mycontainer/logs?stdout=true&tail=all"