目錄
站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到丹徒網(wǎng)站設(shè)計(jì)與丹徒網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、國(guó)際域名空間、虛擬空間、企業(yè)郵箱。業(yè)務(wù)覆蓋丹徒地區(qū)。
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管理本地的 Docker
運(yùn)行容器
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)}后臺(tái)運(yùn)行容器
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)}查看容器列表
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) }}
// 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)}停止所有運(yùn)行中的容器
通過(guò)上面的例子,我們可以獲取容器的列表,所以在這個(gè)案例中,我們可以去停止所有正在運(yùn)行的容器。
注意:不要在生產(chǎn)服務(wù)器上運(yùn)行下面的代碼。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") }}獲取指定容器的日志
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)}查看鏡像列表
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) }}拉取鏡像
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)}拉取私有鏡像
除了公開(kāi)的鏡像,我們平時(shí)還會(huì)用到一些私有鏡像,可以是 DockerHub 上私有鏡像,也可以是自托管的鏡像倉(cāng)庫(kù),比如 。這個(gè)時(shí)候,我們需要提供對(duì)應(yīng)的憑證才可以拉取鏡像。
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)}保存容器成鏡像
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)}管理遠(yuǎn)程的 Docker
遠(yuǎn)程連接
默認(rèn) Docker 是通過(guò)非網(wǎng)絡(luò)的 Unix 套接字運(yùn)行的,只能夠進(jìn)行本地通信(/var/run/docker.sock),是不能夠直接遠(yuǎn)程連接 Docker 的。
# vi /etc/docker/daemon.json{ "hosts": [ "tcp://192.168.59.3:2375", "unix:///var/run/docker.sock" ]}systemctl restart docker修改 client
cli, err = client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation(), client.WithHost("tcp://192.168.59.3:2375"))總結(jié)
現(xiàn)在已經(jīng)有很多可以管理 Docker 的產(chǎn)品,它們便是這樣進(jìn)行實(shí)現(xiàn)的,比如:。
到此這篇關(guān)于使用Golang玩轉(zhuǎn)Docker API的實(shí)踐的文章就介紹到這了,更多相關(guān)Golang運(yùn)行Docker API內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
分享題目:使用Golang玩轉(zhuǎn)DockerAPI的實(shí)踐
文章網(wǎng)址:http://jinyejixie.com/article30/ejsgso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、Google、自適應(yīng)網(wǎng)站、響應(yīng)式網(wǎng)站、品牌網(wǎng)站制作、網(wǎng)站營(yíng)銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)