2015年1月22日木曜日

groovyとDocker Remote APIでcontainerを停止する

groovyとDocker Remote APIでcontainerを停止するには、以下のようなコードを実行します。

サンプルコード
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.3.5')
import org.apache.http.client.methods.*
import org.apache.http.impl.client.*
import org.apache.http.message.*
import org.apache.http.auth.*
import org.apache.http.entity.*
import groovy.json.*

def host = "192.168.1.215" // replace this
def port = 4243
def containerId = "your-container-id"
def t2k = 10 // wait 10sec

def httpclient = new DefaultHttpClient()
httpclient.withCloseable {

  def method = new HttpPost(
    "http://${host}:${port}/containers/${containerId}/stop?t=${t2k}"
  )

  def response = httpclient.execute(method)
  println response.getStatusLine().getStatusCode()
}
Docker Remote APIを使用可能にする方法
ubuntuの場合、/etc/default/dockerに以下のように記述します。
DOCKER_OPTS="-H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock -d"
そのあと、dockerを再起動します。
sudo service docker restart

動作環境
Docker 1.2.0

0 件のコメント:

コメントを投稿