[TOC]
通过docker sdk api操作docker
以下为使用go语言的sdk,官网及源码里面的测试用例都没有一个完整的创建容器的例子(带端口映射等),真TM烂,只好自己整理出来。
其中也遇到些坑,最坑的是在使用端口映射时报的类型问题,见最后
Container
其中除了创建有点复杂,其它操作都比较简单。
创建Container
其主要复杂在端口的映射上面。如果hostConfig
里面的PortBindings
对应的PortMap
中相应端口对应的PortBinding
,其值有几个(空值也算)就会对外映射几个端口。
代码如:
func createContainer(cli *client.Client) string {
exports := make(nat.PortSet, 10)
port, err := nat.NewPort("tcp", "80")
log(err)
exports[port] = struct{}{}
config := &container.Config{Image: "nginx", ExposedPorts: exports}
portBind := nat.PortBinding{HostPort: "80"}
portMap := make(nat.PortMap, 0)
tmp := make([]nat.PortBinding, 0, 1)
tmp = append(tmp, portBind)
portMap[port] = tmp
hostConfig := &container.HostConfig{PortBindings: portMap}
// networkingConfig := &network.NetworkingConfig{}
containerName := "hel"
body, err := cli.ContainerCreate(context.Background(), config, hostConfig, nil, containerName)
log(err)
fmt.Printf("ID: %s\n", body.ID)
return body.ID
}
其中启动、停止、删除都比较简单,不再一步一步说明。完整代码如下:
package main
import (
"context"
"fmt"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
)
func main() {
cli, err := client.NewClient("tcp://192.168.100.33:2376", "v1.12", nil, nil)
defer cli.Close()
log(err)
listImage(cli)
id := createContainer(cli)
fmt.Printf("%s\n", id)
time.Sleep(time.Second * 1)
startContainer(id, cli)
time.Sleep(time.Second * 40)
stopContainer(id, cli)
time.Sleep(time.Second * 3)
id, err = removeContainer(id, cli)
if err == nil {
fmt.Println("删除容器", id, "成功")
}
}
// 列出镜像
func listImage(cli *client.Client) {
images, err := cli.ImageList(context.Background(), types.ImageListOptions{})
log(err)
for _, image := range images {
fmt.Println(image)
}
}
// 创建容器
func createContainer(cli *client.Client) string {
exports := make(nat.PortSet, 10)
port, err := nat.NewPort("tcp", "80")
log(err)
exports[port] = struct{}{}
config := &container.Config{Image: "nginx", ExposedPorts: exports}
portBind := nat.PortBinding{HostPort: "80"}
portMap := make(nat.PortMap, 0)
tmp := make([]nat.PortBinding, 0, 1)
tmp = append(tmp, portBind)
portMap[port] = tmp
hostConfig := &container.HostConfig{PortBindings: portMap}
containerName := "hel"
body, err := cli.ContainerCreate(context.Background(), config, hostConfig, nil, containerName)
log(err)
fmt.Printf("ID: %s\n", body.ID)
return body.ID
}
// 启动
func startContainer(containerID string, cli *client.Client) {
err := cli.ContainerStart(context.Background(), containerID, types.ContainerStartOptions{})
log(err)
if err == nil {
fmt.Println("容器", containerID, "启动成功")
}
}
// 停止
func stopContainer(containerID string, cli *client.Client) {
timeout := time.Second * 10
err := cli.ContainerStop(context.Background(), containerID, &timeout)
if err != nil {
log(err)
} else {
fmt.Printf("容器%s已经被停止\n", containerID)
}
}
// 删除
func removeContainer(containerID string, cli *client.Client) (string, error) {
err := cli.ContainerRemove(context.Background(), containerID, types.ContainerRemoveOptions{})
log(err)
return containerID, err
}
func log(err error) {
if err != nil {
fmt.Printf("%v\n", err)
panic(err)
}
}
端口映射问题
在使用docker go sdk的时候,进行容器创建,如果指定了映射的端口,会报以下错误:
cannot use ports (type map["bitbucket.org/myorg/myproject/vendor/github.com/docker/go-connect
ions/nat".Port]struct {}) as type "github.com/docker/docker/vendor/github.com/docker/go-connections/nat".PortSet in field
value
其解决方法:把gopath里面pkg下面docker的vendor
里面相应的connections包删除即可。src下的最好也删除。如果编译报缺少包,则把其相应的包get下来即可。