Docker 提供了一個(gè)與 Docker 守護進(jìn)程交互的 API (稱(chēng)為Docker Engine API),我們可以使用官方提供的 Go 語(yǔ)言的 SDK 進(jìn)行構建和擴展 Docker 應用程序和解決方案。
通過(guò)下面的命令就可以安裝 SDK 了:
go get github.com/docker/docker/client
該部分會(huì )介紹如何使用 Golang + Docker API 進(jìn)行管理本地的 Docker。
第一個(gè)例子將展示如何運行容器,相當于 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) }
還可以在后臺運行容器,相當于 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) }
列出正在運行的容器,就像使用 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,我們可以通過(guò)修改 types.ContainerListOptions 中的 All 屬性達到這個(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) }
通過(guò)上面的例子,我們可以獲取容器的列表,所以在這個(gè)案例中,我們可以去停止所有正在運行的容器。
注意:不要在生產(chǎn)服務(wù)器上運行下面的代碼。
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") } }
通過(guò)指定容器的 ID,我們可以獲取對應 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) }
獲取本地所有的鏡像,相當于 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) } }
拉取指定鏡像,相當于 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) }
除了公開(kāi)的鏡像,我們平時(shí)還會(huì )用到一些私有鏡像,可以是 DockerHub 上私有鏡像,也可以是自托管的鏡像倉庫,比如 。這個(gè)時(shí)候,我們需要提供對應的憑證才可以拉取鏡像。
值得注意的是:在使用 Docker API 的 Go SDK 時(shí),憑證是以明文的方式進(jìn)行傳輸的,所以如果是自建的鏡像倉庫,請務(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è)已有的容器通過(guò) 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) }
當然,除了可以管理本地的 Docker, 我們同樣也可以通過(guò)使用 Golang + Docker API 管理遠程的 Docker。
默認 Docker 是通過(guò)非網(wǎng)絡(luò )的 Unix 套接字運行的,只能夠進(jìn)行本地通信(/var/run/docker.sock),是不能夠直接遠程連接 Docker 的。
我們需要編輯配置文件 /etc/docker/daemon.json,并修改以下內容(把 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í)候需要指定遠程 Docker 的地址,這樣就可以像管理本地 Docker 一樣管理遠程的 Docker 了:
cli, err = client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation(), client.WithHost("tcp://192.168.59.3:2375"))
現在已經(jīng)有很多可以管理 Docker 的產(chǎn)品,它們便是這樣進(jìn)行實(shí)現的,比如:。
到此這篇關(guān)于使用Golang玩轉Docker API的實(shí)踐的文章就介紹到這了,更多相關(guān)Golang運行Docker API內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站