Docker Engine SDK を利用した開発
読む時間の目安: 7 分
Docker には、Docker デーモンとの対話的な処理を行うための API が提供されています。 (これを Docker Engine API と呼びます。) Go 言語や Python における SDK のようなものです。 SDK を使えば、Docker アプリや Docker ソリューションのビルドやスケール変更を、すばやく簡単に行うことができます。 Go 言語や Python ではうまく動作しない場合には、Docker Engine API を直接処理してみてください。
Docker Engine API は、wget
や curl
といった HTTP クライアントからアクセスできる RESTful API です。
あるいは、今日あるほとんどのプログラミング言語に組み入れられる HTTP ライブラリです。
SDK のインストール
Use the following commands to install the Go or Python SDK. Both SDKs can be installed and coexist together.
Go 言語の SDK
$ go get github.com/docker/docker/client
The client requires a recent version of Go. Run go version
and ensure that you
are running a currently supported version of Go
Read the full Docker Engine Go SDK reference.
Python SDK
-
推奨 =
pip install docker
を実行します。 -
pip
を利用できない場合は以下を行います。- パッケージを直接ダウンロード します。
- パッケージを伸張(解凍)して、生成されたディレクトリに移動します。
python setup.py install
を実行します。
Read the full Docker Engine Python SDK reference.
API リファレンスの参照
You can view the reference for the latest version of the API or choose a specific version.
Versioned API and SDK
The version of the Docker Engine API you should use depends upon the version of your Docker daemon and Docker client. Refer to the versioned API and SDK section in the API documentation for details.
SDK and API quickstart
Use the following guidelines to choose the SDK or API version to use in your code:
- If you’re starting a new project, use the latest version, but use API version negotiation or specify the version you are using. This helps prevent surprises.
- If you need a new feature, update your code to use at least the minimum version that supports the feature, and prefer the latest version you can use.
- Otherwise, continue to use the version that your code is already using.
As an example, the docker run
command can be easily implemented using the
Docker API directly, or using the Python or Go SDK.
package main
import (
"context"
"io"
"os"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
)
func main() {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
panic(err)
}
reader, err := cli.ImagePull(ctx, "docker.io/library/alpine", types.ImagePullOptions{})
if err != nil {
panic(err)
}
io.Copy(os.Stdout, reader)
resp, err := cli.ContainerCreate(ctx, &container.Config{
Image: "alpine",
Cmd: []string{"echo", "hello world"},
}, nil, nil, nil, "")
if err != nil {
panic(err)
}
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
panic(err)
}
statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning)
select {
case err := <-errCh:
if err != nil {
panic(err)
}
case <-statusCh:
}
out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true})
if err != nil {
panic(err)
}
stdcopy.StdCopy(os.Stdout, os.Stderr, out)
}
import docker
client = docker.from_env()
print client.containers.run("alpine", ["echo", "hello", "world"])
$ curl --unix-socket /var/run/docker.sock -H "Content-Type: application/json" \
-d '{"Image": "alpine", "Cmd": ["echo", "hello world"]}' \
-X POST http://localhost/v1.41/containers/create
{"Id":"1c6594faf5","Warnings":null}
$ curl --unix-socket /var/run/docker.sock -X POST http://localhost/v1.41/containers/1c6594faf5/start
$ curl --unix-socket /var/run/docker.sock -X POST http://localhost/v1.41/containers/1c6594faf5/wait
{"StatusCode":0}
$ curl --unix-socket /var/run/docker.sock "http://localhost/v1.41/containers/1c6594faf5/logs?stdout=1"
hello world
When using cURL to connect over a unix socket, the hostname is not important. The
examples above use localhost
, but any hostname would work.
Using cURL 7.47.0 or below?
The examples above assume you are using cURL 7.50.0 or above. Older versions of cURL used a non-standard URL notation when using a socket connection.
If you are using an older version of cURL, use
http:/<API version>/
instead, for example,http:/v1.41/containers/1c6594faf5/start
For more examples, take a look at the SDK examples.
Unofficial libraries
There are a number of community supported libraries available for other languages. They have not been tested by Docker, so if you run into any issues, file them with the library maintainers.
言語 | ライブラリ |
---|---|
C | libdocker |
C# | Docker.DotNet |
C++ | lasote/docker_client |
Clojure | clj-docker-client |
Clojure | contajners |
Dart | bwu_docker |
Erlang | erldocker |
Gradle | gradle-docker-plugin |
Groovy | docker-client |
Haskell | docker-hs |
HTML (Web Components) | docker-elements |
Java | docker-client |
Java | docker-java |
Java | docker-java-api |
Java | jocker |
NodeJS | dockerode |
NodeJS | harbor-master |
Perl | Eixo::Docker |
PHP | Docker-PHP |
Ruby | docker-api |
Rust | docker-rust |
Rust | shiplift |
Scala | tugboat |
Scala | reactive-docker |
Swift | docker-client-swift |