Docker 提供了一個(gè)與 Docker 守護(hù)進(jìn)程交互的 API (稱為Docker Engine API),我們可以使用官方提供的 Go 語(yǔ)言的 SDK 進(jìn)行構(gòu)建和擴(kuò)展 Docker 應(yīng)用程序和解決方案。
通過下面的命令就可以安裝 SDK 了:
go get github.com/docker/docker/client
該部分會(huì)介紹如何使用 Golang + Docker API 進(jìn)行管理本地的 Docker。
第一個(gè)例子將展示如何運(yùn)行容器,相當(dāng)于 docker run docker.io/library/alpine echo "hello world":
package main import ( "context" "io" "os" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" "github.com/docker/docker/client" "github.com/docker/docker/pkg/stdcopy" ) func main() { ctx := context.Background() cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) if err != nil { panic(err) } reader, err := cli.ImagePull(ctx, "docker.io/library/alpine", types.ImagePullOptions{}) if err != nil { panic(err) } io.Copy(os.Stdout, reader) 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) } statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning) select { case err := <-errCh: if err != nil { panic(err) } case <-statusCh: } out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true}) if err != nil { panic(err) } stdcopy.StdCopy(os.Stdout, os.Stderr, out) }
還可以在后臺(tái)運(yùn)行容器,相當(dāng)于 docker run -d bfirsh/reticulate-splines:
package main import ( "context" "fmt" "io" "os" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" "github.com/docker/docker/client" ) func main() { ctx := context.Background() cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) 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) }
列出正在運(yùn)行的容器,就像使用 docker ps 一樣:
package main import ( "context" "fmt" "github.com/docker/docker/api/types" "github.com/docker/docker/client" ) func main() { ctx := context.Background() cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) if err != nil { panic(err) } containers, err := cli.ContainerList(ctx, types.ContainerListOptions{}) if err != nil { panic(err) } for _, container := range containers { fmt.Println(container.ID) } }
如果是 docker ps -a,我們可以通過修改 types.ContainerListOptions 中的 All 屬性達(dá)到這個(gè)目的:
// type ContainerListOptions struct { // Quiet bool // Size bool // All bool // Latest bool // Since string // Before string // Limit int // Filters filters.Args // } options := types.ContainerListOptions{ All: true, } containers, err := cli.ContainerList(ctx, options) if err != nil { panic(err) }
通過上面的例子,我們可以獲取容器的列表,所以在這個(gè)案例中,我們可以去停止所有正在運(yùn)行的容器。
注意:不要在生產(chǎn)服務(wù)器上運(yùn)行下面的代碼。
package main import ( "context" "fmt" "github.com/docker/docker/api/types" "github.com/docker/docker/client" ) func main() { ctx := context.Background() cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) if err != nil { panic(err) } containers, err := cli.ContainerList(ctx, types.ContainerListOptions{}) if err != nil { panic(err) } for _, container := range containers { fmt.Print("Stopping container ", container.ID[:10], "... ") if err := cli.ContainerStop(ctx, container.ID, nil); err != nil { panic(err) } fmt.Println("Success") } }
通過指定容器的 ID,我們可以獲取對(duì)應(yīng) ID 的容器的日志:
package main import ( "context" "io" "os" "github.com/docker/docker/api/types" "github.com/docker/docker/client" ) func main() { ctx := context.Background() cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) 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) }
獲取本地所有的鏡像,相當(dāng)于 docker image ls 或 docker images:
package main import ( "context" "fmt" "github.com/docker/docker/api/types" "github.com/docker/docker/client" ) func main() { ctx := context.Background() cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) if err != nil { panic(err) } images, err := cli.ImageList(ctx, types.ImageListOptions{}) if err != nil { panic(err) } for _, image := range images { fmt.Println(image.ID) } }
拉取指定鏡像,相當(dāng)于 docker pull alpine:
package main import ( "context" "io" "os" "github.com/docker/docker/api/types" "github.com/docker/docker/client" ) func main() { ctx := context.Background() cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) if err != nil { panic(err) } out, err := cli.ImagePull(ctx, "alpine", types.ImagePullOptions{}) if err != nil { panic(err) } defer out.Close() io.Copy(os.Stdout, out) }
除了公開的鏡像,我們平時(shí)還會(huì)用到一些私有鏡像,可以是 DockerHub 上私有鏡像,也可以是自托管的鏡像倉(cāng)庫(kù),比如 harbor。這個(gè)時(shí)候,我們需要提供對(duì)應(yīng)的憑證才可以拉取鏡像。
值得注意的是:在使用 Docker API 的 Go SDK 時(shí),憑證是以明文的方式進(jìn)行傳輸?shù)?,所以如果是自建的鏡像倉(cāng)庫(kù),請(qǐng)務(wù)必使用 HTTPS!
package main import ( "context" "encoding/base64" "encoding/json" "io" "os" "github.com/docker/docker/api/types" "github.com/docker/docker/client" ) func main() { ctx := context.Background() cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) if err != nil { panic(err) } authConfig := types.AuthConfig{ Username: "username", Password: "password", } encodedJSON, err := json.Marshal(authConfig) if err != nil { panic(err) } authStr := base64.URLEncoding.EncodeToString(encodedJSON) out, err := cli.ImagePull(ctx, "alpine", types.ImagePullOptions{RegistryAuth: authStr}) if err != nil { panic(err) } defer out.Close() io.Copy(os.Stdout, out) }
我們可以將一個(gè)已有的容器通過 commit 保存成一個(gè)鏡像:
package main import ( "context" "fmt" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" "github.com/docker/docker/client" ) func main() { ctx := context.Background() cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) 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) } statusCh, errCh := cli.ContainerWait(ctx, createResp.ID, container.WaitConditionNotRunning) select { case err := <-errCh: if err != nil { panic(err) } case <-statusCh: } commitResp, err := cli.ContainerCommit(ctx, createResp.ID, types.ContainerCommitOptions{Reference: "helloworld"}) if err != nil { panic(err) } fmt.Println(commitResp.ID) }
當(dāng)然,除了可以管理本地的 Docker, 我們同樣也可以通過使用 Golang + Docker API 管理遠(yuǎn)程的 Docker。
默認(rèn) Docker 是通過非網(wǎng)絡(luò)的 Unix 套接字運(yùn)行的,只能夠進(jìn)行本地通信(/var/run/docker.sock),是不能夠直接遠(yuǎn)程連接 Docker 的。
我們需要編輯配置文件 /etc/docker/daemon.json,并修改以下內(nèi)容(把 192.168.59.3 改成你自己的 IP 地址),然后重啟 Docker:
# vi /etc/docker/daemon.json { "hosts": [ "tcp://192.168.59.3:2375", "unix:///var/run/docker.sock" ] } systemctl restart docker
創(chuàng)建 client 的時(shí)候需要指定遠(yuǎn)程 Docker 的地址,這樣就可以像管理本地 Docker 一樣管理遠(yuǎn)程的 Docker 了:
cli, err = client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation(), client.WithHost("tcp://192.168.59.3:2375"))
現(xiàn)在已經(jīng)有很多可以管理 Docker 的產(chǎn)品,它們便是這樣進(jìn)行實(shí)現(xiàn)的,比如:portainer。
到此這篇關(guān)于使用Golang玩轉(zhuǎn)Docker API的實(shí)踐的文章就介紹到這了,更多相關(guān)Golang運(yùn)行Docker API內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:雞西 隴南 朝陽(yáng) 運(yùn)城 遵義 連云港 荊門 面試通知
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《使用Golang玩轉(zhuǎn)Docker API的實(shí)踐》,本文關(guān)鍵詞 使用,Golang,玩轉(zhuǎn),Docker,API,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。上一篇:docker的一些基本指令