Docker Remote APIでservice createする



##やりたいコマンド

```
$ docker service create \
--name example001 \
-p 30001:8080 
--mount type=volume,src=fs-XXXXXX/example001,dst=/mnt/userdata,volume-driver=efs \
nginx:latest
```

###オプションメモ

|Param|説明|
|:--|:--|
|--name|サービス名|
|-p|ノード・ポートとしてポートを公開|
|--mount|サービスに対するマウントをアタッチ|


## APIリクエスト

```
POST /services/create -d '{
  "Name": "example001",
  "TaskTemplate": {
    "ContainerSpec": {
      "Image": "redis:3.0.6",
      "Mounts": [{
      	"Type": "volume",
      	"Target": "/mnt/userdata",
      	"Source": "fs-XXXXXX/example001",
      	"VolumeOptions": {
      	  "DriverConfig": {
      	  	"Name": "efs"
      	  }
      	}
      }]
    }
  },
  "EndpointSpec": {
    "Ports": [
      {
        "Protocol": "tcp",
        "PublishedPort": 30001,
        "TargetPort": 8080
      }
    ]
  }
}'
```

## 実行結果

成功するとSwarmのサービスIDが返ってくる。

```
{"ID":"XXXXXXXXXXXXXXXXXXXXXXXXX"}
```

エラーの場合はこう。（すでに使っているportを使った）

```
{"message":"rpc error: code = 3 desc = port '30001' is already in use by service XXXXXXXXXXXXXXXXXXXXXXXXX"}
```

サービスIDが返ってきてるので、ステータスも確認してみる。

```
GET /services/XXXXXXXXXXXXXXXXXXXXXXXXX | jq .
{
  "ID": "XXXXXXXXXXXXXXXXXXXXXXXXX",
  "Version": {
    "Index": 615
  },
  "CreatedAt": "2016-08-17T12:43:11.176565145Z",
  "UpdatedAt": "2016-08-17T12:43:11.2277272Z",
  "Spec": {
    "Name": "example001",
    "TaskTemplate": {
      "ContainerSpec": {
        "Image": "nginx:latest",
        "Mounts": [
          {
            "Type": "volume",
            "Source": "fs-XXXXXX/example001",
            "Target": "/mnt/userdata",
            "VolumeOptions": {
              "DriverConfig": {
                "Name": "efs"
              }
            }
          }
        ]
      }
    },
    "Mode": {
      "Replicated": {
        "Replicas": 1
      }
    },
    "EndpointSpec": {
      "Mode": "vip",
      "Ports": [
        {
          "Protocol": "tcp",
          "TargetPort": 8080,
          "PublishedPort": 30001
        }
      ]
    }
  },
  "Endpoint": {
    "Spec": {
      "Mode": "vip",
      "Ports": [
        {
          "Protocol": "tcp",
          "TargetPort": 8080,
          "PublishedPort": 30001
        }
      ]
    },
    "Ports": [
      {
        "Protocol": "tcp",
        "TargetPort": 8080,
        "PublishedPort": 30001
      }
    ],
    "VirtualIPs": [
      {
        "NetworkID": "XXXXXXXXXXXXX",
        "Addr": "10.255.0.15/16"
      }
    ]
  },
  "UpdateStatus": {
    "StartedAt": "0001-01-01T00:00:00Z",
    "CompletedAt": "0001-01-01T00:00:00Z"
  }
}
```

##サービスを消す

GETじゃなくてDELETEすればOK

```
DELETE /services/XXXXXXXXXXXXXXXXXXXXXXXXX
```
戻り値はなし。

```
GET /services/XXXXXXXXXXXXXXXXXXXXXXXXX | jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    58  100    58    0     0    130      0 --:--:-- --:--:-- --:--:--   130
{
  "message": "service XXXXXXXXXXXXXXXXXXXXXXXXX not found"
}
```
もう一度GETしようとすると、「そんなサービスねーよ」って返ってきます。

```
DELETE /services/XXXXXXXXXXXXXXXXXXXXXXXXX | jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    58  100    58    0     0     99      0 --:--:-- --:--:-- --:--:--   100
{
  "message": "service XXXXXXXXXXXXXXXXXXXXXXXXX not found"
}
```
DELETEでも同じですね。

##サービスIDじゃなくてサービス名でもOK
```
GET /services/{SERVISE_NAME}
DELETE /services/{SERVISE_NAME}
```
でもいけます。