title |
---|
Getting started with the Engine API |
To try out the Docker Engine API in development, you first need to install Docker.
Next, you need to install an SDK for the language you are using. There are official ones available for Python and Go, and a number of community maintained libraries for other languages. Head to the SDKs page to find and install them.
The most basic thing you can do with Docker is running a container. On the command line, you would use the docker run
command, but this is just as easy to do from your own apps too.
This is the equivalent of doing docker run alpine echo hello world
:
import ( "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() { ctx := context.Background() cli, err := client.NewEnvClient() if err != nil { panic(err) }
_, err = cli.ImagePull(ctx, "alpine", types.ImagePullOptions{})
if err != nil {
panic(err)
}
resp, err := cli.ContainerCreate(ctx, &container.Config{
Image: "alpine",
Cmd: []string{"echo", "hello world"},
}, nil, nil, "")
if err != nil {
panic(err)
}
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
panic(err)
}
if _, err = cli.ContainerWait(ctx, resp.ID); err != nil {
panic(err)
}
out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true})
if err != nil {
panic(err)
}
io.Copy(os.Stdout, out)
} {% endhighlight %}
$ curl --unix-socket /var/run/docker.sock -X POST http:/v1.24/containers/1c6594faf5/start
$ curl --unix-socket /var/run/docker.sock -X POST http:/v1.24/containers/1c6594faf5/wait {"StatusCode":0}
$ curl --unix-socket /var/run/docker.sock "http:/v1.24/containers/1c6594faf5/logs?stdout=1" hello world {% endhighlight %}
You can also run containers in the background, the equivalent of docker run -d bfirsh/reticulate-splines
:
import ( "fmt" "io" "os"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"golang.org/x/net/context"
)
func main() { ctx := context.Background() cli, err := client.NewEnvClient() if err != nil { panic(err) }
imageName := "bfirsh/reticulate-splines"
out, err := cli.ImagePull(ctx, imageName, types.ImagePullOptions{})
if err != nil {
panic(err)
}
io.Copy(os.Stdout, out)
resp, err := cli.ContainerCreate(ctx, &container.Config{
Image: imageName,
}, nil, nil, "")
if err != nil {
panic(err)
}
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
panic(err)
}
fmt.Println(resp.ID)
} {% endhighlight %}
$ curl --unix-socket /var/run/docker.sock -X POST http:/v1.24/containers/1c6594faf5/start {% endhighlight %}
Like docker ps
, we can use the API to list containers that are running:
import ( "context" "fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
func main() { cli, err := client.NewEnvClient() if err != nil { panic(err) }
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})
if err != nil {
panic(err)
}
for _, container := range containers {
fmt.Println(container.ID)
}
} {% endhighlight %}
{% endhighlight %}
Now we know what containers exist, we can perform operations on them. For example, we can stop all running containers:
import ( "context"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
func main() { ctx := context.Background() cli, err := client.NewEnvClient() if err != nil { panic(err) }
containers, err := cli.ContainerList(ctx, types.ContainerListOptions{})
if err != nil {
panic(err)
}
for _, container := range containers {
if err := cli.ContainerStop(ctx, container.ID, nil); err != nil {
panic(err)
}
}
} {% endhighlight %}
$ curl --unix-socket /var/run/docker.sock
-X POST http:/v1.24/containers/ae63e8b89a26/stop
{% endhighlight %}
We can also perform actions on individual containers. For example, to print the logs of a container given its ID:
import ( "context" "io" "os"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
func main() { ctx := context.Background() cli, err := client.NewEnvClient() if err != nil { panic(err) }
options := types.ContainerLogsOptions{ShowStdout: true}
out, err := cli.ContainerLogs(ctx, "f1064a8a4c82", options)
if err != nil {
panic(err)
}
io.Copy(os.Stdout, out)
} {% endhighlight %}
Images are the basis of containers, and can be managed in a similar way. You can list the images on your Engine, similar to docker images
:
import ( "context" "fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
func main() { cli, err := client.NewEnvClient() if err != nil { panic(err) }
images, err := cli.ImageList(context.Background(), types.ImageListOptions{})
if err != nil {
panic(err)
}
for _, image := range images {
fmt.Println(image.ID)
}
} {% endhighlight %}
You can pull images, like docker pull
:
import ( "io" "os"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"golang.org/x/net/context"
)
func main() { ctx := context.Background() cli, err := client.NewEnvClient() if err != nil { panic(err) }
out, err := cli.ImagePull(ctx, "alpine", types.ImagePullOptions{})
if err != nil {
panic(err)
}
io.Copy(os.Stdout, out)
} {% endhighlight %}
And commit containers to create images from their contents:
import ( "fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"golang.org/x/net/context"
)
func main() { ctx := context.Background() cli, err := client.NewEnvClient() if err != nil { panic(err) }
createResp, err := cli.ContainerCreate(ctx, &container.Config{
Image: "alpine",
Cmd: []string{"touch", "/helloworld"},
}, nil, nil, "")
if err != nil {
panic(err)
}
if err := cli.ContainerStart(ctx, createResp.ID, types.ContainerStartOptions{}); err != nil {
panic(err)
}
if _, err = cli.ContainerWait(ctx, createResp.ID); err != nil {
panic(err)
}
commitResp, err := cli.ContainerCommit(ctx, createResp.ID, types.ContainerCommitOptions{Reference: "helloworld"})
if err != nil {
panic(err)
}
fmt.Println(commitResp.ID)
} {% endhighlight %}