go-dockerclient

Docker Engine API的Go客户端。(Go client for the Docker Engine API.)

Github stars Tracking Chart

go-dockerclient

这个包为 Docker 远程 API 提供了一个客户端,它还为 Swarm API 中的扩展提供了支持。它还提供了对 Swarm API 中扩展的支持。

这个包还提供了对 docker 的网络 API 的支持,它是对 libnetwork 远程 API 的一个简单直通。

更多细节,请查看远程 API 文档

go-dockerclient 和官方 SDK 的区别

官方 SDK 链接:https://docs.docker.com/develop/sdk/

go-dockerclient 是在 Docker 有正式的 Go SDK 之前创建的,现在还在维护和活跃,因为它仍然在使用。Docker API 中的新特性不会在这里自动实现:这是基于需求的,如果有人想要,他们可以提交一个问题或 PR,这个功能可能会被实现/合并。
对于新项目,使用官方 SDK 可能更合适,因为 go-dockerclient 落后于官方 SDK。

当使用官方 SDK 时,请记住,由于其依赖关系的组织方式,你可能需要一些额外的步骤才能将其导入到你的项目中(参见 #784moby/moby#28269)。

例子

package main
import (
    "fmt"
    docker "github.com/fsouza/go-dockerclient"
)
func main() {
    client, err := docker.NewClientFromEnv()
    if err != nil {
        panic(err)
    }
    imgs, err := client.ListImages(docker.ListImagesOptions{All: false})
    if err != nil {
        panic(err)
    }
    for _, img := range imgs {
        fmt.Println("ID: ", img.ID)
        fmt.Println("RepoTags: ", img.RepoTags)
        fmt.Println("Created: ", img.Created)
        fmt.Println("Size: ", img.Size)
        fmt.Println("VirtualSize: ", img.VirtualSize)
        fmt.Println("ParentId: ", img.ParentID)
    }
}

使用TLS

为了实例化一个启用了TLS的守护进程的客户端,你应该使用NewTLSClient,将端点、密钥和证书的路径作为参数。

package main
import (
    "fmt"
    docker "github.com/fsouza/go-dockerclient"
)
func main() {
    const endpoint = "tcp://[ip]:[port]"
    path := os.Getenv("DOCKER_CERT_PATH")
    ca := fmt.Sprintf("%s/ca.pem", path)
    cert := fmt.Sprintf("%s/cert.pem", path)
    key := fmt.Sprintf("%s/key.pem", path)
    client, _ := docker.NewTLSClient(endpoint, cert, key, ca)
    // use client
}

如果使用 docker-machine,或者其他导出环境变量 DOCKER_HOST、DOCKER_TLS_VERIFY、DOCKER_CERT_PATH、DOCKER_API_VERSION 的应用程序,你可以使用 NewClientFromEnv。

package main
import (
    "fmt"
    docker "github.com/fsouza/go-dockerclient"
)
func main() {
    client, err := docker.NewClientFromEnv()
    if err != nil {
        // handle err
    }
    // use client
}

更多细节请参见文档。

开发

所有的开发命令都可以在 Makefile 中看到。

提交的代码必须通过。

运行 make test 将运行所有检查,以及安装任何所需的依赖关系。

模块

go-dockerclient 支持 Go 模块。

如果你使用的是 dep,你可以在发布页面查看与 dep 完全兼容的最新版本。

如果使用其他厂商的工具,用户需要手动指定 go-dockerclient 的依赖关系。

与 Docker 1.9 和 Go 1.4 一起使用。

有一个标签用于在 Docker 1.9 中使用 go-dockerclient(需要将 go-dockerclient 与 Go 1.4 一起编译),标签名称为d ocker-1.9/go-1.4。

下面的说明可以用来获取与 Go 1.4 编译的 go-dockerclient 版本。

% git clone -b docker-1.9/go-1.4 https://github.com/fsouza/go-dockerclient.git $GOPATH/src/github.com/fsouza/go-dockerclient
% git clone -b v1.9.1 https://github.com/docker/docker.git $GOPATH/src/github.com/docker/docker
% go get github.com/fsouza/go-dockerclient


(The first version translated by vz on 2020.08.29)

Overview

Name With Ownerfsouza/go-dockerclient
Primary LanguageGo
Program languageGo (Language Count: 3)
PlatformLinux, Mac, Windows
License:BSD 2-Clause "Simplified" License
Release Count60
Last Release Namev1.11.0 (Posted on )
First Release Namedocker-1.9/go-1.4 (Posted on 2017-07-26 20:35:09)
Created At2013-05-11 11:21:31
Pushed At2024-05-03 15:15:44
Last Commit At
Stargazers Count2.1k
Watchers Count55
Fork Count562
Commits Count2.4k
Has Issues Enabled
Issues Count405
Issue Open Count10
Pull Requests Count551
Pull Requests Open Count0
Pull Requests Close Count100
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

go-dockerclient

Travis Build Status
AppVeyor Build status
GoDoc

This package presents a client for the Docker remote API. It also provides
support for the extensions in the Swarm API.

This package also provides support for docker's network API, which is a simple
passthrough to the libnetwork remote API.

For more details, check the remote API
documentation
.

Difference between go-dockerclient and the official SDK

Link for the official SDK: https://docs.docker.com/develop/sdk/

go-dockerclient was created before Docker had an official Go SDK and is
still maintained and relatively active because it's still used out there. New
features in the Docker API do not get automatically implemented here: it's
based on demand, if someone wants it, they can file an issue or a PR and the
feature may get implemented/merged.

For new projects, using the official SDK is probably more appropriate as
go-dockerclient lags behind the official SDK.

When using the official SDK, keep in mind that because of how the its
dependencies are organized, you may need some extra steps in order to be able
to import it in your projects (see
#784 and
moby/moby#28269).

Example

package main

import (
	"fmt"

	docker "github.com/fsouza/go-dockerclient"
)

func main() {
	client, err := docker.NewClientFromEnv()
	if err != nil {
		panic(err)
	}
	imgs, err := client.ListImages(docker.ListImagesOptions{All: false})
	if err != nil {
		panic(err)
	}
	for _, img := range imgs {
		fmt.Println("ID: ", img.ID)
		fmt.Println("RepoTags: ", img.RepoTags)
		fmt.Println("Created: ", img.Created)
		fmt.Println("Size: ", img.Size)
		fmt.Println("VirtualSize: ", img.VirtualSize)
		fmt.Println("ParentId: ", img.ParentID)
	}
}

Using with TLS

In order to instantiate the client for a TLS-enabled daemon, you should use
NewTLSClient, passing the endpoint and path for key and certificates as
parameters.

package main

import (
	"fmt"

	docker "github.com/fsouza/go-dockerclient"
)

func main() {
	const endpoint = "tcp://[ip]:[port]"
	path := os.Getenv("DOCKER_CERT_PATH")
	ca := fmt.Sprintf("%s/ca.pem", path)
	cert := fmt.Sprintf("%s/cert.pem", path)
	key := fmt.Sprintf("%s/key.pem", path)
	client, _ := docker.NewTLSClient(endpoint, cert, key, ca)
	// use client
}

If using docker-machine, or another
application that exports environment variables DOCKER_HOST,
DOCKER_TLS_VERIFY, DOCKER_CERT_PATH, DOCKER_API_VERSION, you can use
NewClientFromEnv.

package main

import (
	"fmt"

	docker "github.com/fsouza/go-dockerclient"
)

func main() {
	client, _ := docker.NewClientFromEnv()
	// use client
}

See the documentation for more details.

Developing

All development commands can be seen in the Makefile.

Commited code must pass:

Running make test will check all of these. You can reformat the code with
make fmt.

Modules

go-dockerclient supports Go modules.

If you're using dep, you can check the releases
page
for the latest
release fully compatible with dep.

With other vendoring tools, users might need to specify go-dockerclient's
dependencies manually.

Using with Docker 1.9 and Go 1.4

There's a tag for using go-dockerclient with Docker 1.9 (which requires
compiling go-dockerclient with Go 1.4), the tag name is docker-1.9/go-1.4.

The instructions below can be used to get a version of go-dockerclient that compiles with Go 1.4:

% git clone -b docker-1.9/go-1.4 https://github.com/fsouza/go-dockerclient.git $GOPATH/src/github.com/fsouza/go-dockerclient
% git clone -b v1.9.1 https://github.com/docker/docker.git $GOPATH/src/github.com/docker/docker
% go get github.com/fsouza/go-dockerclient
To the top