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 httpclient = new DefaultHttpClient()
httpclient.withCloseable {
def method = new HttpPost(
"http://${host}:${port}/containers/${containerId}/start"
)
def json = new JsonBuilder()
json (
[PortBindings:[ "80/tcp":[["HostPort":"8080"]] ]]
)
println json
method.setHeader("Content-Type", "application/json; charset=utf-8")
method.setEntity(new StringEntity(json.toString(), "UTF-8"))
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