要从 Golang Docker SDK 中的 types.ContainerJSON 结构体获取子网网络名称和地址,可以使用以下代码:

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)
    if err != nil {
        panic(err)
    }

    containerID := "your-container-id"

    containerJSON, err := cli.ContainerInspect(ctx, containerID)
    if err != nil {
        panic(err)
    }

    // 获取子网网络名称和地址
    for _, network := range containerJSON.NetworkSettings.Networks {
        fmt.Printf("Subnet network name: %s\n", network.NetworkID)
        fmt.Printf("Subnet network address: %s\n", network.IPAddress)
    }
}

此代码将打印出子网网络名称和地址。请注意,这将仅适用于具有子网网络的容器。如果容器没有子网网络,将无法检索这些值。


原文地址: https://www.cveoy.top/t/topic/fWyt 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录