/
dockerinfo.go
49 lines (40 loc) · 1.26 KB
/
dockerinfo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package util
import (
"context"
"strconv"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
// GetTDEngineHostName Get TDEngien docker host name
func GetTDEngineHostName() (string, string) {
ctx := context.Background()
// get client object
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
panic(err)
}
// get container list. this function is similar to docker ps
containers, err := cli.ContainerList(ctx, types.ContainerListOptions{})
// include all containers that in runtime or showdown. ContainerList is similar to docker ps -a
//containers, err := cli.ContainerList(ctx, types.ContainerListOptions{All: true})
if err != nil {
panic(err)
}
taosdockerhost := ""
taosdockerport := ""
// get all result
for _, container := range containers {
// fmt.Println(container.ID, container.Names, container.Created, container.Status, container.Ports)
if strings.Contains(container.Names[0][1:], "taos") {
// filter docker name . using TDEngine
taosdockerhost = container.Names[0][1:]
for _, v := range container.Ports {
if v.PublicPort != 0 {
taosdockerport = strconv.Itoa(int(v.PublicPort))
}
}
}
}
return taosdockerhost, taosdockerport
}