使用Golang玩轉(zhuǎn)Docker API的實踐
Docker 提供了一個與 Docker 守護進程交互的 API (稱為Docker Engine API),我們可以使用官方提供的 Go 語言的 SDK 進行構(gòu)建和擴展 Docker 應(yīng)用程序和解決方案。
安裝 SDK通過下面的命令就可以安裝 SDK 了:
go get github.com/docker/docker/client管理本地的 Docker
該部分會介紹如何使用 Golang + Docker API 進行管理本地的 Docker。
運行容器第一個例子將展示如何運行容器,相當于 docker run docker.io/library/alpine echo 'hello world':
package mainimport ( '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)}后臺運行容器
還可以在后臺運行容器,相當于 docker run -d bfirsh/reticulate-splines:
package mainimport ( '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)}查看容器列表
列出正在運行的容器,就像使用 docker ps 一樣:
package mainimport ( '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 屬性達到這個目的:
// 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)}停止所有運行中的容器
通過上面的例子,我們可以獲取容器的列表,所以在這個案例中,我們可以去停止所有正在運行的容器。
注意:不要在生產(chǎn)服務(wù)器上運行下面的代碼。
package mainimport ( '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,我們可以獲取對應(yīng) ID 的容器的日志:
package mainimport ( '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)}查看鏡像列表
獲取本地所有的鏡像,相當于 docker image ls 或 docker images:
package mainimport ( '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) }}拉取鏡像
拉取指定鏡像,相當于 docker pull alpine:
package mainimport ( '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)}拉取私有鏡像
除了公開的鏡像,我們平時還會用到一些私有鏡像,可以是 DockerHub 上私有鏡像,也可以是自托管的鏡像倉庫,比如 harbor。這個時候,我們需要提供對應(yīng)的憑證才可以拉取鏡像。
值得注意的是:在使用 Docker API 的 Go SDK 時,憑證是以明文的方式進行傳輸?shù)?,所以如果是自建的鏡像倉庫,請務(wù)必使用 HTTPS!
package mainimport ( '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)}保存容器成鏡像
我們可以將一個已有的容器通過 commit 保存成一個鏡像:
package mainimport ( '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)}管理遠程的 Docker
當然,除了可以管理本地的 Docker, 我們同樣也可以通過使用 Golang + Docker API 管理遠程的 Docker。
遠程連接默認 Docker 是通過非網(wǎng)絡(luò)的 Unix 套接字運行的,只能夠進行本地通信(/var/run/docker.sock),是不能夠直接遠程連接 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修改 client
創(chuàng)建 client 的時候需要指定遠程 Docker 的地址,這樣就可以像管理本地 Docker 一樣管理遠程的 Docker 了:
cli, err = client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation(), client.WithHost('tcp://192.168.59.3:2375'))總結(jié)
現(xiàn)在已經(jīng)有很多可以管理 Docker 的產(chǎn)品,它們便是這樣進行實現(xiàn)的,比如:portainer。
到此這篇關(guān)于使用Golang玩轉(zhuǎn)Docker API的實踐的文章就介紹到這了,更多相關(guān)Golang運行Docker API內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Warning: require(): open_basedir restriction in effect,目錄配置open_basedir報錯問題分析2. ASP中常用的22個FSO文件操作函數(shù)整理3. Vue+elementUI下拉框自定義顏色選擇器方式4. React+umi+typeScript創(chuàng)建項目的過程5. SharePoint Server 2019新特性介紹6. php測試程序運行速度和頁面執(zhí)行速度的代碼7. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究8. ASP的Global.asa文件技巧用法9. ASP中if語句、select 、while循環(huán)的使用方法10. html清除浮動的6種方法示例
