Docker Remote API を使ってコンテナを起動させてみた

Docker Remote API を触って、簡単なコマンドを実行してみた。

実行環境

$ docker --version
Docker version 1.12.1, build 6f9534c

API ドキュメント

docs.docker.com

コンテナの作成

$ curl -v -X POST \
  --unix-socket /var/run/docker.sock \
  -H "Content-Type: application/json" \
  -d '{"Image": "elixir", "Cmd": ["elixir", "--version"]}' \
  http:/containers/create
*   Trying /var/run/docker.sock...
* Connected to http (/Users/sinsoku/Library/Containers/com.docker.) port 80 (#0)
> POST /containers/create HTTP/1.1
> Host: http
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 51
> 
* upload completely sent off: 51 out of 51 bytes
< HTTP/1.1 201 Created
< Content-Length: 90
< Content-Type: application/json
< Date: Tue, 29 Nov 2016 17:31:03 GMT
< Server: Docker/1.12.1 (linux)
< 
{"Id":"c8542f8b6965ea049a0ea4214b3bcb171345d50f032d8960366fb3db1d28e647","Warnings":null}
* Connection #0 to host http left intact

コンテナの起動

$ curl -v -X POST \
  --unix-socket /var/run/docker.sock \
  http:/containers/c8542f8b6965ea049a0ea4214b3bcb171345d50f032d8960366fb3db1d28e647/start
*   Trying /var/run/docker.sock...
* Connected to http (/Users/sinsoku/Library/Containers/com.docker.) port 80 (#0)
> POST /containers/c8542f8b6965ea049a0ea4214b3bcb171345d50f032d8960366fb3db1d28e647/start HTTP/1.1
> Host: http
> User-Agent: curl/7.43.0
> Accept: */*
> 
< HTTP/1.1 204 No Content
< Date: Tue, 29 Nov 2016 17:31:14 GMT
< Server: Docker/1.12.1 (linux)
< 
* Connection #0 to host http left intact

コンテナの実行結果

$ curl -v --unix-socket /var/run/docker.sock \
  /containers/c8542f8b6965ea049a0ea4214b3bcb171345d50f032d8960366fb3db1d28e647/logs?stdout=1
*   Trying /var/run/docker.sock...
* Connected to http (/Users/sinsoku/Library/Containers/com.docker.) port 80 (#0)
> GET /containers/c8542f8b6965ea049a0ea4214b3bcb171345d50f032d8960366fb3db1d28e647/logs?stdout=1 HTTP/1.1
> Host: http
> User-Agent: curl/7.43.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: text/plain; charset=utf-8
< Date: Tue, 29 Nov 2016 17:32:40 GMT
< Server: Docker/1.12.1 (linux)
< Transfer-Encoding: chunked
< 
cErlang/OTP 19 [erts-8.1] [source] [64-bit] [smp:2:2] [async-threads:10] [hipe] [kernel-poll:false]

Elixir 1.3.4
* Connection #0 to host http left intact