package main
import (
"fmt"
"io"
"os"
"github.com/docker/engine-api/client"
"github.com/docker/engine-api/types"
"github.com/docker/engine-api/types/container"
"golang.org/x/net/context"
)
func main() {
cli, err := client.NewClient("unix:///var/run/docker.sock", "v1.22", nil, nil)
if err != nil {
panic(err)
}
ctx := context.Background()
_, err = cli.ImagePull(ctx, "alpine", types.ImagePullOptions{})
if err != nil {
panic(err)
}
containerConfig := &container.Config{
Image: "alpine",
Cmd: []string{"echo", "hello world"},
}
resp, err := cli.ContainerCreate(ctx, containerConfig, nil, nil, "")
if err != nil {
panic(err)
}
if err := cli.ContainerStart(ctx, resp.ID); err != nil {
panic(err)
}
statusCode, err := cli.ContainerWait(ctx, resp.ID)
if err != nil {
panic(err)
}
fmt.Println(statusCode)
out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true})
if err != nil {
panic(err)
}
io.Copy(os.Stdout, out)
}
-
-
Save bfirsh/d3435b3f26033dd533cc4732639ae2ef to your computer and use it in GitHub Desktop.
Docker API docs
$ pip install docker-py
>>> import docker
>>> client = docker.from_env()
>>> client.pull("alpine")
'{"status":"Pulling from library/alpine","id":"2.6"}\r\n{"status":"Pulling fs layer","progressDetail":{},"id":"2a3ebcb7fbcc"}\r\n{"status":"Downloading","progressDetail":{"current":19889,"total":1952210},"progress":"[\\u003e ] 19.89 kB/1.952 MB","id":"2a3ebcb7fbcc"}\r\n{"status":"Downloading","progressDetail":{"current":53681,"total":1952210},"progress":"[=\\u003e ] 53.68 kB/1.952 MB","id":"2a3ebcb7fbcc"}\r\n{"status":"Downloading","progressDetail":{"current":85601,"total":1952210},"progress":"[==\\u003e ] 85.6 kB/1.952 MB","id":"2a3ebcb7fbcc"}\r\n{"status":"Downloading","progressDetail":{"current":107353,"total":1952210},"progress":"[==\\u003e ] 107.4 kB/1.952 MB","id":"2a3ebcb7fbcc"}\r\n{"status":"Downloading","progressDetail":{"current":137825,"total":1952210},"progress":"[===\\u003e ] 137.8 kB/1.952 MB","id":"2a3ebcb7fbcc"}\r\n{"status":"Downloading","progressDetail":{"current":161985,"total":1952210},"progress":"[====\\u003e ] 162 kB/1.952 MB","id":"2a3ebcb7fbcc"}\r\n{"status":"Downloading","progressDetail":{"current":191073,"total":1952210},"progress":"[====\\u003e ] 191.1 kB/1.952 MB","id":"2a3ebcb7fbcc"}\r\n{"status":"Downloading","progressDetail":{"current":223841,"total":1952210},"progress":"[=====\\u003e ] 223.8 kB/1.952 MB","id":"2a3ebcb7fbcc"}\r\n{"status":"Downloading","progressDetail":{"current":244745,"total":1952210},"progress":"[======\\u003e ] 244.7 kB/1.952 MB","id":"2a3ebcb7fbcc"}\r\n{"status":"Downloading","progressDetail":{"current":277513,"total":1952210},"progress":"[=======\\u003e ] 277.5 kB/1.952 MB","id":"2a3ebcb7fbcc"}\r\n{"status":"Downloading","progressDetail":{"current":297393,"total":1952210},"progress":"[=======\\u003e ] 297.4 kB/1.952 MB","id":"2a3ebcb7fbcc"}\r\n{"status":"Downloading","progressDetail":{"current":330161,"total":1952210},"progress":"[========\\u003e ] 330.2 kB/1.952 MB","id":"2a3ebcb7fbcc"}\r\n'
...
>>> container = client.create_container(image="alpine", command="echo hello world")
>>> client.start(container)
>>> client.wait(container)
0
>>> client.logs(container)
b'hello world\n'
$ gem install docker-api
require 'docker'
image = Docker::Image.create('fromImage' => 'alpine')
container = Docker::Container.create('Cmd' => ['echo', 'hello world'], 'Image' => 'alpine')
container.start
container.wait
# => {"StatusCode"=>0}
container.logs(stdout: true)
# => "\u0001\u0000\u0000\u0000\u0000\u0000\u0000\fhello world\n"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment