package main import ( "context" "fmt" "io/ioutil" "log" "net/http" "os" "strings" //"docker.io/go-docker" //"docker.io/go-docker/api/types" "github.com/docker/docker/api/types" "github.com/docker/docker/client" ) const ( fixedDockerVersion = "1.38" ) var ( dockerCtx context.Context dockerCLI *client.Client ) func setupContext() { if dockerCtx == nil { dockerCtx = context.Background() } } func setupDockerCLI() (err error) { os.Setenv("DOCKER_API_VERSION", "1.27") if dockerCLI == nil { dockerCLI, err = client.NewEnvClient() } return err } func listDockerContainers() { log.Println("listing the docker containers...") // setup the context and the docker cli connection setupContext() setupDockerCLI() // get a list of all networks networks, err := dockerCLI.NetworkList(context.Background(), types.NetworkListOptions{}) if err != nil { panic(err) } // get a list of all containers containers, err := dockerCLI.ContainerList(context.Background(), types.ContainerListOptions{}) if err != nil { panic(err) } // find the container names of the companion containers var companionContainerNames []string for _, container := range containers { if strings.Contains(container.Image, "companion") { companionContainerNames = append(companionContainerNames, container.Names[0][1:]) } } fmt.Printf("%#v\n\n", companionContainerNames) // find the IPs of the companion containers var companionContainerIPs []string for _, network := range networks { for _, container := range network.Containers { for _, name := range companionContainerNames { if container.Name == name { companionContainerIPs = append(companionContainerIPs, container.IPv4Address) } } } } fmt.Printf("%#v\n\n", companionContainerIPs) // print all ips found for _, rawip := range companionContainerIPs { if strings.Compare(rawip[0:3], "172") == 0 { ip := rawip[:len(rawip)-3] fmt.Printf("ip: %s\n\n", ip) // get the stats from the container stats, err := getStats(ip, 8080) if err != nil { fmt.Println(err) } fmt.Printf("%s\n", stats) } } } func getStats(containerName string, containerPort uint16) (string, error) { url := fmt.Sprintf("http://%s:%d/api/getStats", containerName, containerPort) log.Printf("url: %s", url) resp, err := http.Get(url) if err != nil { return "", fmt.Errorf("could not make the http get request: %v", err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return "", fmt.Errorf("could not read the request body: %v", err) } return string(body), nil }