亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何通過golang獲取容器ID

如何通過golang獲取容器ID

Go
慕妹3242003 2023-06-12 16:12:19
我使用 golang 開發應用程序。我想在應用程序中獲取容器。我已經厭倦了 shell。但我想通過 go 獲取容器。謝謝
查看完整描述

1 回答

?
肥皂起泡泡

TA貢獻1829條經驗 獲得超6個贊

你可以使用 docker/client

https://godoc.org/github.com/docker/docker/client


示例代碼:


# listcontainers.go


package main


import (

? ? "context"

? ? "fmt"


? ? "github.com/docker/docker/api/types"

? ? "github.com/docker/docker/client"

)


func main() {

? ? cli, err := client.NewClientWithOpts(client.FromEnv)

? ? if err != nil {

? ? ? ? panic(err)

? ? }


? ? containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})

? ? if err != nil {

? ? ? ? panic(err)

? ? }


? ? for _, container := range containers {

? ? ? ? fmt.Printf("%s %s\n", container.ID[:10], container.Image)

? ? }

}

然后像這樣執行


DOCKER_API_VERSION=1.35 go run listcontainers.go


查看完整回答
反對 回復 2023-06-12
  • 1 回答
  • 0 關注
  • 201 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號