Docker Deep 2-api Example
2017/9/18
First, the purpose of demonstration http API use of the way Note 1: this instance is used under docker swarm mode , the purpose is: Update the image of the specified service. Note 2: To be executed on swarm manager node . docker API documents are automatically generated and there are not too many useful examples available. "Version" ~]# docker versionclient: version: 17.06.0-ce api version: 1.30 Go version: go1.8.3 Git commit: 02c1d87 Built: Fri Jun 23 21:20:36 2017 os/arch: linux/amd64server: version: 17.06.0-ce API version: 1.30 (minimum version 1.12) go version: go1.8.3 git commit: 02c1d87 Built: Fri Jun 23 21:21:56 2017 Os/arch: linux/amd64 experimental: false Two, example 1, create a service docker service create --name t001 -- publish 22222:80 --detach=true opera443399/whoami:0.72, update Service 1) Target service_image_latest= " opera443399/whoami:0.6 "Service_name=" t001 "2) get the version of the current service service_version_index=$ (curl -s --unix-socket /var/run/docker.sock http:/v1.30/services? filters= ' \{"name": \["' ${service_name} '" \]\} ' |jq '. []. Version.index ') 3) Perform update curl -s --unix-socket /var/run/docker.sock "Http:/v1.30/services/${service_name}/update?version=${service_version_index}" -X POST -H "content-type: Application/json " -d " { \ "name\": \ "${service_name}\", &Nbsp; \ "Tasktemplate\": { \ "Containerspec\": { \ "image\": \ "${service_image_latest}\" } } } " |jq ".     4) View service status curl -s --unix-socket /var/run/ docker.sock http:/services?filters= ' \{"name": \["' ${service_name} '" \]\} ' |jq '. ' 1. What if you use a custom network when creating service ? Status: Unresolved note this paragraph about Networks notes: referenced from: https://github.com/moby/moby/blob/master/api/types/swarm/service.go// servicespec represents the spec of a&nBsp;service.type servicespec struct {annotations// tasktemplate defines how the service should construct new tasks when// orchestrating this service. tasktemplate taskspec ' JSON: ", Omitempty" ' Mode ServiceMode ' JSON: ', Omitempty ' updateconfig *updateconfig ' JSON: ", omitempty" ' rollbackconfig *updateconfig ' JSON: ", Omitempty" '// networks field in servicespec is deprecated. the// same field in taskspec should be used instead.// this field will be removed in a future release. networks []networkattachmentconfig ' JSON: ", Omitempty" ' EndpointSpec * Endpointspec ' JSON: ", Omitempty" '} Reference from: https://github.com/moby/moby/blob/master/api/types/ Swarm/task.go// taskspec represents the spec of a task.type taskspec struct {// containerspec and pluginspec are mutually exclusive.// PluginSpec will only be used when the ' Runtime ' field is set to ' plugin ' containerspec *containerspec ' JSON: ", Omitempty "' Pluginspec *runtime. pluginspec ' JSON: ", Omitempty" ' resources *resourcerequirements ' JSON: ', Omitempty ' restartpolicy *restartpolicy ' JSON: ', Omitempty ' placement *placement &nBSP; ' JSON: ', omitempty "' networks []networkattachmentconfig ' JSON:", Omitempty "'// logdriver specifies the logdriver to use for tasks Created from this// spec. if not present, the one on cluster default on swarm. spec will be// used, finally falling back to the engine Default if not specified. logdriver *driver ' JSON: ", Omitempty" '// forceupdate is a counter that Triggers an update even if no relevant// parameters have been changed. forceupdate uint64runtime runtimetype ' JSON: ', Omitempty '} tried in API Added: Tasktemplate.networks { \ "Name\": \ "${service_name}\", &nbsP; \ "tasktemplate\": { \ " Containerspec\ ": { \ "image\": \ "${service_image_latest}\" }, \ "Networks\": [ { \ "target\": \ "xxx\" } ] } But the effect is: The ingress network disappears, the service outbound port disappears. Because, when you create service : DOCKER&Nbsp;service create --name t001 --network t001only --publish 22222:80 - -detach=true opera443399/whoami:0.7 use --network will be associated to a network t001only use --publish will be associated to a network ingress Therefore, there are actually 2 networks. Note 1: In the process of repeated testing, there is a strange phenomenon, when the creation of service , the container is in the state of new , unable to go online, temporarily did not find the reason, thus suspended the test. NOTE 2: It may be possible to try to specify ip and port information during the update service process, and then update this section information after the next test.         ZYXW, reference 1, apihttps://docs.docker.com/engine/api/v1.302, Moby srchttps://github.com/moby/moby/blob/master/api/types/swarm/task.gohttps://github.com/moby/moby/blob/ Master/api/types/swarm/service.go3, portainer srchttps://github.com/portainer/portainer/blob/ 04ea81e7cd8401690058c4b4264452bf9d7a05eb/app/components/service/servicecontroller.js
Docker Deep 2-api Example