安装 SDK
$ go get github.com/docker/docker/client
使用如下方式直接连接本地的 Docker Daemon,不需要过多配置,直接用环境变量的参数初始化客户端即可。
// NewEnvClient 直接使用环境变量中的 DOCKER_HOST, DOCKER_TLS_VERIFY, DOCKER_CERT_PATH, DOCKER_API_VERSION 配置 c, err := client.NewEnvClient()
开启一个MongoDB镜像容器,5s后删除该容器
package main import ( "context" "fmt" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" "github.com/docker/docker/client" "github.com/docker/go-connections/nat" "time" ) func main() { c, err := client.NewEnvClient() if err != nil { panic(err) } ctx := context.Background() resp, err := c.ContainerCreate(ctx, &container.Config{ Image: "mongo:latest", ExposedPorts: nat.PortSet{ "27017/tcp": {}, //要暴露的端口 }, }, &container.HostConfig{ PortBindings: nat.PortMap{ "27017/tcp": []nat.PortBinding{ //容器端口 { HostIP: "0.0.0.0", HostPort: "0", //宿主机端口, 0代表随机端口 }, }, }, }, nil, nil, "") if err != nil { panic(err) } err = c.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}) if err != nil { panic(err) } fmt.Println("container started") time.Sleep(5*time.Second) inspectRes, err := c.ContainerInspect(ctx, resp.ID) if err != nil { panic(err) } fmt.Printf("listening at %+v\n",inspectRes.NetworkSettings.Ports["27017/tcp"][0])//查看当前所监听的IP端口信息 fmt.Println("killing container") err = c.ContainerRemove(ctx, resp.ID, types.ContainerRemoveOptions{ Force: true, //停掉容器 }) //删除该Docker容器 if err != nil { panic(err) } }
远程操作前提下需要进行配置免密登录,请参考我另一篇博客VSCode搭建远程服务器开发Ubuntu第一小节部分, 其次要先在云服务器上把镜像docker pull xxx
拉取下来
准备好以上重要这一步,就可以进行下面的操作,否则会报错返回错误信息
安装 SDK
$ go get github.com/docker/cli/cli/connhelper $ go get github.com/docker/docker/client
不再使用环境变量默认的配置,而是通过配置参数的方式初始化 Docker 客户端,同时指定连接的 HTTP 客户端和上下文等信息。这样当尝试连接远程服务器的 Docker Daemon 时,connhelper 就会自动完成 ssh key 的验证操作
helper, _ := connhelper.GetConnectionHelper("ssh://Username@IP:22") httpClient := &http.Client{ Transport: &http.Transport{ DialContext: helper.Dialer, }, } c, err := client.NewClientWithOpts( client.WithHTTPClient(httpClient), client.WithHost(helper.Host), client.WithDialContext(helper.Dialer), )
开启一个MongoDB镜像容器,5s后删除该容器
package main import ( "context" "fmt" "github.com/docker/cli/cli/connhelper" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" "github.com/docker/docker/client" "github.com/docker/go-connections/nat" "net/http" "time" ) func main() { helper, _ := connhelper.GetConnectionHelper("ssh://username@IP:22") httpClient := &http.Client{ Transport: &http.Transport{ DialContext: helper.Dialer, }, } c, err := client.NewClientWithOpts( client.WithHTTPClient(httpClient), client.WithHost(helper.Host), client.WithDialContext(helper.Dialer), ) ctx := context.Background() resp, err := c.ContainerCreate(ctx, &container.Config{ Image: "mongo:latest", ExposedPorts: nat.PortSet{ "27017/tcp": {}, //要暴露的端口 }, }, &container.HostConfig{ PortBindings: nat.PortMap{ "27017/tcp": []nat.PortBinding{ //容器端口 { HostIP: "0.0.0.0", HostPort: "0", //宿主机端口, 0代表随机端口 }, }, }, }, nil, nil, "") if err != nil { panic(err) } err = c.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}) if err != nil { panic(err) } fmt.Println("container started") time.Sleep(5*time.Second) inspectRes, err := c.ContainerInspect(ctx, resp.ID) if err != nil { panic(err) } fmt.Printf("listening at %+v\n",inspectRes.NetworkSettings.Ports["27017/tcp"][0])//查看当前所监听的IP端口信息 fmt.Println("killing container") err = c.ContainerRemove(ctx, resp.ID, types.ContainerRemoveOptions{ Force: true, //停掉容器 }) //删除该Docker容器 if err != nil { panic(err) } }