下载包
go get "github.com/docker/docker/api/types"
go get "github.com/docker/docker/client"
创建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
", 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
", 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
", 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已经被停止
", 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
", 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下来即可。
注意: 客户端版本和docker版本兼容问题